UNPKG

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