1 |
|
2 | void function () {
|
3 | var $0, _, aliasPair, btoa, build, cache$3, cache$4, CJSEverywhere, dep, deps, escodegen, escodegenFormat, fs, handlerPair, knownOpts, match, nopt, opt, optAliases, options, originalEntryPoint, path, positionalArgs, root, startBuild, stdinput;
|
4 | fs = require('fs');
|
5 | path = require('path');
|
6 | escodegen = require('escodegen');
|
7 | nopt = require('nopt');
|
8 | btoa = require('Base64').btoa;
|
9 | CJSEverywhere = require('./module');
|
10 | escodegenFormat = {
|
11 | indent: {
|
12 | style: ' ',
|
13 | base: 0
|
14 | },
|
15 | renumber: true,
|
16 | hexadecimal: true,
|
17 | quotes: 'auto',
|
18 | parentheses: false
|
19 | };
|
20 | knownOpts = {};
|
21 | for (var cache$ = [
|
22 | 'deps',
|
23 | 'help',
|
24 | 'ignore-missing',
|
25 | 'inline-source-map',
|
26 | 'inline-sources',
|
27 | 'minify',
|
28 | 'node',
|
29 | 'verbose',
|
30 | 'watch'
|
31 | ], i$ = 0, length$ = cache$.length; i$ < length$; ++i$) {
|
32 | opt = cache$[i$];
|
33 | knownOpts[opt] = Boolean;
|
34 | }
|
35 | for (var cache$1 = [
|
36 | 'export',
|
37 | 'output',
|
38 | 'root',
|
39 | 'source-map'
|
40 | ], i$1 = 0, length$1 = cache$1.length; i$1 < length$1; ++i$1) {
|
41 | opt = cache$1[i$1];
|
42 | knownOpts[opt] = String;
|
43 | }
|
44 | for (var cache$2 = [
|
45 | 'alias',
|
46 | 'handler'
|
47 | ], i$2 = 0, length$2 = cache$2.length; i$2 < length$2; ++i$2) {
|
48 | opt = cache$2[i$2];
|
49 | knownOpts[opt] = [
|
50 | String,
|
51 | Array
|
52 | ];
|
53 | }
|
54 | optAliases = {
|
55 | a: '--alias',
|
56 | h: '--handler',
|
57 | m: '--minify',
|
58 | o: '--output',
|
59 | r: '--root',
|
60 | s: '--source-map',
|
61 | v: '--verbose',
|
62 | w: '--watch',
|
63 | x: '--export'
|
64 | };
|
65 | options = nopt(knownOpts, optAliases, process.argv, 2);
|
66 | positionalArgs = options.argv.remain;
|
67 | delete options.argv;
|
68 | if (null != options.node)
|
69 | options.node;
|
70 | else
|
71 | options.node = true;
|
72 | if (null != options['inline-sources'])
|
73 | options['inline-sources'];
|
74 | else
|
75 | options['inline-sources'] = true;
|
76 | if (null != options.alias)
|
77 | options.alias;
|
78 | else
|
79 | options.alias = [];
|
80 | if (null != options.handler)
|
81 | options.handler;
|
82 | else
|
83 | options.handler = [];
|
84 | options.ignoreMissing = options['ignore-missing'];
|
85 | options.sourceMap = options['source-map'];
|
86 | options.inlineSources = options['inline-sources'];
|
87 | options.inlineSourceMap = options['inline-source-map'];
|
88 | if (options.help) {
|
89 | $0 = process.argv[0] === 'node' ? process.argv[1] : process.argv[0];
|
90 | $0 = path.basename($0);
|
91 | console.log('\n Usage: ' + $0 + ' OPT* path/to/entry-file.ext OPT*\n\n -a, --alias ALIAS:TO replace requires of file identified by ALIAS with TO\n -h, --handler EXT:MODULE handle files with extension EXT with module MODULE\n -m, --minify minify output\n -o, --output FILE output to FILE instead of stdout\n -r, --root DIR unqualified requires are relative to DIR; default: cwd\n -s, --source-map FILE output a source map to FILE\n -v, --verbose verbose output sent to stderr\n -w, --watch watch input files/dependencies for changes and rebuild bundle\n -x, --export NAME export the given entry module as NAME\n --deps do not bundle; just list the files that would be bundled\n --help display this help message and exit\n --ignore-missing continue without error when dependency resolution fails\n --inline-source-map include the source map as a data URI in the generated bundle\n --inline-sources include source content in generated source maps; default: on\n --node include process object; emulate node environment; default: on\n --version display the version number and exit\n');
|
92 | process.exit(0);
|
93 | }
|
94 | if (options.version) {
|
95 | console.log(require('../package.json').version);
|
96 | process.exit(0);
|
97 | }
|
98 | if (!(positionalArgs.length === 1)) {
|
99 | console.error('wrong number of entry points given; expected 1');
|
100 | process.exit(1);
|
101 | }
|
102 | options.aliases = {};
|
103 | for (var i$3 = 0, length$3 = options.alias.length; i$3 < length$3; ++i$3) {
|
104 | aliasPair = options.alias[i$3];
|
105 | match = aliasPair.match((cache$3 = /([^:]+):(.*)/, null != cache$3 ? cache$3 : []));
|
106 | if (null != match) {
|
107 | options.aliases[match[1]] = match[2];
|
108 | } else {
|
109 | console.error('invalid alias: ' + aliasPair);
|
110 | process.exit(1);
|
111 | }
|
112 | }
|
113 | delete options.alias;
|
114 | options.handlers = {};
|
115 | for (var i$4 = 0, length$4 = options.handler.length; i$4 < length$4; ++i$4) {
|
116 | handlerPair = options.handler[i$4];
|
117 | match = handlerPair.match((cache$4 = /([^:]+):(.*)/, null != cache$4 ? cache$4 : []));
|
118 | if (null != match) {
|
119 | (function (ext, mod) {
|
120 | return options.handlers[ext] = require(mod);
|
121 | }('.' + match[1], match[2]));
|
122 | } else {
|
123 | console.error('invalid handler: ' + handlerPair);
|
124 | process.exit(1);
|
125 | }
|
126 | }
|
127 | delete options.handler;
|
128 | root = options.root ? path.resolve(options.root) : process.cwd();
|
129 | originalEntryPoint = positionalArgs[0];
|
130 | if (options.deps) {
|
131 | deps = CJSEverywhere.traverseDependencies(originalEntryPoint, root, options);
|
132 | for (_ in deps) {
|
133 | if (!isOwn$(deps, _))
|
134 | continue;
|
135 | dep = deps[_];
|
136 | console.log(dep.canonicalName);
|
137 | }
|
138 | process.exit(0);
|
139 | }
|
140 | if (options.watch && !options.output) {
|
141 | console.error('--watch requires --ouput');
|
142 | process.exit(1);
|
143 | }
|
144 | build = function (entryPoint, processed) {
|
145 | var bundled, cache$5, cache$6, canonicalName, code, datauri, e, esmangle, file, fileContents, filename, map, newDeps, sourceMappingUrl;
|
146 | if (null == processed)
|
147 | processed = {};
|
148 | try {
|
149 | newDeps = CJSEverywhere.traverseDependencies(entryPoint, root, options);
|
150 | if (options.watch)
|
151 | for (filename in newDeps) {
|
152 | if (!isOwn$(newDeps, filename))
|
153 | continue;
|
154 | dep = newDeps[filename];
|
155 | console.error('built ' + dep.canonicalName + ' (' + options.cache[filename] + ')');
|
156 | }
|
157 | for (file in newDeps) {
|
158 | if (!isOwn$(newDeps, file))
|
159 | continue;
|
160 | processed[file] = newDeps[file];
|
161 | }
|
162 | } catch (e$) {
|
163 | e = e$;
|
164 | if (options.watch) {
|
165 | console.error('ERROR: ' + e.message);
|
166 | } else {
|
167 | throw e;
|
168 | }
|
169 | }
|
170 | bundled = CJSEverywhere.bundle(processed, originalEntryPoint, root, options);
|
171 | if (options.minify) {
|
172 | esmangle = require('esmangle');
|
173 | bundled = esmangle.mangle(esmangle.optimize(bundled), { destructive: true });
|
174 | }
|
175 | cache$5 = escodegen.generate(bundled, {
|
176 | comment: !options.minify,
|
177 | sourceMap: true,
|
178 | sourceMapWithCode: true,
|
179 | sourceMapRoot: null != options.sourceMap ? path.relative(path.dirname(options.sourceMap), root) || '.' : void 0,
|
180 | format: options.minify ? escodegen.FORMAT_MINIFY : escodegenFormat
|
181 | });
|
182 | code = cache$5.code;
|
183 | map = cache$5.map;
|
184 | if ((options.sourceMap || options.inlineSourceMap) && options.inlineSources)
|
185 | for (filename in processed) {
|
186 | if (!isOwn$(processed, filename))
|
187 | continue;
|
188 | {
|
189 | cache$6 = processed[filename];
|
190 | canonicalName = cache$6.canonicalName;
|
191 | fileContents = cache$6.fileContents;
|
192 | }
|
193 | map.setSourceContent(canonicalName, fileContents);
|
194 | }
|
195 | if (options.sourceMap) {
|
196 | fs.writeFileSync(options.sourceMap, '' + map);
|
197 | sourceMappingUrl = options.output ? path.relative(path.dirname(options.output), options.sourceMap) : options.sourceMap;
|
198 | if (!options.inlineSourceMap)
|
199 | code = '' + code + '\n//# sourceMappingURL=' + sourceMappingUrl;
|
200 | }
|
201 | if (options.inlineSourceMap) {
|
202 | datauri = 'data:application/json;charset=utf-8;base64,' + btoa('' + map);
|
203 | code = '' + code + '\n//# sourceMappingURL=' + datauri;
|
204 | }
|
205 | if (options.output) {
|
206 | fs.writeFileSync(options.output, code);
|
207 | } else {
|
208 | process.stdout.write('' + code + '\n');
|
209 | }
|
210 | if (options.watch || options.verbose)
|
211 | console.error('BUNDLE COMPLETE');
|
212 | return processed;
|
213 | };
|
214 | startBuild = function () {
|
215 | var processed, startWatching, watching;
|
216 | if (options.watch) {
|
217 | options.cache = {};
|
218 | console.error('BUNDLING starting at ' + originalEntryPoint);
|
219 | }
|
220 | processed = build(originalEntryPoint);
|
221 | if (options.watch) {
|
222 | watching = [];
|
223 | return (startWatching = function (processed) {
|
224 | return function (accum$) {
|
225 | var canonicalName, file;
|
226 | for (file in processed) {
|
227 | if (!isOwn$(processed, file))
|
228 | continue;
|
229 | canonicalName = processed[file].canonicalName;
|
230 | if (!!in$(file, watching))
|
231 | continue;
|
232 | accum$.push(function (file, canonicalName) {
|
233 | watching.push(file);
|
234 | return fs.watchFile(file, {
|
235 | persistent: true,
|
236 | interval: 500
|
237 | }, function (curr, prev) {
|
238 | var ino;
|
239 | ino = process.platform === 'win32' ? null != curr.ino : curr.ino;
|
240 | if (!ino) {
|
241 | console.error('WARNING: watched file ' + file + ' has disappeared');
|
242 | return;
|
243 | }
|
244 | console.error('REBUNDLING starting at ' + canonicalName);
|
245 | processed = build(file, processed);
|
246 | startWatching(processed);
|
247 | });
|
248 | }(file, canonicalName));
|
249 | }
|
250 | return accum$;
|
251 | }.call(this, []);
|
252 | })(processed);
|
253 | }
|
254 | };
|
255 | if (originalEntryPoint === '-') {
|
256 | stdinput = '';
|
257 | process.stdin.on('data', function (data) {
|
258 | return stdinput += data;
|
259 | });
|
260 | process.stdin.on('end', function () {
|
261 | originalEntryPoint = require('mktemp').createFileSync('temp-XXXXXXXXX.js');
|
262 | fs.writeFileSync(originalEntryPoint, stdinput);
|
263 | process.on('exit', function () {
|
264 | return fs.unlinkSync(originalEntryPoint);
|
265 | });
|
266 | return startBuild();
|
267 | });
|
268 | process.stdin.setEncoding('utf8');
|
269 | process.stdin.resume();
|
270 | } else {
|
271 | startBuild();
|
272 | }
|
273 | function isOwn$(o, p) {
|
274 | return {}.hasOwnProperty.call(o, p);
|
275 | }
|
276 | function in$(member, list) {
|
277 | for (var i = 0, length = list.length; i < length; ++i)
|
278 | if (i in list && list[i] === member)
|
279 | return true;
|
280 | return false;
|
281 | }
|
282 | }.call(this);
|