terser 4.8.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/README.md +61 -127
- package/bin/package.json +10 -0
- package/bin/terser +10 -466
- package/dist/bundle.min.js +24167 -1
- package/dist/bundle.min.js.map +1 -1
- package/dist/package.json +10 -0
- package/lib/ast.js +1729 -0
- package/lib/cli.js +487 -0
- package/lib/compress/index.js +7508 -0
- package/lib/equivalent-to.js +302 -0
- package/lib/minify.js +264 -0
- package/lib/mozilla-ast.js +1243 -0
- package/lib/output.js +2192 -0
- package/lib/parse.js +3190 -0
- package/lib/propmangle.js +322 -0
- package/lib/scope.js +965 -0
- package/lib/size.js +448 -0
- package/lib/sourcemap.js +106 -0
- package/lib/transform.js +313 -0
- package/lib/utils/first_in_statement.js +50 -0
- package/lib/utils/index.js +302 -0
- package/main.js +27 -0
- package/package.json +32 -12
- package/tools/exit.cjs +7 -0
- package/tools/terser.d.ts +4 -630
- package/tools/colorless-console.js +0 -11
- package/tools/exit.js +0 -15
- package/tools/node.js +0 -19
package/bin/terser
CHANGED
@@ -1,477 +1,21 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
|
-
// -*- js -*-
|
3
|
-
/* eslint-env node */
|
4
2
|
|
5
3
|
"use strict";
|
6
4
|
|
7
|
-
require("../tools/exit.
|
5
|
+
require("../tools/exit.cjs");
|
8
6
|
|
9
|
-
var fs = require("fs");
|
10
|
-
var info = require("../package.json");
|
11
|
-
var path = require("path");
|
12
|
-
var program = require("commander");
|
13
|
-
|
14
|
-
var Terser = require("..");
|
15
7
|
try {
|
16
8
|
require("source-map-support").install();
|
17
9
|
} catch (err) {}
|
18
10
|
|
19
|
-
const
|
20
|
-
|
21
|
-
|
22
|
-
compress: false,
|
23
|
-
mangle: false
|
24
|
-
};
|
25
|
-
program.version(info.name + " " + info.version);
|
26
|
-
program.parseArgv = program.parse;
|
27
|
-
program.parse = undefined;
|
28
|
-
if (process.argv.includes("ast")) program.helpInformation = describe_ast;
|
29
|
-
else 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
|
-
};
|
39
|
-
program.option("-p, --parse <options>", "Specify parser options.", parse_js());
|
40
|
-
program.option("-c, --compress [options]", "Enable compressor/specify compressor options.", parse_js());
|
41
|
-
program.option("-m, --mangle [options]", "Mangle names/specify mangler options.", parse_js());
|
42
|
-
program.option("--mangle-props [options]", "Mangle properties/specify mangler options.", parse_js());
|
43
|
-
program.option("-b, --beautify [options]", "Beautify output/specify output options.", parse_js());
|
44
|
-
program.option("-o, --output <file>", "Output file (default STDOUT).");
|
45
|
-
program.option("--comments [filter]", "Preserve copyright comments in the output.");
|
46
|
-
program.option("--config-file <file>", "Read minify() options from JSON file.");
|
47
|
-
program.option("-d, --define <expr>[=value]", "Global definitions.", parse_js("define"));
|
48
|
-
program.option("--ecma <version>", "Specify ECMAScript release: 5, 2015, 2016 or 2017...");
|
49
|
-
program.option("-e, --enclose [arg[,...][:value[,...]]]", "Embed output in a big function with configurable arguments and values.");
|
50
|
-
program.option("--ie8", "Support non-standard Internet Explorer 8.");
|
51
|
-
program.option("--keep-classnames", "Do not mangle/drop class names.");
|
52
|
-
program.option("--keep-fnames", "Do not mangle/drop function names. Useful for code relying on Function.prototype.name.");
|
53
|
-
program.option("--module", "Input is an ES6 module");
|
54
|
-
program.option("--name-cache <file>", "File to hold mangled name mappings.");
|
55
|
-
program.option("--rename", "Force symbol expansion.");
|
56
|
-
program.option("--no-rename", "Disable symbol expansion.");
|
57
|
-
program.option("--safari10", "Support non-standard Safari 10.");
|
58
|
-
program.option("--source-map [options]", "Enable source map/specify source map options.", parse_js());
|
59
|
-
program.option("--timings", "Display operations run time on STDERR.");
|
60
|
-
program.option("--toplevel", "Compress and/or mangle variables in toplevel scope.");
|
61
|
-
program.option("--verbose", "Print diagnostic messages.");
|
62
|
-
program.option("--warn", "Print warning messages.");
|
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
|
-
"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
|
-
});
|
86
|
-
if ("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
|
-
}
|
94
|
-
if (program.beautify) {
|
95
|
-
options.output = typeof program.beautify == "object" ? program.beautify : {};
|
96
|
-
if (!("beautify" in options.output)) {
|
97
|
-
options.output.beautify = true;
|
98
|
-
}
|
99
|
-
}
|
100
|
-
if (program.comments) {
|
101
|
-
if (typeof options.output != "object") options.output = {};
|
102
|
-
options.output.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.output = {
|
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
|
-
var convert_path = function(name) {
|
149
|
-
return name;
|
150
|
-
};
|
151
|
-
if (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
|
-
}
|
160
|
-
if (program.verbose) {
|
161
|
-
options.warnings = "verbose";
|
162
|
-
} else if (program.warn) {
|
163
|
-
options.warnings = true;
|
164
|
-
}
|
165
|
-
|
166
|
-
let filesList;
|
167
|
-
if (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
|
-
|
175
|
-
if (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
|
-
|
192
|
-
function convert_ast(fn) {
|
193
|
-
return Terser.AST_Node.from_mozilla_ast(Object.keys(files).reduce(fn, null));
|
194
|
-
}
|
195
|
-
|
196
|
-
function 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
|
-
if (prop === "block_scope") return;
|
285
|
-
result[prop] = value[prop];
|
286
|
-
});
|
287
|
-
return result;
|
288
|
-
}
|
289
|
-
return value;
|
290
|
-
}, 2));
|
291
|
-
} else if (program.output == "spidermonkey") {
|
292
|
-
print(JSON.stringify(Terser.minify(result.code, {
|
293
|
-
compress: false,
|
294
|
-
mangle: false,
|
295
|
-
output: {
|
296
|
-
ast: true,
|
297
|
-
code: false
|
298
|
-
}
|
299
|
-
}).ast.to_mozilla_ast(), null, 2));
|
300
|
-
} else if (program.output) {
|
301
|
-
fs.writeFileSync(program.output, result.code);
|
302
|
-
if (options.sourceMap.url !== "inline" && result.map) {
|
303
|
-
fs.writeFileSync(program.output + ".map", result.map);
|
304
|
-
}
|
305
|
-
} else {
|
306
|
-
print(result.code);
|
307
|
-
}
|
308
|
-
if (program.nameCache) {
|
309
|
-
fs.writeFileSync(program.nameCache, JSON.stringify(options.nameCache));
|
310
|
-
}
|
311
|
-
if (result.timings) for (var phase in result.timings) {
|
312
|
-
print_error("- " + phase + ": " + result.timings[phase].toFixed(3) + "s");
|
313
|
-
}
|
314
|
-
}
|
315
|
-
|
316
|
-
function fatal(message) {
|
317
|
-
if (message instanceof Error) message = message.stack.replace(/^\S*?Error:/, "ERROR:");
|
318
|
-
print_error(message);
|
319
|
-
process.exit(1);
|
320
|
-
}
|
321
|
-
|
322
|
-
// A file glob function that only supports "*" and "?" wildcards in the basename.
|
323
|
-
// Example: "foo/bar/*baz??.*.js"
|
324
|
-
// Argument `glob` may be a string or an array of strings.
|
325
|
-
// Returns an array of strings. Garbage in, garbage out.
|
326
|
-
function simple_glob(glob) {
|
327
|
-
if (Array.isArray(glob)) {
|
328
|
-
return [].concat.apply([], glob.map(simple_glob));
|
329
|
-
}
|
330
|
-
if (glob && glob.match(/[*?]/)) {
|
331
|
-
var dir = path.dirname(glob);
|
332
|
-
try {
|
333
|
-
var entries = fs.readdirSync(dir);
|
334
|
-
} catch (ex) {}
|
335
|
-
if (entries) {
|
336
|
-
var pattern = "^" + path.basename(glob)
|
337
|
-
.replace(/[.+^$[\]\\(){}]/g, "\\$&")
|
338
|
-
.replace(/\*/g, "[^/\\\\]*")
|
339
|
-
.replace(/\?/g, "[^/\\\\]") + "$";
|
340
|
-
var mod = process.platform === "win32" ? "i" : "";
|
341
|
-
var rx = new RegExp(pattern, mod);
|
342
|
-
var results = entries.filter(function(name) {
|
343
|
-
return rx.test(name);
|
344
|
-
}).map(function(name) {
|
345
|
-
return path.join(dir, name);
|
346
|
-
});
|
347
|
-
if (results.length) return results;
|
348
|
-
}
|
349
|
-
}
|
350
|
-
return [ glob ];
|
351
|
-
}
|
11
|
+
const fs = require("fs");
|
12
|
+
const path = require("path");
|
13
|
+
const program = require("commander");
|
352
14
|
|
353
|
-
|
354
|
-
|
355
|
-
return fs.readFileSync(path, "utf8");
|
356
|
-
} catch (ex) {
|
357
|
-
if ((ex.code == "ENOENT" || ex.code == "ENAMETOOLONG") && default_value != null) return default_value;
|
358
|
-
fatal(ex);
|
359
|
-
}
|
360
|
-
}
|
15
|
+
const packageJson = require("../package.json");
|
16
|
+
const { _run_cli: run_cli } = require("..");
|
361
17
|
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
Terser.parse(value, {
|
367
|
-
expression: true
|
368
|
-
}).walk(new Terser.TreeWalker(function(node) {
|
369
|
-
if (node instanceof Terser.AST_Assign) {
|
370
|
-
var name = node.left.print_to_string();
|
371
|
-
var value = node.right;
|
372
|
-
if (flag) {
|
373
|
-
options[name] = value;
|
374
|
-
} else if (value instanceof Terser.AST_Array) {
|
375
|
-
options[name] = value.elements.map(to_string);
|
376
|
-
} else if (value instanceof Terser.AST_RegExp) {
|
377
|
-
value = value.value;
|
378
|
-
options[name] = new RegExp(value.source, value.flags);
|
379
|
-
} else {
|
380
|
-
options[name] = to_string(value);
|
381
|
-
}
|
382
|
-
return true;
|
383
|
-
}
|
384
|
-
if (node instanceof Terser.AST_Symbol || node instanceof Terser.AST_PropAccess) {
|
385
|
-
var name = node.print_to_string();
|
386
|
-
options[name] = true;
|
387
|
-
return true;
|
388
|
-
}
|
389
|
-
if (!(node instanceof Terser.AST_Sequence)) throw node;
|
390
|
-
|
391
|
-
function to_string(value) {
|
392
|
-
return value instanceof Terser.AST_Constant ? value.getValue() : value.print_to_string({
|
393
|
-
quote_keys: true
|
394
|
-
});
|
395
|
-
}
|
396
|
-
}));
|
397
|
-
} catch(ex) {
|
398
|
-
if (flag) {
|
399
|
-
fatal("Error parsing arguments for '" + flag + "': " + value);
|
400
|
-
} else {
|
401
|
-
options[value] = null;
|
402
|
-
}
|
403
|
-
}
|
404
|
-
return options;
|
405
|
-
};
|
406
|
-
}
|
407
|
-
|
408
|
-
function symdef(def) {
|
409
|
-
var ret = (1e6 + def.id) + " " + def.name;
|
410
|
-
if (def.mangled_name) ret += " " + def.mangled_name;
|
411
|
-
return ret;
|
412
|
-
}
|
413
|
-
|
414
|
-
function collect_from_map(map, callback) {
|
415
|
-
var result = [];
|
416
|
-
map.forEach(function (def) {
|
417
|
-
result.push(callback(def));
|
418
|
-
});
|
419
|
-
return result;
|
420
|
-
}
|
421
|
-
|
422
|
-
function format_object(obj) {
|
423
|
-
var lines = [];
|
424
|
-
var padding = "";
|
425
|
-
Object.keys(obj).map(function(name) {
|
426
|
-
if (padding.length < name.length) padding = Array(name.length + 1).join(" ");
|
427
|
-
return [ name, JSON.stringify(obj[name]) ];
|
428
|
-
}).forEach(function(tokens) {
|
429
|
-
lines.push(" " + tokens[0] + padding.slice(tokens[0].length - 2) + tokens[1]);
|
430
|
-
});
|
431
|
-
return lines.join("\n");
|
432
|
-
}
|
433
|
-
|
434
|
-
function print_error(msg) {
|
435
|
-
process.stderr.write(msg);
|
436
|
-
process.stderr.write("\n");
|
437
|
-
}
|
438
|
-
|
439
|
-
function print(txt) {
|
440
|
-
process.stdout.write(txt);
|
441
|
-
process.stdout.write("\n");
|
442
|
-
}
|
443
|
-
|
444
|
-
function describe_ast() {
|
445
|
-
var out = Terser.OutputStream({ beautify: true });
|
446
|
-
function doitem(ctor) {
|
447
|
-
out.print("AST_" + ctor.TYPE);
|
448
|
-
var props = ctor.SELF_PROPS.filter(function(prop) {
|
449
|
-
return !/^\$/.test(prop);
|
450
|
-
});
|
451
|
-
if (props.length > 0) {
|
452
|
-
out.space();
|
453
|
-
out.with_parens(function() {
|
454
|
-
props.forEach(function(prop, i) {
|
455
|
-
if (i) out.space();
|
456
|
-
out.print(prop);
|
457
|
-
});
|
458
|
-
});
|
459
|
-
}
|
460
|
-
if (ctor.documentation) {
|
461
|
-
out.space();
|
462
|
-
out.print_string(ctor.documentation);
|
463
|
-
}
|
464
|
-
if (ctor.SUBCLASSES.length > 0) {
|
465
|
-
out.space();
|
466
|
-
out.with_block(function() {
|
467
|
-
ctor.SUBCLASSES.forEach(function(ctor, i) {
|
468
|
-
out.indent();
|
469
|
-
doitem(ctor);
|
470
|
-
out.newline();
|
471
|
-
});
|
472
|
-
});
|
473
|
-
}
|
474
|
-
}
|
475
|
-
doitem(Terser.AST_Node);
|
476
|
-
return out + "\n";
|
477
|
-
}
|
18
|
+
run_cli({ program, packageJson, fs, path }).catch((error) => {
|
19
|
+
console.error(error);
|
20
|
+
process.exitCode = 1;
|
21
|
+
});
|