Skip to content

Commit

Permalink
Add a main_module option.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobie committed Oct 31, 2010
1 parent ac79ed4 commit f2fe0f3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions bin/modulrize
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions lib/modulr/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def to_js(buffer = '')
buffer << "\n(function() {"
buffer << lib
buffer << transport
reorder_top_level_modules
buffer << requires
buffer << "})();\n"
end
Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions lib/modulr/sync_collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.