diff --git a/README.markdown b/README.markdown index 791839b..4294433 100644 --- a/README.markdown +++ b/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. diff --git a/bin/modulrize b/bin/modulrize index 50716ba..1f29ee5 100755 --- a/bin/modulrize +++ b/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 diff --git a/lib/modulr/collector.rb b/lib/modulr/collector.rb index d041fb6..c1a5788 100644 --- a/lib/modulr/collector.rb +++ b/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) diff --git a/lib/modulr/sync_collector.rb b/lib/modulr/sync_collector.rb index 9abbfac..f751be6 100644 --- a/lib/modulr/sync_collector.rb +++ b/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 \ No newline at end of file