forked from codespeaks/modulr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodulrize
executable file
·97 lines (78 loc) · 2.98 KB
/
modulrize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../lib/modulr'
require 'optparse'
options = {
:output => STDOUT
}
opts = OptionParser.new do |opts|
opts.banner = 'Usage: modulrize program.js [options] > output.js'
opts.separator ''
opts.separator 'Options:'
opts.on('-o', '--output=FILE', 'Write the output to FILE. Defaults to stdout.') do |output|
options[:output] = File.open(output, 'w')
end
opts.on('-r', '--root=DIR', 'Set DIR as root directory. Defaults to the directory containing FILE.') do |root|
options[:root] = root
end
opts.on('--lazy-eval [MODULES]', Array,
'Enable lazy evaluation of all JS modules or of those specified by MODULES.',
'MODULES accepts a comma-separated list of identifiers.') do |modules|
modules = true unless modules
options[:lazy_eval] = modules
end
opts.on('--minify', 'Minify output using YUI Compressor.') do |minify|
options[:minify] = minify
end
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|
options[:global] = global || true
end
opts.on('--sync', 'Load all dependencies synchronously.') do |global|
options[:sync] = true
end
opts.on('--dependency-graph[=OUTPUT]', 'Create a dependency graph of the module.') do |output|
options[:dependency_graph] = true
options[:output] = output
end
opts.on('-h', '--help', 'Show this message.') do
puts opts
exit
end
opts.separator ''
opts.separator 'Minification options (these are forwarded to YUI Compressor without the "minify-" prefix):'
{
'line-break COLUMN' => 'Insert a line break after the specified column number.',
'verbose' => 'Display informational messages and warnings.',
'nomunge' => 'Minify only, do not obfuscate.',
'preserve-semi' => 'Preserve all semicolons.',
'disable-optimizations' => 'Disable all micro optimizations.'
}.each do |option, message|
prefixed_option = option.sub(/\A(\[no-\])?/, '--\\1minify-')
def normalize_option(option)
option.gsub('-', '_').to_sym
end
opts.on(prefixed_option, message) do |value|
if !options[:minify] || options[:minify] == true
options[:minify] = {}
end
options[:minify][normalize_option(option)] = value
end
end
end
opts.parse!
output = options.delete(:output)
begin
if ARGV.size != 1
puts opts
exit 1
end
if options.delete(:dependency_graph)
result = Modulr.graph(ARGV.first, options)
else
args = opts.default_argv.dup
args << options
result = Modulr.ize(*args)
output.print(result)
end
ensure
output.close if output.respond_to?(:close)
end