terser 4.1.4 → 4.3.1
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/CHANGELOG.md +32 -1
- package/README.md +29 -16
- package/bin/terser +473 -0
- package/bin/uglifyjs +3 -466
- package/dist/bundle.min.js +1 -1
- package/dist/bundle.min.js.map +1 -1
- package/package.json +9 -11
- package/tools/domprops.js +8 -0
- package/tools/terser.d.ts +2 -2
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,36 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## v4.3.1
|
4
|
+
|
5
|
+
- Fixed an issue from 4.3.0 where any block scope within a for loop erroneously had its parent set to the function scopee
|
6
|
+
- Fixed an issue where compressing IIFEs with argument expansions would result in some parameters becoming undefined
|
7
|
+
- addEventListener options argument's properties are now part of the DOM properties list.
|
8
|
+
|
9
|
+
## v4.3.0
|
10
|
+
|
11
|
+
- Do not drop computed object keys with side effects
|
12
|
+
- Functions passed to other functions in calls are now wrapped in parentheses by default, which speeds up loading most modules
|
13
|
+
- Objects with computed properties are now less likely to be hoisted
|
14
|
+
- Speed and memory efficiency optimizations
|
15
|
+
- Fixed scoping issues with `try` and `switch`
|
16
|
+
|
17
|
+
## v4.2.1
|
18
|
+
|
19
|
+
- Minor refactors
|
20
|
+
- Fixed a bug similar to #369 in collapse_vars
|
21
|
+
- Functions can no longer be inlined into a place where they're going to be compared with themselves.
|
22
|
+
- reduce_funcs option is now legacy, as using reduce_vars without reduce_funcs caused some weird corner cases. As a result, it is now implied in reduce_vars and can't be turned off without turning off reduce_vars.
|
23
|
+
- Bug which would cause a random stack overflow has now been fixed.
|
24
|
+
|
25
|
+
## v4.2.0
|
26
|
+
|
27
|
+
- When the source map URL is `inline`, don't write it to a file.
|
28
|
+
- Fixed output parens when a lambda literal is the tag on a tagged template string.
|
29
|
+
- The `mangle.properties.undeclared` option was added. This enables the property mangler to mangle properties of variables which can be found in the name cache, but whose properties are not known to this Terser run.
|
30
|
+
- The v8 bug where the toString and source representations of regexes like `RegExp("\\\n")` includes an actual newline is now fixed.
|
31
|
+
- Now we're guaranteed to not have duplicate comments in the output
|
32
|
+
- Domprops updates
|
33
|
+
|
3
34
|
## v4.1.4
|
4
35
|
|
5
36
|
- Fixed a crash when inlining a function into somewhere else when it has interdependent, non-removable variables.
|
@@ -27,7 +58,7 @@
|
|
27
58
|
|
28
59
|
## v4.0.2
|
29
60
|
|
30
|
-
(Hotfix release. Reverts unmapped segments PR [#342](https://github.com/terser
|
61
|
+
(Hotfix release. Reverts unmapped segments PR [#342](https://github.com/terser/terser/pull/342), which will be put back on Terser when the upstream issue is resolved)
|
31
62
|
|
32
63
|
## v4.0.1
|
33
64
|
|
package/README.md
CHANGED
@@ -1,22 +1,27 @@
|
|
1
|
-
terser
|
2
|
-
======
|
1
|
+
<h1><img src="https://terser.org/img/terser-banner-logo.png" alt="Terser" width="400"></h1>
|
3
2
|
|
4
|
-
![
|
3
|
+
[![NPM Version][npm-image]][npm-url]
|
4
|
+
[![NPM Downloads][downloads-image]][downloads-url]
|
5
|
+
[![Linux Build][travis-image]][travis-url]
|
5
6
|
|
6
7
|
A JavaScript parser and mangler/compressor toolkit for ES6+.
|
7
8
|
|
8
|
-
*note*: You can support this project on patreon: <a target="_blank" rel="nofollow" href="https://www.patreon.com/fabiosantoscode"><img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" alt="patron" width="100px" height="auto"></a>. Check out [PATRONS.md](https://github.com/terser
|
9
|
+
*note*: You can support this project on patreon: <a target="_blank" rel="nofollow" href="https://www.patreon.com/fabiosantoscode"><img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" alt="patron" width="100px" height="auto"></a>. Check out [PATRONS.md](https://github.com/terser/terser/blob/master/PATRONS.md) for our first-tier patrons.
|
9
10
|
|
10
11
|
Terser recommends you use RollupJS to bundle your modules, as that produces smaller code overall.
|
11
12
|
|
12
13
|
*Beautification* has been undocumented and is *being removed* from terser, we recommend you use [prettier](https://npmjs.com/package/prettier).
|
13
14
|
|
14
|
-
[
|
15
|
+
Find the changelog in [CHANGELOG.md](https://github.com/terser/terser/blob/master/CHANGELOG.md)
|
15
16
|
|
16
|
-
Find the changelog in [CHANGELOG.md](https://github.com/terser-js/terser/blob/master/CHANGELOG.md)
|
17
17
|
|
18
|
-
A JavaScript parser, mangler/compressor and beautifier toolkit for ES6+.
|
19
18
|
|
19
|
+
[npm-image]: https://img.shields.io/npm/v/terser.svg
|
20
|
+
[npm-url]: https://npmjs.org/package/terser
|
21
|
+
[downloads-image]: https://img.shields.io/npm/dm/terser.svg
|
22
|
+
[downloads-url]: https://npmjs.org/package/terser
|
23
|
+
[travis-image]: https://img.shields.io/travis/terser/terser/master.svg
|
24
|
+
[travis-url]: https://travis-ci.org/terser/terser
|
20
25
|
|
21
26
|
Why choose terser?
|
22
27
|
------------------
|
@@ -39,7 +44,7 @@ From NPM for use as a command line app:
|
|
39
44
|
From NPM for programmatic use:
|
40
45
|
|
41
46
|
npm install terser
|
42
|
-
|
47
|
+
|
43
48
|
# Command line usage
|
44
49
|
|
45
50
|
terser [input files] [options]
|
@@ -106,6 +111,7 @@ a double dash to prevent input files being used as option arguments:
|
|
106
111
|
`wrap_iife` Wrap IIFEs in parenthesis. Note: you may
|
107
112
|
want to disable `negate_iife` under
|
108
113
|
compressor options.
|
114
|
+
`wrap_func_args` Wrap function arguments in parenthesis.
|
109
115
|
-o, --output <file> Output file path (default STDOUT). Specify `ast` or
|
110
116
|
`spidermonkey` to write Terser or SpiderMonkey AST
|
111
117
|
as JSON to STDOUT respectively.
|
@@ -255,7 +261,7 @@ way to use this is to use the `regex` option like so:
|
|
255
261
|
terser example.js -c -m --mangle-props regex=/_$/
|
256
262
|
```
|
257
263
|
|
258
|
-
This will mangle all properties that start with an
|
264
|
+
This will mangle all properties that start with an
|
259
265
|
underscore. So you can use it to mangle internal methods.
|
260
266
|
|
261
267
|
By default, it will mangle all properties in the
|
@@ -525,7 +531,7 @@ if (result.error) throw result.error;
|
|
525
531
|
## Minify options
|
526
532
|
|
527
533
|
- `ecma` (default `undefined`) - pass `5`, `6`, `7` or `8` to override `parse`,
|
528
|
-
`compress` and `output` options.
|
534
|
+
`compress` and `output`'s `ecma` options.
|
529
535
|
|
530
536
|
- `warnings` (default `false`) — pass `true` to return compressor warnings
|
531
537
|
in `result.warnings`. Use the value `"verbose"` for more detailed warnings.
|
@@ -660,6 +666,8 @@ var result = Terser.minify({"compiled.js": "compiled code"}, {
|
|
660
666
|
|
661
667
|
If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.url`.
|
662
668
|
|
669
|
+
If you happen to need the source map as a raw object, set `sourceMap.asObject` to `true`.
|
670
|
+
|
663
671
|
## Parse options
|
664
672
|
|
665
673
|
- `bare_returns` (default `false`) -- support top level `return` statements
|
@@ -797,11 +805,7 @@ If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.u
|
|
797
805
|
Specify `"strict"` to treat `foo.bar` as side-effect-free only when
|
798
806
|
`foo` is certain to not throw, i.e. not `null` or `undefined`.
|
799
807
|
|
800
|
-
- `reduce_funcs` (
|
801
|
-
inlined as function expressions when permissible allowing further
|
802
|
-
optimization. Enabled by default. Option depends on `reduce_vars`
|
803
|
-
being enabled. Some code runs faster in the Chrome V8 engine if this
|
804
|
-
option is disabled. Does not negatively impact other major browsers.
|
808
|
+
- `reduce_funcs` (legacy option, safely ignored for backwards compatibility).
|
805
809
|
|
806
810
|
- `reduce_vars` (default: `true`) -- Improve optimization on variables assigned with and
|
807
811
|
used as constant values.
|
@@ -948,6 +952,11 @@ Terser.minify(code, { mangle: { toplevel: true } }).code;
|
|
948
952
|
- `reserved` (default: `[]`) — Do not mangle property names listed in the
|
949
953
|
`reserved` array.
|
950
954
|
|
955
|
+
- `undeclared` (default: `false`) - Mangle those names when they are accessed
|
956
|
+
as properties of known top level variables but their declarations are never
|
957
|
+
found in input code. May be useful when only minifying parts of a project.
|
958
|
+
See [#397](https://github.com/terser/terser/issues/397) for more details.
|
959
|
+
|
951
960
|
## Output options
|
952
961
|
|
953
962
|
The code generator tries to output shortest code possible by default. In
|
@@ -1023,6 +1032,10 @@ can pass additional arguments that control the code output:
|
|
1023
1032
|
function expressions. See
|
1024
1033
|
[#640](https://github.com/mishoo/UglifyJS2/issues/640) for more details.
|
1025
1034
|
|
1035
|
+
- `wrap_func_args` (default `true`) -- pass `false` if you do not want to wrap
|
1036
|
+
function expressions that are passed as arguments, in parenthesis. See
|
1037
|
+
[OptimizeJS](https://github.com/nolanlawson/optimize-js) for more details.
|
1038
|
+
|
1026
1039
|
# Miscellaneous
|
1027
1040
|
|
1028
1041
|
### Keeping copyright notices or other comments
|
@@ -1304,7 +1317,7 @@ In the terser CLI we use [source-map-support](https://npmjs.com/source-map-suppo
|
|
1304
1317
|
|
1305
1318
|
# README.md Patrons:
|
1306
1319
|
|
1307
|
-
*note*: You can support this project on patreon: <a target="_blank" rel="nofollow" href="https://www.patreon.com/fabiosantoscode"><img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" alt="patron" width="100px" height="auto"></a>. Check out [PATRONS.md](https://github.com/terser
|
1320
|
+
*note*: You can support this project on patreon: <a target="_blank" rel="nofollow" href="https://www.patreon.com/fabiosantoscode"><img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" alt="patron" width="100px" height="auto"></a>. Check out [PATRONS.md](https://github.com/terser/terser/blob/master/PATRONS.md) for our first-tier patrons.
|
1308
1321
|
|
1309
1322
|
These are the second-tier patrons. Great thanks for your support!
|
1310
1323
|
|
package/bin/terser
ADDED
@@ -0,0 +1,473 @@
|
|
1
|
+
#!/usr/bin/env node
|
2
|
+
// -*- js -*-
|
3
|
+
/* eslint-env node */
|
4
|
+
|
5
|
+
"use strict";
|
6
|
+
|
7
|
+
require("../tools/exit.js");
|
8
|
+
|
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
|
+
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 = 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, 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 Terser.AST_Node.from_mozilla_ast(Object.keys(files).reduce(fn, null));
|
190
|
+
}
|
191
|
+
|
192
|
+
function run() {
|
193
|
+
Terser.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 = Terser.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 Terser.AST_Token) return;
|
269
|
+
if (value instanceof Map) return;
|
270
|
+
if (value instanceof Terser.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(Terser.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
|
+
Terser.parse(value, {
|
362
|
+
expression: true
|
363
|
+
}).walk(new Terser.TreeWalker(function(node) {
|
364
|
+
if (node instanceof Terser.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 Terser.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 Terser.AST_Symbol || node instanceof Terser.AST_PropAccess) {
|
377
|
+
var name = node.print_to_string();
|
378
|
+
options[name] = true;
|
379
|
+
return true;
|
380
|
+
}
|
381
|
+
if (!(node instanceof Terser.AST_Sequence)) throw node;
|
382
|
+
|
383
|
+
function to_string(value) {
|
384
|
+
return value instanceof Terser.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 = Terser.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(Terser.AST_Node);
|
472
|
+
return out + "\n";
|
473
|
+
}
|