Permalink
Browse files

Add a main_module option.

  • Loading branch information...
1 parent ac79ed4 commit f2fe0f3360e04c15e7690ff8405670f84342a735 @tobie tobie committed Oct 31, 2010
Showing with 25 additions and 2 deletions.
  1. +1 −0 README.markdown
  2. +4 −0 bin/modulrize
  3. +19 −0 lib/modulr/collector.rb
  4. +1 −2 lib/modulr/sync_collector.rb
View
1 README.markdown
@@ -55,6 +55,7 @@ Options are as follows:
--global-export[=GLOBAL_VAR] If GLOBAL_VAR is specified and only one module is being processed, exports it to the GLOBAL_VAR global variable.
If GLOBAL_VAR is specified and multiple modules are being processed, exports each one of them as a property of GLOBAL_VAR.
If GLOBAL_VAR isn't specified, exports the module to global variables corresponding to their identifier.
+ --main-module=MAIN_MODULE Set the identifier of the main module (available through `require.main`).
--sync Load all dependencies synchronously.
--dependency-graph[=OUTPUT] Create a dependency graph of the module.
-h, --help Show this message.
View
4 bin/modulrize
@@ -35,6 +35,10 @@ opts = OptionParser.new do |opts|
options[:global] = global || true
end
+ opts.on('--main-module=MAIN_MODULE', 'Set the identifier of the main module (available through `require.main`).') do |main_module|
+ options[:main_module] = main_module
+ end
+
opts.on('--sync', 'Load all dependencies synchronously.') do |global|
options[:sync] = true
end
View
19 lib/modulr/collector.rb
@@ -15,6 +15,7 @@ def to_js(buffer = '')
buffer << "\n(function() {"
buffer << lib
buffer << transport
+ reorder_top_level_modules
buffer << requires
buffer << "})();\n"
end
@@ -37,6 +38,24 @@ def reset
top_level_modules.clear
end
+ def main_module?
+ !!main_module
+ end
+
+ def main_module
+ if mm_id = @options[:main_module]
+ @top_level_modules.find { |m| m.id == mm_id }
+ end
+ end
+
+ def reorder_top_level_modules
+ if mm = main_module
+ index = top_level_modules.index(mm)
+ top_level_modules[index] = top_level_modules[0]
+ top_level_modules[0] = mm
+ end
+ end
+
def module_from_path(path)
identifier = File.basename(path, '.js')
root = @root || File.dirname(path)
View
3 lib/modulr/sync_collector.rb
@@ -8,7 +8,7 @@ def add_module_from_path(path)
private
def lib
output = File.read(PATH_TO_MODULR_SYNC_JS)
- if top_level_modules.size > 1
+ if top_level_modules.size > 1 && !main_module?
output << "\nvar module = {};\n"
output << "\nrequire.main = module;\n"
end
@@ -18,6 +18,5 @@ def lib
def requires
top_level_modules.map { |m| "\n require('#{m.id}');" }.join
end
-
end
end

0 comments on commit f2fe0f3

Please sign in to comment.