modulr 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -41,6 +41,10 @@ To process a JavaScript source file, just run:
41
41
 
42
42
  $ modulrize filename.js > output.js
43
43
 
44
+ You can also simultaneously process multiples files like so:
45
+
46
+ $ modulrize filename.js other_filename.js > output.js
47
+
44
48
  Options are as follows:
45
49
 
46
50
  -o, --output=FILE Write the output to FILE. Defaults to stdout.
@@ -48,18 +52,21 @@ Options are as follows:
48
52
  --lazy-eval [MODULES] Enable lazy evaluation of all JS modules or of those specified by MODULES.
49
53
  MODULES accepts a comma-separated list of identifiers.
50
54
  --minify Minify output using YUI Compressor.
51
- --global-export=GLOBAL_VAR Export main module's exports to the GLOBAL_VAR global variable.
52
- --sync Avoid using require.ensure.
55
+ --global-export[=GLOBAL_VAR] If GLOBAL_VAR is specified and only one module is being processed, exports it to the GLOBAL_VAR global variable.
56
+ If GLOBAL_VAR is specified and multiple modules are being processed, exports each one of them as a property of GLOBAL_VAR.
57
+ If GLOBAL_VAR isn't specified, exports the module to global variables corresponding to their identifier.
58
+ --sync Load all dependencies synchronously.
53
59
  --dependency-graph[=OUTPUT] Create a dependency graph of the module.
54
60
  -h, --help Show this message.
55
61
 
56
62
  Minification options (these are forwarded to YUI Compressor without the "minify-" prefix):
57
63
 
58
64
  --minify-disable-optimizations Disable all micro optimizations.
59
- --minify-nomunge Minify only, do not obfuscate.
60
- --minify-verbose Display informational messages and warnings.
61
- --minify-line-break COLUMN Insert a line break after the specified column number.
62
- --minify-preserve-semi Preserve all semicolons.
65
+ --minify-nomunge Minify only, do not obfuscate.
66
+ --minify-verbose Display informational messages and warnings.
67
+ --minify-line-break COLUMN Insert a line break after the specified column number.
68
+ --minify-preserve-semi Preserve all semicolons.
69
+
63
70
 
64
71
  Specs
65
72
  -----
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.6.0
data/bin/modulrize CHANGED
@@ -31,8 +31,8 @@ opts = OptionParser.new do |opts|
31
31
  options[:minify] = minify
32
32
  end
33
33
 
34
- opts.on('--global-export=GLOBAL_VAR', 'Export main module\'s exports to the GLOBAL_VAR global variable.') do |global|
35
- options[:global] = global
34
+ opts.on('--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.') do |global|
35
+ options[:global] = global || true
36
36
  end
37
37
 
38
38
  opts.on('--sync', 'Load all dependencies synchronously.') do |global|
@@ -87,7 +87,9 @@ begin
87
87
  if options.delete(:dependency_graph)
88
88
  result = Modulr.graph(ARGV.first, options)
89
89
  else
90
- result = Modulr.ize(ARGV.first, options)
90
+ args = opts.default_argv.dup
91
+ args << options
92
+ result = Modulr.ize(*args)
91
93
  output.print(result)
92
94
  end
93
95
  ensure
@@ -1,27 +1,70 @@
1
1
  module Modulr
2
2
  class Collector
3
- attr_reader :modules, :main
3
+ attr_reader :modules, :top_level_modules
4
4
 
5
5
  def initialize(options = {})
6
6
  @root = options[:root]
7
7
  @lazy_eval = options[:lazy_eval]
8
8
  @modules = []
9
+ @top_level_modules = []
9
10
  end
10
11
 
11
12
  def parse_file(path)
12
- @src = File.read(path)
13
- @root ||= File.dirname(path)
14
- @main = JSModule.new(File.basename(path, '.js'), @root, path)
15
- collect_dependencies(main)
13
+ parse_files(path)
16
14
  end
17
15
 
16
+ def parse_files(*paths)
17
+ reset
18
+ paths.each do |path|
19
+ add_module_from_path(path)
20
+ end
21
+ end
22
+
23
+ def reset
24
+ modules.clear
25
+ top_level_modules.clear
26
+ end
27
+ private :reset
28
+
29
+ def module_from_path(path)
30
+ identifier = File.basename(path, '.js')
31
+ root = @root || File.dirname(path)
32
+ JSModule.new(identifier, root, path)
33
+ end
34
+ private :module_from_path
35
+
36
+ def add_module_from_path(path)
37
+ js_module = module_from_path(path)
38
+ top_level_modules << js_module
39
+ collect_dependencies(js_module)
40
+ js_module
41
+ end
42
+ private :add_module_from_path
43
+
18
44
  def to_js(buffer = '')
19
- buffer << File.read(PATH_TO_MODULR_JS)
20
- buffer << "\n(function(require, module) {"
45
+ buffer << globals
46
+ buffer << "\n(function() {"
47
+ buffer << lib
21
48
  buffer << transport
22
- buffer << main.ensure
23
- buffer << "})(modulr.require, modulr.require.main);\n"
49
+ buffer << requires
50
+ buffer << "})();\n"
51
+ end
52
+
53
+ def lib
54
+ src = File.read(PATH_TO_MODULR_JS)
55
+ "#{src}\nvar require = modulr.require, module = require.main;\n"
56
+ end
57
+ private :lib
58
+
59
+ def requires
60
+ top_level_modules.map { |m| m.ensure }.join("\n")
61
+ end
62
+ private :requires
63
+
64
+ def globals
65
+ ''
24
66
  end
67
+ private :globals
25
68
 
26
69
  def transport
27
70
  pairs = modules.map do |m|
@@ -34,6 +77,7 @@ module Modulr
34
77
  end
35
78
  "require.define({#{pairs.join(', ')}\n});"
36
79
  end
80
+ private :transport
37
81
 
38
82
  private
39
83
  def collect_dependencies(js_module)
@@ -6,27 +6,48 @@ module Modulr
6
6
  super
7
7
  end
8
8
 
9
- def to_js(buffer = '')
10
- buffer << "#{define_global} = (function() {\n"
11
- buffer << File.read(PATH_TO_MODULR_SYNC_JS)
12
- buffer << transport
13
- buffer << "\n return require('#{main.id}');\n"
14
- buffer << "})();\n"
9
+ def globals
10
+ if @global == true
11
+ top_level_modules.map { |m| define_global(m.id) }.join
12
+ else
13
+ define_global(@global)
14
+ end
15
15
  end
16
+ private :globals
16
17
 
17
- def define_global
18
- if @global.include?('.')
19
- props = @global.split('.')
18
+ def define_global(global)
19
+ if global.include?('.')
20
+ props = global.split('.')
20
21
  str = props.shift
21
22
  results = "var #{str};"
22
23
  props.each do |prop|
23
24
  results << "\n#{str} = #{str} || {};"
24
25
  str << ".#{prop}"
25
26
  end
26
- "#{results}\n#{str}"
27
+ "#{results}\n#{str};\n"
28
+ else
29
+ "var #{global};\n"
30
+ end
31
+ end
32
+ private :define_global
33
+
34
+ def requires
35
+ if @global == true
36
+ top_level_modules.map do |m|
37
+ "\n #{m.id} = require('#{m.id}');"
38
+ end.join
27
39
  else
28
- "var #{@global}"
40
+ if top_level_modules.size == 1 #export to named global
41
+ "\n #{@global} = require('#{top_level_modules.first.id}');"
42
+ else
43
+ #export to props of named global
44
+ top_level_modules.inject("\n#{@global} = {};") do |str, m|
45
+ str << "\n #{@global}.#{m.id} = require('#{m.id}');"
46
+ end
47
+ end
29
48
  end
30
49
  end
50
+ private :requires
51
+
31
52
  end
32
53
  end
@@ -1,16 +1,19 @@
1
1
  module Modulr
2
2
  class SyncCollector < Collector
3
- def parse_file(path)
4
- super(path)
5
- modules << main unless modules.include?(main)
3
+ def add_module_from_path(path)
4
+ js_module = super(path)
5
+ modules << js_module unless modules.include?(js_module)
6
6
  end
7
7
 
8
- def to_js(buffer = '')
9
- buffer << "(function() {\n"
10
- buffer << File.read(PATH_TO_MODULR_SYNC_JS)
11
- buffer << transport
12
- buffer << "\nrequire('#{main.id}');\n"
13
- buffer << "})();\n"
8
+ def lib
9
+ File.read(PATH_TO_MODULR_SYNC_JS)
14
10
  end
11
+ private :lib
12
+
13
+ def requires
14
+ top_level_modules.map { |m| "\n require('#{m.id}');" }.join
15
+ end
16
+ private :requires
17
+
15
18
  end
16
19
  end
data/lib/modulr.rb CHANGED
@@ -17,7 +17,10 @@ module Modulr
17
17
  PATH_TO_MODULR_JS = File.join(LIB_DIR, '..', 'assets', 'modulr.js')
18
18
  PATH_TO_MODULR_SYNC_JS = File.join(LIB_DIR, '..', 'assets', 'modulr.sync.js')
19
19
 
20
- def self.ize(input_filename, options = {})
20
+ def self.ize(*args)
21
+ options = args.last.is_a?(Hash) ? args.pop : {}
22
+ input_files = args
23
+
21
24
  if options[:global]
22
25
  collector = GlobalExportCollector.new(options)
23
26
  elsif options[:sync]
@@ -25,7 +28,8 @@ module Modulr
25
28
  else
26
29
  collector = Collector.new(options)
27
30
  end
28
- collector.parse_file(input_filename)
31
+
32
+ collector.parse_files(*input_files)
29
33
  minify(collector.to_js, options[:minify])
30
34
  end
31
35
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 5
7
+ - 6
8
8
  - 0
9
- version: 0.5.0
9
+ version: 0.6.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Tobie Langel
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-04 00:00:00 +02:00
17
+ date: 2010-10-31 00:00:00 +02:00
18
18
  default_executable: modulrize
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency