source: trip-planner-front/node_modules/terser/lib/cli.js@ 571e0df

Last change on this file since 571e0df was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 17.7 KB
Line 
1import { minify, _default_options } from "../main.js";
2import { parse } from "./parse.js";
3import {
4 AST_Assign,
5 AST_Array,
6 AST_Constant,
7 AST_Node,
8 AST_PropAccess,
9 AST_RegExp,
10 AST_Sequence,
11 AST_Symbol,
12 AST_Token,
13 walk
14} from "./ast.js";
15import { OutputStream } from "./output.js";
16
17export async function run_cli({ program, packageJson, fs, path }) {
18 const skip_keys = new Set([ "cname", "parent_scope", "scope", "uses_eval", "uses_with" ]);
19 var files = {};
20 var options = {
21 compress: false,
22 mangle: false
23 };
24 const default_options = await _default_options();
25 program.version(packageJson.name + " " + packageJson.version);
26 program.parseArgv = program.parse;
27 program.parse = undefined;
28
29 if (process.argv.includes("ast")) program.helpInformation = describe_ast;
30 else if (process.argv.includes("options")) program.helpInformation = function() {
31 var text = [];
32 for (var option in default_options) {
33 text.push("--" + (option === "sourceMap" ? "source-map" : option) + " options:");
34 text.push(format_object(default_options[option]));
35 text.push("");
36 }
37 return text.join("\n");
38 };
39
40 program.option("-p, --parse <options>", "Specify parser options.", parse_js());
41 program.option("-c, --compress [options]", "Enable compressor/specify compressor options.", parse_js());
42 program.option("-m, --mangle [options]", "Mangle names/specify mangler options.", parse_js());
43 program.option("--mangle-props [options]", "Mangle properties/specify mangler options.", parse_js());
44 program.option("-f, --format [options]", "Format options.", parse_js());
45 program.option("-b, --beautify [options]", "Alias for --format.", parse_js());
46 program.option("-o, --output <file>", "Output file (default STDOUT).");
47 program.option("--comments [filter]", "Preserve copyright comments in the output.");
48 program.option("--config-file <file>", "Read minify() options from JSON file.");
49 program.option("-d, --define <expr>[=value]", "Global definitions.", parse_js("define"));
50 program.option("--ecma <version>", "Specify ECMAScript release: 5, 2015, 2016 or 2017...");
51 program.option("-e, --enclose [arg[,...][:value[,...]]]", "Embed output in a big function with configurable arguments and values.");
52 program.option("--ie8", "Support non-standard Internet Explorer 8.");
53 program.option("--keep-classnames", "Do not mangle/drop class names.");
54 program.option("--keep-fnames", "Do not mangle/drop function names. Useful for code relying on Function.prototype.name.");
55 program.option("--module", "Input is an ES6 module");
56 program.option("--name-cache <file>", "File to hold mangled name mappings.");
57 program.option("--rename", "Force symbol expansion.");
58 program.option("--no-rename", "Disable symbol expansion.");
59 program.option("--safari10", "Support non-standard Safari 10.");
60 program.option("--source-map [options]", "Enable source map/specify source map options.", parse_js());
61 program.option("--timings", "Display operations run time on STDERR.");
62 program.option("--toplevel", "Compress and/or mangle variables in toplevel scope.");
63 program.option("--wrap <name>", "Embed everything as a function with “exports” corresponding to “name” globally.");
64 program.arguments("[files...]").parseArgv(process.argv);
65 if (program.configFile) {
66 options = JSON.parse(read_file(program.configFile));
67 }
68 if (!program.output && program.sourceMap && program.sourceMap.url != "inline") {
69 fatal("ERROR: cannot write source map to STDOUT");
70 }
71
72 [
73 "compress",
74 "enclose",
75 "ie8",
76 "mangle",
77 "module",
78 "safari10",
79 "sourceMap",
80 "toplevel",
81 "wrap"
82 ].forEach(function(name) {
83 if (name in program) {
84 options[name] = program[name];
85 }
86 });
87
88 if ("ecma" in program) {
89 if (program.ecma != (program.ecma | 0)) fatal("ERROR: ecma must be an integer");
90 const ecma = program.ecma | 0;
91 if (ecma > 5 && ecma < 2015)
92 options.ecma = ecma + 2009;
93 else
94 options.ecma = ecma;
95 }
96 if (program.format || program.beautify) {
97 const chosenOption = program.format || program.beautify;
98 options.format = typeof chosenOption === "object" ? chosenOption : {};
99 }
100 if (program.comments) {
101 if (typeof options.format != "object") options.format = {};
102 options.format.comments = typeof program.comments == "string" ? (program.comments == "false" ? false : program.comments) : "some";
103 }
104 if (program.define) {
105 if (typeof options.compress != "object") options.compress = {};
106 if (typeof options.compress.global_defs != "object") options.compress.global_defs = {};
107 for (var expr in program.define) {
108 options.compress.global_defs[expr] = program.define[expr];
109 }
110 }
111 if (program.keepClassnames) {
112 options.keep_classnames = true;
113 }
114 if (program.keepFnames) {
115 options.keep_fnames = true;
116 }
117 if (program.mangleProps) {
118 if (program.mangleProps.domprops) {
119 delete program.mangleProps.domprops;
120 } else {
121 if (typeof program.mangleProps != "object") program.mangleProps = {};
122 if (!Array.isArray(program.mangleProps.reserved)) program.mangleProps.reserved = [];
123 }
124 if (typeof options.mangle != "object") options.mangle = {};
125 options.mangle.properties = program.mangleProps;
126 }
127 if (program.nameCache) {
128 options.nameCache = JSON.parse(read_file(program.nameCache, "{}"));
129 }
130 if (program.output == "ast") {
131 options.format = {
132 ast: true,
133 code: false
134 };
135 }
136 if (program.parse) {
137 if (!program.parse.acorn && !program.parse.spidermonkey) {
138 options.parse = program.parse;
139 } else if (program.sourceMap && program.sourceMap.content == "inline") {
140 fatal("ERROR: inline source map only works with built-in parser");
141 }
142 }
143 if (~program.rawArgs.indexOf("--rename")) {
144 options.rename = true;
145 } else if (!program.rename) {
146 options.rename = false;
147 }
148
149 let convert_path = name => name;
150 if (typeof program.sourceMap == "object" && "base" in program.sourceMap) {
151 convert_path = function() {
152 var base = program.sourceMap.base;
153 delete options.sourceMap.base;
154 return function(name) {
155 return path.relative(base, name);
156 };
157 }();
158 }
159
160 let filesList;
161 if (options.files && options.files.length) {
162 filesList = options.files;
163
164 delete options.files;
165 } else if (program.args.length) {
166 filesList = program.args;
167 }
168
169 if (filesList) {
170 simple_glob(filesList).forEach(function(name) {
171 files[convert_path(name)] = read_file(name);
172 });
173 } else {
174 await new Promise((resolve) => {
175 var chunks = [];
176 process.stdin.setEncoding("utf8");
177 process.stdin.on("data", function(chunk) {
178 chunks.push(chunk);
179 }).on("end", function() {
180 files = [ chunks.join("") ];
181 resolve();
182 });
183 process.stdin.resume();
184 });
185 }
186
187 await run_cli();
188
189 function convert_ast(fn) {
190 return AST_Node.from_mozilla_ast(Object.keys(files).reduce(fn, null));
191 }
192
193 async function run_cli() {
194 var content = program.sourceMap && program.sourceMap.content;
195 if (content && content !== "inline") {
196 options.sourceMap.content = read_file(content, content);
197 }
198 if (program.timings) options.timings = true;
199
200 try {
201 if (program.parse) {
202 if (program.parse.acorn) {
203 files = convert_ast(function(toplevel, name) {
204 return require("acorn").parse(files[name], {
205 ecmaVersion: 2018,
206 locations: true,
207 program: toplevel,
208 sourceFile: name,
209 sourceType: options.module || program.parse.module ? "module" : "script"
210 });
211 });
212 } else if (program.parse.spidermonkey) {
213 files = convert_ast(function(toplevel, name) {
214 var obj = JSON.parse(files[name]);
215 if (!toplevel) return obj;
216 toplevel.body = toplevel.body.concat(obj.body);
217 return toplevel;
218 });
219 }
220 }
221 } catch (ex) {
222 fatal(ex);
223 }
224
225 let result;
226 try {
227 result = await minify(files, options);
228 } catch (ex) {
229 if (ex.name == "SyntaxError") {
230 print_error("Parse error at " + ex.filename + ":" + ex.line + "," + ex.col);
231 var col = ex.col;
232 var lines = files[ex.filename].split(/\r?\n/);
233 var line = lines[ex.line - 1];
234 if (!line && !col) {
235 line = lines[ex.line - 2];
236 col = line.length;
237 }
238 if (line) {
239 var limit = 70;
240 if (col > limit) {
241 line = line.slice(col - limit);
242 col = limit;
243 }
244 print_error(line.slice(0, 80));
245 print_error(line.slice(0, col).replace(/\S/g, " ") + "^");
246 }
247 }
248 if (ex.defs) {
249 print_error("Supported options:");
250 print_error(format_object(ex.defs));
251 }
252 fatal(ex);
253 return;
254 }
255
256 if (program.output == "ast") {
257 if (!options.compress && !options.mangle) {
258 result.ast.figure_out_scope({});
259 }
260 console.log(JSON.stringify(result.ast, function(key, value) {
261 if (value) switch (key) {
262 case "thedef":
263 return symdef(value);
264 case "enclosed":
265 return value.length ? value.map(symdef) : undefined;
266 case "variables":
267 case "globals":
268 return value.size ? collect_from_map(value, symdef) : undefined;
269 }
270 if (skip_keys.has(key)) return;
271 if (value instanceof AST_Token) return;
272 if (value instanceof Map) return;
273 if (value instanceof AST_Node) {
274 var result = {
275 _class: "AST_" + value.TYPE
276 };
277 if (value.block_scope) {
278 result.variables = value.block_scope.variables;
279 result.enclosed = value.block_scope.enclosed;
280 }
281 value.CTOR.PROPS.forEach(function(prop) {
282 result[prop] = value[prop];
283 });
284 return result;
285 }
286 return value;
287 }, 2));
288 } else if (program.output == "spidermonkey") {
289 try {
290 const minified = await minify(result.code, {
291 compress: false,
292 mangle: false,
293 format: {
294 ast: true,
295 code: false
296 }
297 });
298 console.log(JSON.stringify(minified.ast.to_mozilla_ast(), null, 2));
299 } catch (ex) {
300 fatal(ex);
301 return;
302 }
303 } else if (program.output) {
304 fs.writeFileSync(program.output, result.code);
305 if (options.sourceMap && options.sourceMap.url !== "inline" && result.map) {
306 fs.writeFileSync(program.output + ".map", result.map);
307 }
308 } else {
309 console.log(result.code);
310 }
311 if (program.nameCache) {
312 fs.writeFileSync(program.nameCache, JSON.stringify(options.nameCache));
313 }
314 if (result.timings) for (var phase in result.timings) {
315 print_error("- " + phase + ": " + result.timings[phase].toFixed(3) + "s");
316 }
317 }
318
319 function fatal(message) {
320 if (message instanceof Error) message = message.stack.replace(/^\S*?Error:/, "ERROR:");
321 print_error(message);
322 process.exit(1);
323 }
324
325 // A file glob function that only supports "*" and "?" wildcards in the basename.
326 // Example: "foo/bar/*baz??.*.js"
327 // Argument `glob` may be a string or an array of strings.
328 // Returns an array of strings. Garbage in, garbage out.
329 function simple_glob(glob) {
330 if (Array.isArray(glob)) {
331 return [].concat.apply([], glob.map(simple_glob));
332 }
333 if (glob && glob.match(/[*?]/)) {
334 var dir = path.dirname(glob);
335 try {
336 var entries = fs.readdirSync(dir);
337 } catch (ex) {}
338 if (entries) {
339 var pattern = "^" + path.basename(glob)
340 .replace(/[.+^$[\]\\(){}]/g, "\\$&")
341 .replace(/\*/g, "[^/\\\\]*")
342 .replace(/\?/g, "[^/\\\\]") + "$";
343 var mod = process.platform === "win32" ? "i" : "";
344 var rx = new RegExp(pattern, mod);
345 var results = entries.filter(function(name) {
346 return rx.test(name);
347 }).map(function(name) {
348 return path.join(dir, name);
349 });
350 if (results.length) return results;
351 }
352 }
353 return [ glob ];
354 }
355
356 function read_file(path, default_value) {
357 try {
358 return fs.readFileSync(path, "utf8");
359 } catch (ex) {
360 if ((ex.code == "ENOENT" || ex.code == "ENAMETOOLONG") && default_value != null) return default_value;
361 fatal(ex);
362 }
363 }
364
365 function parse_js(flag) {
366 return function(value, options) {
367 options = options || {};
368 try {
369 walk(parse(value, { expression: true }), node => {
370 if (node instanceof AST_Assign) {
371 var name = node.left.print_to_string();
372 var value = node.right;
373 if (flag) {
374 options[name] = value;
375 } else if (value instanceof AST_Array) {
376 options[name] = value.elements.map(to_string);
377 } else if (value instanceof AST_RegExp) {
378 value = value.value;
379 options[name] = new RegExp(value.source, value.flags);
380 } else {
381 options[name] = to_string(value);
382 }
383 return true;
384 }
385 if (node instanceof AST_Symbol || node instanceof AST_PropAccess) {
386 var name = node.print_to_string();
387 options[name] = true;
388 return true;
389 }
390 if (!(node instanceof AST_Sequence)) throw node;
391
392 function to_string(value) {
393 return value instanceof AST_Constant ? value.getValue() : value.print_to_string({
394 quote_keys: true
395 });
396 }
397 });
398 } catch(ex) {
399 if (flag) {
400 fatal("Error parsing arguments for '" + flag + "': " + value);
401 } else {
402 options[value] = null;
403 }
404 }
405 return options;
406 };
407 }
408
409 function symdef(def) {
410 var ret = (1e6 + def.id) + " " + def.name;
411 if (def.mangled_name) ret += " " + def.mangled_name;
412 return ret;
413 }
414
415 function collect_from_map(map, callback) {
416 var result = [];
417 map.forEach(function (def) {
418 result.push(callback(def));
419 });
420 return result;
421 }
422
423 function format_object(obj) {
424 var lines = [];
425 var padding = "";
426 Object.keys(obj).map(function(name) {
427 if (padding.length < name.length) padding = Array(name.length + 1).join(" ");
428 return [ name, JSON.stringify(obj[name]) ];
429 }).forEach(function(tokens) {
430 lines.push(" " + tokens[0] + padding.slice(tokens[0].length - 2) + tokens[1]);
431 });
432 return lines.join("\n");
433 }
434
435 function print_error(msg) {
436 process.stderr.write(msg);
437 process.stderr.write("\n");
438 }
439
440 function describe_ast() {
441 var out = OutputStream({ beautify: true });
442 function doitem(ctor) {
443 out.print("AST_" + ctor.TYPE);
444 const props = ctor.SELF_PROPS.filter(prop => !/^\$/.test(prop));
445
446 if (props.length > 0) {
447 out.space();
448 out.with_parens(function() {
449 props.forEach(function(prop, i) {
450 if (i) out.space();
451 out.print(prop);
452 });
453 });
454 }
455
456 if (ctor.documentation) {
457 out.space();
458 out.print_string(ctor.documentation);
459 }
460
461 if (ctor.SUBCLASSES.length > 0) {
462 out.space();
463 out.with_block(function() {
464 ctor.SUBCLASSES.forEach(function(ctor) {
465 out.indent();
466 doitem(ctor);
467 out.newline();
468 });
469 });
470 }
471 }
472 doitem(AST_Node);
473 return out + "\n";
474 }
475}
Note: See TracBrowser for help on using the repository browser.