terser 4.2.0 → 4.3.2

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.

Potentially problematic release.


This version of terser might be problematic. Click here for more details.

package/bin/uglifyjs CHANGED
@@ -4,470 +4,7 @@
4
4
 
5
5
  "use strict";
6
6
 
7
- require("../tools/exit.js");
7
+ process.stderr.write( "DEPRECATION WARNING: uglifyjs binary will soon be discontinued!\n");
8
+ process.stderr.write("Please use \"terser\" instead.\n\n");
8
9
 
9
- var fs = require("fs");
10
- var info = require("../package.json");
11
- var path = require("path");
12
- var program = require("commander");
13
-
14
- var UglifyJS = require("..");
15
- try {
16
- require("source-map-support").install();
17
- } catch (err) {}
18
-
19
- var skip_keys = [ "cname", "inlined", "parent_scope", "scope", "uses_eval", "uses_with" ];
20
- var files = {};
21
- var options = {
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 = UglifyJS.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, 6, 7 or 8.");
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
- options.ecma = program.ecma | 0;
89
- }
90
- if (program.beautify) {
91
- options.output = typeof program.beautify == "object" ? program.beautify : {};
92
- if (!("beautify" in options.output)) {
93
- options.output.beautify = true;
94
- }
95
- }
96
- if (program.comments) {
97
- if (typeof options.output != "object") options.output = {};
98
- options.output.comments = typeof program.comments == "string" ? program.comments : "some";
99
- }
100
- if (program.define) {
101
- if (typeof options.compress != "object") options.compress = {};
102
- if (typeof options.compress.global_defs != "object") options.compress.global_defs = {};
103
- for (var expr in program.define) {
104
- options.compress.global_defs[expr] = program.define[expr];
105
- }
106
- }
107
- if (program.keepClassnames) {
108
- options.keep_classnames = true;
109
- }
110
- if (program.keepFnames) {
111
- options.keep_fnames = true;
112
- }
113
- if (program.mangleProps) {
114
- if (program.mangleProps.domprops) {
115
- delete program.mangleProps.domprops;
116
- } else {
117
- if (typeof program.mangleProps != "object") program.mangleProps = {};
118
- if (!Array.isArray(program.mangleProps.reserved)) program.mangleProps.reserved = [];
119
- }
120
- if (typeof options.mangle != "object") options.mangle = {};
121
- options.mangle.properties = program.mangleProps;
122
- }
123
- if (program.nameCache) {
124
- options.nameCache = JSON.parse(read_file(program.nameCache, "{}"));
125
- }
126
- if (program.output == "ast") {
127
- options.output = {
128
- ast: true,
129
- code: false
130
- };
131
- }
132
- if (program.parse) {
133
- if (!program.parse.acorn && !program.parse.spidermonkey) {
134
- options.parse = program.parse;
135
- } else if (program.sourceMap && program.sourceMap.content == "inline") {
136
- fatal("ERROR: inline source map only works with built-in parser");
137
- }
138
- }
139
- if (~program.rawArgs.indexOf("--rename")) {
140
- options.rename = true;
141
- } else if (!program.rename) {
142
- options.rename = false;
143
- }
144
- var convert_path = function(name) {
145
- return name;
146
- };
147
- if (typeof program.sourceMap == "object" && "base" in program.sourceMap) {
148
- convert_path = function() {
149
- var base = program.sourceMap.base;
150
- delete options.sourceMap.base;
151
- return function(name) {
152
- return path.relative(base, name);
153
- };
154
- }();
155
- }
156
- if (program.verbose) {
157
- options.warnings = "verbose";
158
- } else if (program.warn) {
159
- options.warnings = true;
160
- }
161
-
162
- let filesList;
163
- if (options.files && options.files.length) {
164
- filesList = options.files;
165
-
166
- delete options.files;
167
- } else if (program.args.length) {
168
- filesList = program.args;
169
- }
170
-
171
- if (filesList) {
172
- simple_glob(filesList).forEach(function(name) {
173
- files[convert_path(name)] = read_file(name);
174
- });
175
- run();
176
- } else {
177
- var chunks = [];
178
- process.stdin.setEncoding("utf8");
179
- process.stdin.on("data", function(chunk) {
180
- chunks.push(chunk);
181
- }).on("end", function() {
182
- files = [ chunks.join("") ];
183
- run();
184
- });
185
- process.stdin.resume();
186
- }
187
-
188
- function convert_ast(fn) {
189
- return UglifyJS.AST_Node.from_mozilla_ast(Object.keys(files).reduce(fn, null));
190
- }
191
-
192
- function run() {
193
- UglifyJS.AST_Node.warn_function = function(msg) {
194
- print_error("WARN: " + msg);
195
- };
196
- var content = program.sourceMap && program.sourceMap.content;
197
- if (content && content !== "inline") {
198
- options.sourceMap.content = read_file(content, content);
199
- }
200
- if (program.timings) options.timings = true;
201
- try {
202
- if (program.parse) {
203
- if (program.parse.acorn) {
204
- files = convert_ast(function(toplevel, name) {
205
- return require("acorn").parse(files[name], {
206
- ecmaVersion: 2018,
207
- locations: true,
208
- program: toplevel,
209
- sourceFile: name,
210
- sourceType: options.module || program.parse.module ? "module" : "script"
211
- });
212
- });
213
- } else if (program.parse.spidermonkey) {
214
- files = convert_ast(function(toplevel, name) {
215
- var obj = JSON.parse(files[name]);
216
- if (!toplevel) return obj;
217
- toplevel.body = toplevel.body.concat(obj.body);
218
- return toplevel;
219
- });
220
- }
221
- }
222
- } catch (ex) {
223
- fatal(ex);
224
- }
225
- var result = UglifyJS.minify(files, options);
226
- if (result.error) {
227
- var ex = result.error;
228
- if (ex.name == "SyntaxError") {
229
- print_error("Parse error at " + ex.filename + ":" + ex.line + "," + ex.col);
230
- var col = ex.col;
231
- var lines = files[ex.filename].split(/\r?\n/);
232
- var line = lines[ex.line - 1];
233
- if (!line && !col) {
234
- line = lines[ex.line - 2];
235
- col = line.length;
236
- }
237
- if (line) {
238
- var limit = 70;
239
- if (col > limit) {
240
- line = line.slice(col - limit);
241
- col = limit;
242
- }
243
- print_error(line.slice(0, 80));
244
- print_error(line.slice(0, col).replace(/\S/g, " ") + "^");
245
- }
246
- }
247
- if (ex.defs) {
248
- print_error("Supported options:");
249
- print_error(format_object(ex.defs));
250
- }
251
- fatal(ex);
252
- } else if (program.output == "ast") {
253
- if (!options.compress && !options.mangle) {
254
- result.ast.figure_out_scope({});
255
- }
256
- print(JSON.stringify(result.ast, function(key, value) {
257
- if (value) switch (key) {
258
- case "thedef":
259
- return symdef(value);
260
- case "enclosed":
261
- return value.length ? value.map(symdef) : undefined;
262
- case "variables":
263
- case "functions":
264
- case "globals":
265
- return value.size ? collect_from_map(value, symdef) : undefined;
266
- }
267
- if (skip_key(key)) return;
268
- if (value instanceof UglifyJS.AST_Token) return;
269
- if (value instanceof Map) return;
270
- if (value instanceof UglifyJS.AST_Node) {
271
- var result = {
272
- _class: "AST_" + value.TYPE
273
- };
274
- if (value.block_scope) {
275
- result.variables = value.block_scope.variables;
276
- result.functions = value.block_scope.functions;
277
- result.enclosed = value.block_scope.enclosed;
278
- }
279
- value.CTOR.PROPS.forEach(function(prop) {
280
- result[prop] = value[prop];
281
- });
282
- return result;
283
- }
284
- return value;
285
- }, 2));
286
- } else if (program.output == "spidermonkey") {
287
- print(JSON.stringify(UglifyJS.minify(result.code, {
288
- compress: false,
289
- mangle: false,
290
- output: {
291
- ast: true,
292
- code: false
293
- }
294
- }).ast.to_mozilla_ast(), null, 2));
295
- } else if (program.output) {
296
- fs.writeFileSync(program.output, result.code);
297
- if (options.sourceMap.url !== "inline" && result.map) {
298
- fs.writeFileSync(program.output + ".map", result.map);
299
- }
300
- } else {
301
- print(result.code);
302
- }
303
- if (program.nameCache) {
304
- fs.writeFileSync(program.nameCache, JSON.stringify(options.nameCache));
305
- }
306
- if (result.timings) for (var phase in result.timings) {
307
- print_error("- " + phase + ": " + result.timings[phase].toFixed(3) + "s");
308
- }
309
- }
310
-
311
- function fatal(message) {
312
- if (message instanceof Error) message = message.stack.replace(/^\S*?Error:/, "ERROR:");
313
- print_error(message);
314
- process.exit(1);
315
- }
316
-
317
- // A file glob function that only supports "*" and "?" wildcards in the basename.
318
- // Example: "foo/bar/*baz??.*.js"
319
- // Argument `glob` may be a string or an array of strings.
320
- // Returns an array of strings. Garbage in, garbage out.
321
- function simple_glob(glob) {
322
- if (Array.isArray(glob)) {
323
- return [].concat.apply([], glob.map(simple_glob));
324
- }
325
- if (glob && glob.match(/[*?]/)) {
326
- var dir = path.dirname(glob);
327
- try {
328
- var entries = fs.readdirSync(dir);
329
- } catch (ex) {}
330
- if (entries) {
331
- var pattern = "^" + path.basename(glob)
332
- .replace(/[.+^$[\]\\(){}]/g, "\\$&")
333
- .replace(/\*/g, "[^/\\\\]*")
334
- .replace(/\?/g, "[^/\\\\]") + "$";
335
- var mod = process.platform === "win32" ? "i" : "";
336
- var rx = new RegExp(pattern, mod);
337
- var results = entries.filter(function(name) {
338
- return rx.test(name);
339
- }).map(function(name) {
340
- return path.join(dir, name);
341
- });
342
- if (results.length) return results;
343
- }
344
- }
345
- return [ glob ];
346
- }
347
-
348
- function read_file(path, default_value) {
349
- try {
350
- return fs.readFileSync(path, "utf8");
351
- } catch (ex) {
352
- if ((ex.code == "ENOENT" || ex.code == "ENAMETOOLONG") && default_value != null) return default_value;
353
- fatal(ex);
354
- }
355
- }
356
-
357
- function parse_js(flag) {
358
- return function(value, options) {
359
- options = options || {};
360
- try {
361
- UglifyJS.parse(value, {
362
- expression: true
363
- }).walk(new UglifyJS.TreeWalker(function(node) {
364
- if (node instanceof UglifyJS.AST_Assign) {
365
- var name = node.left.print_to_string();
366
- var value = node.right;
367
- if (flag) {
368
- options[name] = value;
369
- } else if (value instanceof UglifyJS.AST_Array) {
370
- options[name] = value.elements.map(to_string);
371
- } else {
372
- options[name] = to_string(value);
373
- }
374
- return true;
375
- }
376
- if (node instanceof UglifyJS.AST_Symbol || node instanceof UglifyJS.AST_PropAccess) {
377
- var name = node.print_to_string();
378
- options[name] = true;
379
- return true;
380
- }
381
- if (!(node instanceof UglifyJS.AST_Sequence)) throw node;
382
-
383
- function to_string(value) {
384
- return value instanceof UglifyJS.AST_Constant ? value.getValue() : value.print_to_string({
385
- quote_keys: true
386
- });
387
- }
388
- }));
389
- } catch(ex) {
390
- if (flag) {
391
- fatal("Error parsing arguments for '" + flag + "': " + value);
392
- } else {
393
- options[value] = null;
394
- }
395
- }
396
- return options;
397
- };
398
- }
399
-
400
- function skip_key(key) {
401
- return skip_keys.includes(key);
402
- }
403
-
404
- function symdef(def) {
405
- var ret = (1e6 + def.id) + " " + def.name;
406
- if (def.mangled_name) ret += " " + def.mangled_name;
407
- return ret;
408
- }
409
-
410
- function collect_from_map(map, callback) {
411
- var result = [];
412
- map.forEach(function (def) {
413
- result.push(callback(def));
414
- });
415
- return result;
416
- }
417
-
418
- function format_object(obj) {
419
- var lines = [];
420
- var padding = "";
421
- Object.keys(obj).map(function(name) {
422
- if (padding.length < name.length) padding = Array(name.length + 1).join(" ");
423
- return [ name, JSON.stringify(obj[name]) ];
424
- }).forEach(function(tokens) {
425
- lines.push(" " + tokens[0] + padding.slice(tokens[0].length - 2) + tokens[1]);
426
- });
427
- return lines.join("\n");
428
- }
429
-
430
- function print_error(msg) {
431
- process.stderr.write(msg);
432
- process.stderr.write("\n");
433
- }
434
-
435
- function print(txt) {
436
- process.stdout.write(txt);
437
- process.stdout.write("\n");
438
- }
439
-
440
- function describe_ast() {
441
- var out = UglifyJS.OutputStream({ beautify: true });
442
- function doitem(ctor) {
443
- out.print("AST_" + ctor.TYPE);
444
- var props = ctor.SELF_PROPS.filter(function(prop) {
445
- return !/^\$/.test(prop);
446
- });
447
- if (props.length > 0) {
448
- out.space();
449
- out.with_parens(function() {
450
- props.forEach(function(prop, i) {
451
- if (i) out.space();
452
- out.print(prop);
453
- });
454
- });
455
- }
456
- if (ctor.documentation) {
457
- out.space();
458
- out.print_string(ctor.documentation);
459
- }
460
- if (ctor.SUBCLASSES.length > 0) {
461
- out.space();
462
- out.with_block(function() {
463
- ctor.SUBCLASSES.forEach(function(ctor, i) {
464
- out.indent();
465
- doitem(ctor);
466
- out.newline();
467
- });
468
- });
469
- }
470
- }
471
- doitem(UglifyJS.AST_Node);
472
- return out + "\n";
473
- }
10
+ require("./terser");