terser 5.7.1 → 5.10.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 +42 -0
- package/README.md +23 -4
- package/bin/terser.mjs +7 -9
- package/dist/bundle.min.js +4847 -3745
- package/lib/ast.js +6 -6
- package/lib/compress/common.js +296 -0
- package/lib/compress/compressor-flags.js +63 -0
- package/lib/compress/drop-side-effect-free.js +350 -0
- package/lib/compress/evaluate.js +461 -0
- package/lib/compress/index.js +474 -3575
- package/lib/compress/inference.js +948 -0
- package/lib/compress/native-objects.js +184 -0
- package/lib/compress/reduce-vars.js +675 -0
- package/lib/compress/tighten-body.js +1461 -0
- package/lib/minify.js +3 -1
- package/lib/mozilla-ast.js +48 -6
- package/lib/output.js +87 -28
- package/lib/parse.js +28 -10
- package/lib/propmangle.js +57 -34
- package/lib/scope.js +46 -25
- package/lib/transform.js +2 -2
- package/package.json +17 -10
- package/tools/terser.d.ts +37 -0
package/lib/ast.js
CHANGED
@@ -41,8 +41,6 @@
|
|
41
41
|
|
42
42
|
***********************************************************************/
|
43
43
|
|
44
|
-
"use strict";
|
45
|
-
|
46
44
|
import {
|
47
45
|
HOP,
|
48
46
|
MAP,
|
@@ -891,12 +889,13 @@ var AST_NameMapping = DEFNODE("NameMapping", "foreign_name name", {
|
|
891
889
|
},
|
892
890
|
});
|
893
891
|
|
894
|
-
var AST_Import = DEFNODE("Import", "imported_name imported_names module_name", {
|
892
|
+
var AST_Import = DEFNODE("Import", "imported_name imported_names module_name assert_clause", {
|
895
893
|
$documentation: "An `import` statement",
|
896
894
|
$propdoc: {
|
897
895
|
imported_name: "[AST_SymbolImport] The name of the variable holding the module's default export.",
|
898
896
|
imported_names: "[AST_NameMapping*] The names of non-default imported variables",
|
899
897
|
module_name: "[AST_String] String literal describing where this module came from",
|
898
|
+
assert_clause: "[AST_Object?] The import assertion"
|
900
899
|
},
|
901
900
|
_walk: function(visitor) {
|
902
901
|
return visitor._visit(this, function() {
|
@@ -925,14 +924,15 @@ var AST_ImportMeta = DEFNODE("ImportMeta", null, {
|
|
925
924
|
$documentation: "A reference to import.meta",
|
926
925
|
});
|
927
926
|
|
928
|
-
var AST_Export = DEFNODE("Export", "exported_definition exported_value is_default exported_names module_name", {
|
927
|
+
var AST_Export = DEFNODE("Export", "exported_definition exported_value is_default exported_names module_name assert_clause", {
|
929
928
|
$documentation: "An `export` statement",
|
930
929
|
$propdoc: {
|
931
930
|
exported_definition: "[AST_Defun|AST_Definitions|AST_DefClass?] An exported definition",
|
932
931
|
exported_value: "[AST_Node?] An exported value",
|
933
932
|
exported_names: "[AST_NameMapping*?] List of exported names",
|
934
933
|
module_name: "[AST_String?] Name of the file to load exports from",
|
935
|
-
is_default: "[Boolean] Whether this is the default exported value of this module"
|
934
|
+
is_default: "[Boolean] Whether this is the default exported value of this module",
|
935
|
+
assert_clause: "[AST_Object?] The import assertion"
|
936
936
|
},
|
937
937
|
_walk: function (visitor) {
|
938
938
|
return visitor._visit(this, function () {
|
@@ -1332,7 +1332,7 @@ var AST_ClassProperty = DEFNODE("ClassProperty", "static quote", {
|
|
1332
1332
|
}
|
1333
1333
|
}, AST_ObjectProperty);
|
1334
1334
|
|
1335
|
-
var AST_ClassPrivateProperty = DEFNODE("
|
1335
|
+
var AST_ClassPrivateProperty = DEFNODE("ClassPrivateProperty", "", {
|
1336
1336
|
$documentation: "A class property for a private property",
|
1337
1337
|
}, AST_ClassProperty);
|
1338
1338
|
|
@@ -0,0 +1,296 @@
|
|
1
|
+
/***********************************************************************
|
2
|
+
|
3
|
+
A JavaScript tokenizer / parser / beautifier / compressor.
|
4
|
+
https://github.com/mishoo/UglifyJS2
|
5
|
+
|
6
|
+
-------------------------------- (C) ---------------------------------
|
7
|
+
|
8
|
+
Author: Mihai Bazon
|
9
|
+
<mihai.bazon@gmail.com>
|
10
|
+
http://mihai.bazon.net/blog
|
11
|
+
|
12
|
+
Distributed under the BSD license:
|
13
|
+
|
14
|
+
Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
|
15
|
+
|
16
|
+
Redistribution and use in source and binary forms, with or without
|
17
|
+
modification, are permitted provided that the following conditions
|
18
|
+
are met:
|
19
|
+
|
20
|
+
* Redistributions of source code must retain the above
|
21
|
+
copyright notice, this list of conditions and the following
|
22
|
+
disclaimer.
|
23
|
+
|
24
|
+
* Redistributions in binary form must reproduce the above
|
25
|
+
copyright notice, this list of conditions and the following
|
26
|
+
disclaimer in the documentation and/or other materials
|
27
|
+
provided with the distribution.
|
28
|
+
|
29
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
|
30
|
+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
31
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
32
|
+
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
|
33
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
34
|
+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
35
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
36
|
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
37
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
38
|
+
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
39
|
+
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
40
|
+
SUCH DAMAGE.
|
41
|
+
|
42
|
+
***********************************************************************/
|
43
|
+
|
44
|
+
import {
|
45
|
+
AST_Array,
|
46
|
+
AST_Arrow,
|
47
|
+
AST_BlockStatement,
|
48
|
+
AST_Call,
|
49
|
+
AST_Class,
|
50
|
+
AST_Const,
|
51
|
+
AST_Constant,
|
52
|
+
AST_DefClass,
|
53
|
+
AST_Defun,
|
54
|
+
AST_EmptyStatement,
|
55
|
+
AST_Export,
|
56
|
+
AST_False,
|
57
|
+
AST_Function,
|
58
|
+
AST_Import,
|
59
|
+
AST_Infinity,
|
60
|
+
AST_LabeledStatement,
|
61
|
+
AST_Lambda,
|
62
|
+
AST_Let,
|
63
|
+
AST_LoopControl,
|
64
|
+
AST_NaN,
|
65
|
+
AST_Node,
|
66
|
+
AST_Null,
|
67
|
+
AST_Number,
|
68
|
+
AST_Object,
|
69
|
+
AST_ObjectKeyVal,
|
70
|
+
AST_PropAccess,
|
71
|
+
AST_RegExp,
|
72
|
+
AST_Scope,
|
73
|
+
AST_Sequence,
|
74
|
+
AST_SimpleStatement,
|
75
|
+
AST_Statement,
|
76
|
+
AST_String,
|
77
|
+
AST_SymbolRef,
|
78
|
+
AST_True,
|
79
|
+
AST_UnaryPrefix,
|
80
|
+
AST_Undefined,
|
81
|
+
|
82
|
+
TreeWalker,
|
83
|
+
} from "../ast.js";
|
84
|
+
import { make_node, regexp_source_fix, string_template, makePredicate } from "../utils/index.js";
|
85
|
+
import { first_in_statement } from "../utils/first_in_statement.js";
|
86
|
+
|
87
|
+
export function merge_sequence(array, node) {
|
88
|
+
if (node instanceof AST_Sequence) {
|
89
|
+
array.push(...node.expressions);
|
90
|
+
} else {
|
91
|
+
array.push(node);
|
92
|
+
}
|
93
|
+
return array;
|
94
|
+
}
|
95
|
+
|
96
|
+
export function make_sequence(orig, expressions) {
|
97
|
+
if (expressions.length == 1) return expressions[0];
|
98
|
+
if (expressions.length == 0) throw new Error("trying to create a sequence with length zero!");
|
99
|
+
return make_node(AST_Sequence, orig, {
|
100
|
+
expressions: expressions.reduce(merge_sequence, [])
|
101
|
+
});
|
102
|
+
}
|
103
|
+
|
104
|
+
export function make_node_from_constant(val, orig) {
|
105
|
+
switch (typeof val) {
|
106
|
+
case "string":
|
107
|
+
return make_node(AST_String, orig, {
|
108
|
+
value: val
|
109
|
+
});
|
110
|
+
case "number":
|
111
|
+
if (isNaN(val)) return make_node(AST_NaN, orig);
|
112
|
+
if (isFinite(val)) {
|
113
|
+
return 1 / val < 0 ? make_node(AST_UnaryPrefix, orig, {
|
114
|
+
operator: "-",
|
115
|
+
expression: make_node(AST_Number, orig, { value: -val })
|
116
|
+
}) : make_node(AST_Number, orig, { value: val });
|
117
|
+
}
|
118
|
+
return val < 0 ? make_node(AST_UnaryPrefix, orig, {
|
119
|
+
operator: "-",
|
120
|
+
expression: make_node(AST_Infinity, orig)
|
121
|
+
}) : make_node(AST_Infinity, orig);
|
122
|
+
case "boolean":
|
123
|
+
return make_node(val ? AST_True : AST_False, orig);
|
124
|
+
case "undefined":
|
125
|
+
return make_node(AST_Undefined, orig);
|
126
|
+
default:
|
127
|
+
if (val === null) {
|
128
|
+
return make_node(AST_Null, orig, { value: null });
|
129
|
+
}
|
130
|
+
if (val instanceof RegExp) {
|
131
|
+
return make_node(AST_RegExp, orig, {
|
132
|
+
value: {
|
133
|
+
source: regexp_source_fix(val.source),
|
134
|
+
flags: val.flags
|
135
|
+
}
|
136
|
+
});
|
137
|
+
}
|
138
|
+
throw new Error(string_template("Can't handle constant of type: {type}", {
|
139
|
+
type: typeof val
|
140
|
+
}));
|
141
|
+
}
|
142
|
+
}
|
143
|
+
|
144
|
+
export function best_of_expression(ast1, ast2) {
|
145
|
+
return ast1.size() > ast2.size() ? ast2 : ast1;
|
146
|
+
}
|
147
|
+
|
148
|
+
export function best_of_statement(ast1, ast2) {
|
149
|
+
return best_of_expression(
|
150
|
+
make_node(AST_SimpleStatement, ast1, {
|
151
|
+
body: ast1
|
152
|
+
}),
|
153
|
+
make_node(AST_SimpleStatement, ast2, {
|
154
|
+
body: ast2
|
155
|
+
})
|
156
|
+
).body;
|
157
|
+
}
|
158
|
+
|
159
|
+
/** Find which node is smaller, and return that */
|
160
|
+
export function best_of(compressor, ast1, ast2) {
|
161
|
+
if (first_in_statement(compressor)) {
|
162
|
+
return best_of_statement(ast1, ast2);
|
163
|
+
} else {
|
164
|
+
return best_of_expression(ast1, ast2);
|
165
|
+
}
|
166
|
+
}
|
167
|
+
|
168
|
+
/** Simplify an object property's key, if possible */
|
169
|
+
export function get_simple_key(key) {
|
170
|
+
if (key instanceof AST_Constant) {
|
171
|
+
return key.getValue();
|
172
|
+
}
|
173
|
+
if (key instanceof AST_UnaryPrefix
|
174
|
+
&& key.operator == "void"
|
175
|
+
&& key.expression instanceof AST_Constant) {
|
176
|
+
return;
|
177
|
+
}
|
178
|
+
return key;
|
179
|
+
}
|
180
|
+
|
181
|
+
export function read_property(obj, key) {
|
182
|
+
key = get_simple_key(key);
|
183
|
+
if (key instanceof AST_Node) return;
|
184
|
+
|
185
|
+
var value;
|
186
|
+
if (obj instanceof AST_Array) {
|
187
|
+
var elements = obj.elements;
|
188
|
+
if (key == "length") return make_node_from_constant(elements.length, obj);
|
189
|
+
if (typeof key == "number" && key in elements) value = elements[key];
|
190
|
+
} else if (obj instanceof AST_Object) {
|
191
|
+
key = "" + key;
|
192
|
+
var props = obj.properties;
|
193
|
+
for (var i = props.length; --i >= 0;) {
|
194
|
+
var prop = props[i];
|
195
|
+
if (!(prop instanceof AST_ObjectKeyVal)) return;
|
196
|
+
if (!value && props[i].key === key) value = props[i].value;
|
197
|
+
}
|
198
|
+
}
|
199
|
+
|
200
|
+
return value instanceof AST_SymbolRef && value.fixed_value() || value;
|
201
|
+
}
|
202
|
+
|
203
|
+
export function has_break_or_continue(loop, parent) {
|
204
|
+
var found = false;
|
205
|
+
var tw = new TreeWalker(function(node) {
|
206
|
+
if (found || node instanceof AST_Scope) return true;
|
207
|
+
if (node instanceof AST_LoopControl && tw.loopcontrol_target(node) === loop) {
|
208
|
+
return found = true;
|
209
|
+
}
|
210
|
+
});
|
211
|
+
if (parent instanceof AST_LabeledStatement) tw.push(parent);
|
212
|
+
tw.push(loop);
|
213
|
+
loop.body.walk(tw);
|
214
|
+
return found;
|
215
|
+
}
|
216
|
+
|
217
|
+
// we shouldn't compress (1,func)(something) to
|
218
|
+
// func(something) because that changes the meaning of
|
219
|
+
// the func (becomes lexical instead of global).
|
220
|
+
export function maintain_this_binding(parent, orig, val) {
|
221
|
+
if (
|
222
|
+
parent instanceof AST_UnaryPrefix && parent.operator == "delete"
|
223
|
+
|| parent instanceof AST_Call && parent.expression === orig
|
224
|
+
&& (
|
225
|
+
val instanceof AST_PropAccess
|
226
|
+
|| val instanceof AST_SymbolRef && val.name == "eval"
|
227
|
+
)
|
228
|
+
) {
|
229
|
+
const zero = make_node(AST_Number, orig, { value: 0 });
|
230
|
+
return make_sequence(orig, [ zero, val ]);
|
231
|
+
} else {
|
232
|
+
return val;
|
233
|
+
}
|
234
|
+
}
|
235
|
+
|
236
|
+
export function is_func_expr(node) {
|
237
|
+
return node instanceof AST_Arrow || node instanceof AST_Function;
|
238
|
+
}
|
239
|
+
|
240
|
+
export function is_iife_call(node) {
|
241
|
+
// Used to determine whether the node can benefit from negation.
|
242
|
+
// Not the case with arrow functions (you need an extra set of parens).
|
243
|
+
if (node.TYPE != "Call") return false;
|
244
|
+
return node.expression instanceof AST_Function || is_iife_call(node.expression);
|
245
|
+
}
|
246
|
+
|
247
|
+
export const identifier_atom = makePredicate("Infinity NaN undefined");
|
248
|
+
export function is_identifier_atom(node) {
|
249
|
+
return node instanceof AST_Infinity
|
250
|
+
|| node instanceof AST_NaN
|
251
|
+
|| node instanceof AST_Undefined;
|
252
|
+
}
|
253
|
+
|
254
|
+
/** Check if this is a SymbolRef node which has one def of a certain AST type */
|
255
|
+
export function is_ref_of(ref, type) {
|
256
|
+
if (!(ref instanceof AST_SymbolRef)) return false;
|
257
|
+
var orig = ref.definition().orig;
|
258
|
+
for (var i = orig.length; --i >= 0;) {
|
259
|
+
if (orig[i] instanceof type) return true;
|
260
|
+
}
|
261
|
+
}
|
262
|
+
|
263
|
+
// Can we turn { block contents... } into just the block contents ?
|
264
|
+
// Not if one of these is inside.
|
265
|
+
export function can_be_evicted_from_block(node) {
|
266
|
+
return !(
|
267
|
+
node instanceof AST_DefClass ||
|
268
|
+
node instanceof AST_Defun ||
|
269
|
+
node instanceof AST_Let ||
|
270
|
+
node instanceof AST_Const ||
|
271
|
+
node instanceof AST_Export ||
|
272
|
+
node instanceof AST_Import
|
273
|
+
);
|
274
|
+
}
|
275
|
+
|
276
|
+
export function as_statement_array(thing) {
|
277
|
+
if (thing === null) return [];
|
278
|
+
if (thing instanceof AST_BlockStatement) return thing.body;
|
279
|
+
if (thing instanceof AST_EmptyStatement) return [];
|
280
|
+
if (thing instanceof AST_Statement) return [ thing ];
|
281
|
+
throw new Error("Can't convert thing to statement array");
|
282
|
+
}
|
283
|
+
|
284
|
+
/** Check if a ref refers to the name of a function/class it's defined within */
|
285
|
+
export function is_recursive_ref(compressor, def) {
|
286
|
+
var node;
|
287
|
+
for (var i = 0; node = compressor.parent(i); i++) {
|
288
|
+
if (node instanceof AST_Lambda || node instanceof AST_Class) {
|
289
|
+
var name = node.name;
|
290
|
+
if (name && name.definition() === def) {
|
291
|
+
return true;
|
292
|
+
}
|
293
|
+
}
|
294
|
+
}
|
295
|
+
return false;
|
296
|
+
}
|
@@ -0,0 +1,63 @@
|
|
1
|
+
/***********************************************************************
|
2
|
+
|
3
|
+
A JavaScript tokenizer / parser / beautifier / compressor.
|
4
|
+
https://github.com/mishoo/UglifyJS2
|
5
|
+
|
6
|
+
-------------------------------- (C) ---------------------------------
|
7
|
+
|
8
|
+
Author: Mihai Bazon
|
9
|
+
<mihai.bazon@gmail.com>
|
10
|
+
http://mihai.bazon.net/blog
|
11
|
+
|
12
|
+
Distributed under the BSD license:
|
13
|
+
|
14
|
+
Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
|
15
|
+
|
16
|
+
Redistribution and use in source and binary forms, with or without
|
17
|
+
modification, are permitted provided that the following conditions
|
18
|
+
are met:
|
19
|
+
|
20
|
+
* Redistributions of source code must retain the above
|
21
|
+
copyright notice, this list of conditions and the following
|
22
|
+
disclaimer.
|
23
|
+
|
24
|
+
* Redistributions in binary form must reproduce the above
|
25
|
+
copyright notice, this list of conditions and the following
|
26
|
+
disclaimer in the documentation and/or other materials
|
27
|
+
provided with the distribution.
|
28
|
+
|
29
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
|
30
|
+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
31
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
32
|
+
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
|
33
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
34
|
+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
35
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
36
|
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
37
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
38
|
+
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
39
|
+
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
40
|
+
SUCH DAMAGE.
|
41
|
+
|
42
|
+
***********************************************************************/
|
43
|
+
|
44
|
+
// bitfield flags to be stored in node.flags.
|
45
|
+
// These are set and unset during compression, and store information in the node without requiring multiple fields.
|
46
|
+
export const UNUSED = 0b00000001;
|
47
|
+
export const TRUTHY = 0b00000010;
|
48
|
+
export const FALSY = 0b00000100;
|
49
|
+
export const UNDEFINED = 0b00001000;
|
50
|
+
export const INLINED = 0b00010000;
|
51
|
+
|
52
|
+
// Nodes to which values are ever written. Used when keep_assign is part of the unused option string.
|
53
|
+
export const WRITE_ONLY = 0b00100000;
|
54
|
+
|
55
|
+
// information specific to a single compression pass
|
56
|
+
export const SQUEEZED = 0b0000000100000000;
|
57
|
+
export const OPTIMIZED = 0b0000001000000000;
|
58
|
+
export const TOP = 0b0000010000000000;
|
59
|
+
export const CLEAR_BETWEEN_PASSES = SQUEEZED | OPTIMIZED | TOP;
|
60
|
+
|
61
|
+
export const has_flag = (node, flag) => node.flags & flag;
|
62
|
+
export const set_flag = (node, flag) => { node.flags |= flag; };
|
63
|
+
export const clear_flag = (node, flag) => { node.flags &= ~flag; };
|