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/lib/transform.js
ADDED
@@ -0,0 +1,313 @@
|
|
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
|
+
"use strict";
|
45
|
+
|
46
|
+
import {
|
47
|
+
AST_Array,
|
48
|
+
AST_Await,
|
49
|
+
AST_Binary,
|
50
|
+
AST_Block,
|
51
|
+
AST_Call,
|
52
|
+
AST_Case,
|
53
|
+
AST_Catch,
|
54
|
+
AST_Class,
|
55
|
+
AST_Conditional,
|
56
|
+
AST_Definitions,
|
57
|
+
AST_Destructuring,
|
58
|
+
AST_Do,
|
59
|
+
AST_Dot,
|
60
|
+
AST_Exit,
|
61
|
+
AST_Expansion,
|
62
|
+
AST_Export,
|
63
|
+
AST_For,
|
64
|
+
AST_ForIn,
|
65
|
+
AST_If,
|
66
|
+
AST_Import,
|
67
|
+
AST_LabeledStatement,
|
68
|
+
AST_Lambda,
|
69
|
+
AST_LoopControl,
|
70
|
+
AST_NameMapping,
|
71
|
+
AST_Node,
|
72
|
+
AST_Number,
|
73
|
+
AST_Object,
|
74
|
+
AST_ObjectProperty,
|
75
|
+
AST_PrefixedTemplateString,
|
76
|
+
AST_Sequence,
|
77
|
+
AST_SimpleStatement,
|
78
|
+
AST_Sub,
|
79
|
+
AST_Switch,
|
80
|
+
AST_TemplateString,
|
81
|
+
AST_Try,
|
82
|
+
AST_Unary,
|
83
|
+
AST_VarDef,
|
84
|
+
AST_While,
|
85
|
+
AST_With,
|
86
|
+
AST_Yield,
|
87
|
+
} from "./ast.js";
|
88
|
+
import {
|
89
|
+
MAP,
|
90
|
+
noop,
|
91
|
+
} from "./utils/index.js";
|
92
|
+
|
93
|
+
function def_transform(node, descend) {
|
94
|
+
node.DEFMETHOD("transform", function(tw, in_list) {
|
95
|
+
let transformed = undefined;
|
96
|
+
tw.push(this);
|
97
|
+
if (tw.before) transformed = tw.before(this, descend, in_list);
|
98
|
+
if (transformed === undefined) {
|
99
|
+
transformed = this;
|
100
|
+
descend(transformed, tw);
|
101
|
+
if (tw.after) {
|
102
|
+
const after_ret = tw.after(transformed, in_list);
|
103
|
+
if (after_ret !== undefined) transformed = after_ret;
|
104
|
+
}
|
105
|
+
}
|
106
|
+
tw.pop();
|
107
|
+
return transformed;
|
108
|
+
});
|
109
|
+
}
|
110
|
+
|
111
|
+
function do_list(list, tw) {
|
112
|
+
return MAP(list, function(node) {
|
113
|
+
return node.transform(tw, true);
|
114
|
+
});
|
115
|
+
}
|
116
|
+
|
117
|
+
def_transform(AST_Node, noop);
|
118
|
+
|
119
|
+
def_transform(AST_LabeledStatement, function(self, tw) {
|
120
|
+
self.label = self.label.transform(tw);
|
121
|
+
self.body = self.body.transform(tw);
|
122
|
+
});
|
123
|
+
|
124
|
+
def_transform(AST_SimpleStatement, function(self, tw) {
|
125
|
+
self.body = self.body.transform(tw);
|
126
|
+
});
|
127
|
+
|
128
|
+
def_transform(AST_Block, function(self, tw) {
|
129
|
+
self.body = do_list(self.body, tw);
|
130
|
+
});
|
131
|
+
|
132
|
+
def_transform(AST_Do, function(self, tw) {
|
133
|
+
self.body = self.body.transform(tw);
|
134
|
+
self.condition = self.condition.transform(tw);
|
135
|
+
});
|
136
|
+
|
137
|
+
def_transform(AST_While, function(self, tw) {
|
138
|
+
self.condition = self.condition.transform(tw);
|
139
|
+
self.body = self.body.transform(tw);
|
140
|
+
});
|
141
|
+
|
142
|
+
def_transform(AST_For, function(self, tw) {
|
143
|
+
if (self.init) self.init = self.init.transform(tw);
|
144
|
+
if (self.condition) self.condition = self.condition.transform(tw);
|
145
|
+
if (self.step) self.step = self.step.transform(tw);
|
146
|
+
self.body = self.body.transform(tw);
|
147
|
+
});
|
148
|
+
|
149
|
+
def_transform(AST_ForIn, function(self, tw) {
|
150
|
+
self.init = self.init.transform(tw);
|
151
|
+
self.object = self.object.transform(tw);
|
152
|
+
self.body = self.body.transform(tw);
|
153
|
+
});
|
154
|
+
|
155
|
+
def_transform(AST_With, function(self, tw) {
|
156
|
+
self.expression = self.expression.transform(tw);
|
157
|
+
self.body = self.body.transform(tw);
|
158
|
+
});
|
159
|
+
|
160
|
+
def_transform(AST_Exit, function(self, tw) {
|
161
|
+
if (self.value) self.value = self.value.transform(tw);
|
162
|
+
});
|
163
|
+
|
164
|
+
def_transform(AST_LoopControl, function(self, tw) {
|
165
|
+
if (self.label) self.label = self.label.transform(tw);
|
166
|
+
});
|
167
|
+
|
168
|
+
def_transform(AST_If, function(self, tw) {
|
169
|
+
self.condition = self.condition.transform(tw);
|
170
|
+
self.body = self.body.transform(tw);
|
171
|
+
if (self.alternative) self.alternative = self.alternative.transform(tw);
|
172
|
+
});
|
173
|
+
|
174
|
+
def_transform(AST_Switch, function(self, tw) {
|
175
|
+
self.expression = self.expression.transform(tw);
|
176
|
+
self.body = do_list(self.body, tw);
|
177
|
+
});
|
178
|
+
|
179
|
+
def_transform(AST_Case, function(self, tw) {
|
180
|
+
self.expression = self.expression.transform(tw);
|
181
|
+
self.body = do_list(self.body, tw);
|
182
|
+
});
|
183
|
+
|
184
|
+
def_transform(AST_Try, function(self, tw) {
|
185
|
+
self.body = do_list(self.body, tw);
|
186
|
+
if (self.bcatch) self.bcatch = self.bcatch.transform(tw);
|
187
|
+
if (self.bfinally) self.bfinally = self.bfinally.transform(tw);
|
188
|
+
});
|
189
|
+
|
190
|
+
def_transform(AST_Catch, function(self, tw) {
|
191
|
+
if (self.argname) self.argname = self.argname.transform(tw);
|
192
|
+
self.body = do_list(self.body, tw);
|
193
|
+
});
|
194
|
+
|
195
|
+
def_transform(AST_Definitions, function(self, tw) {
|
196
|
+
self.definitions = do_list(self.definitions, tw);
|
197
|
+
});
|
198
|
+
|
199
|
+
def_transform(AST_VarDef, function(self, tw) {
|
200
|
+
self.name = self.name.transform(tw);
|
201
|
+
if (self.value) self.value = self.value.transform(tw);
|
202
|
+
});
|
203
|
+
|
204
|
+
def_transform(AST_Destructuring, function(self, tw) {
|
205
|
+
self.names = do_list(self.names, tw);
|
206
|
+
});
|
207
|
+
|
208
|
+
def_transform(AST_Lambda, function(self, tw) {
|
209
|
+
if (self.name) self.name = self.name.transform(tw);
|
210
|
+
self.argnames = do_list(self.argnames, tw);
|
211
|
+
if (self.body instanceof AST_Node) {
|
212
|
+
self.body = self.body.transform(tw);
|
213
|
+
} else {
|
214
|
+
self.body = do_list(self.body, tw);
|
215
|
+
}
|
216
|
+
});
|
217
|
+
|
218
|
+
def_transform(AST_Call, function(self, tw) {
|
219
|
+
self.expression = self.expression.transform(tw);
|
220
|
+
self.args = do_list(self.args, tw);
|
221
|
+
});
|
222
|
+
|
223
|
+
def_transform(AST_Sequence, function(self, tw) {
|
224
|
+
const result = do_list(self.expressions, tw);
|
225
|
+
self.expressions = result.length
|
226
|
+
? result
|
227
|
+
: [new AST_Number({ value: 0 })];
|
228
|
+
});
|
229
|
+
|
230
|
+
def_transform(AST_Dot, function(self, tw) {
|
231
|
+
self.expression = self.expression.transform(tw);
|
232
|
+
});
|
233
|
+
|
234
|
+
def_transform(AST_Sub, function(self, tw) {
|
235
|
+
self.expression = self.expression.transform(tw);
|
236
|
+
self.property = self.property.transform(tw);
|
237
|
+
});
|
238
|
+
|
239
|
+
def_transform(AST_Yield, function(self, tw) {
|
240
|
+
if (self.expression) self.expression = self.expression.transform(tw);
|
241
|
+
});
|
242
|
+
|
243
|
+
def_transform(AST_Await, function(self, tw) {
|
244
|
+
self.expression = self.expression.transform(tw);
|
245
|
+
});
|
246
|
+
|
247
|
+
def_transform(AST_Unary, function(self, tw) {
|
248
|
+
self.expression = self.expression.transform(tw);
|
249
|
+
});
|
250
|
+
|
251
|
+
def_transform(AST_Binary, function(self, tw) {
|
252
|
+
self.left = self.left.transform(tw);
|
253
|
+
self.right = self.right.transform(tw);
|
254
|
+
});
|
255
|
+
|
256
|
+
def_transform(AST_Conditional, function(self, tw) {
|
257
|
+
self.condition = self.condition.transform(tw);
|
258
|
+
self.consequent = self.consequent.transform(tw);
|
259
|
+
self.alternative = self.alternative.transform(tw);
|
260
|
+
});
|
261
|
+
|
262
|
+
def_transform(AST_Array, function(self, tw) {
|
263
|
+
self.elements = do_list(self.elements, tw);
|
264
|
+
});
|
265
|
+
|
266
|
+
def_transform(AST_Object, function(self, tw) {
|
267
|
+
self.properties = do_list(self.properties, tw);
|
268
|
+
});
|
269
|
+
|
270
|
+
def_transform(AST_ObjectProperty, function(self, tw) {
|
271
|
+
if (self.key instanceof AST_Node) {
|
272
|
+
self.key = self.key.transform(tw);
|
273
|
+
}
|
274
|
+
if (self.value) self.value = self.value.transform(tw);
|
275
|
+
});
|
276
|
+
|
277
|
+
def_transform(AST_Class, function(self, tw) {
|
278
|
+
if (self.name) self.name = self.name.transform(tw);
|
279
|
+
if (self.extends) self.extends = self.extends.transform(tw);
|
280
|
+
self.properties = do_list(self.properties, tw);
|
281
|
+
});
|
282
|
+
|
283
|
+
def_transform(AST_Expansion, function(self, tw) {
|
284
|
+
self.expression = self.expression.transform(tw);
|
285
|
+
});
|
286
|
+
|
287
|
+
def_transform(AST_NameMapping, function(self, tw) {
|
288
|
+
self.foreign_name = self.foreign_name.transform(tw);
|
289
|
+
self.name = self.name.transform(tw);
|
290
|
+
});
|
291
|
+
|
292
|
+
def_transform(AST_Import, function(self, tw) {
|
293
|
+
if (self.imported_name) self.imported_name = self.imported_name.transform(tw);
|
294
|
+
if (self.imported_names) do_list(self.imported_names, tw);
|
295
|
+
self.module_name = self.module_name.transform(tw);
|
296
|
+
});
|
297
|
+
|
298
|
+
def_transform(AST_Export, function(self, tw) {
|
299
|
+
if (self.exported_definition) self.exported_definition = self.exported_definition.transform(tw);
|
300
|
+
if (self.exported_value) self.exported_value = self.exported_value.transform(tw);
|
301
|
+
if (self.exported_names) do_list(self.exported_names, tw);
|
302
|
+
if (self.module_name) self.module_name = self.module_name.transform(tw);
|
303
|
+
});
|
304
|
+
|
305
|
+
def_transform(AST_TemplateString, function(self, tw) {
|
306
|
+
self.segments = do_list(self.segments, tw);
|
307
|
+
});
|
308
|
+
|
309
|
+
def_transform(AST_PrefixedTemplateString, function(self, tw) {
|
310
|
+
self.prefix = self.prefix.transform(tw);
|
311
|
+
self.template_string = self.template_string.transform(tw);
|
312
|
+
});
|
313
|
+
|
@@ -0,0 +1,50 @@
|
|
1
|
+
import {
|
2
|
+
AST_Binary,
|
3
|
+
AST_Conditional,
|
4
|
+
AST_Dot,
|
5
|
+
AST_Object,
|
6
|
+
AST_Sequence,
|
7
|
+
AST_Statement,
|
8
|
+
AST_Sub,
|
9
|
+
AST_UnaryPostfix,
|
10
|
+
AST_PrefixedTemplateString
|
11
|
+
} from "../ast.js";
|
12
|
+
|
13
|
+
// return true if the node at the top of the stack (that means the
|
14
|
+
// innermost node in the current output) is lexically the first in
|
15
|
+
// a statement.
|
16
|
+
function first_in_statement(stack) {
|
17
|
+
let node = stack.parent(-1);
|
18
|
+
for (let i = 0, p; p = stack.parent(i); i++) {
|
19
|
+
if (p instanceof AST_Statement && p.body === node)
|
20
|
+
return true;
|
21
|
+
if ((p instanceof AST_Sequence && p.expressions[0] === node) ||
|
22
|
+
(p.TYPE === "Call" && p.expression === node) ||
|
23
|
+
(p instanceof AST_PrefixedTemplateString && p.prefix === node) ||
|
24
|
+
(p instanceof AST_Dot && p.expression === node) ||
|
25
|
+
(p instanceof AST_Sub && p.expression === node) ||
|
26
|
+
(p instanceof AST_Conditional && p.condition === node) ||
|
27
|
+
(p instanceof AST_Binary && p.left === node) ||
|
28
|
+
(p instanceof AST_UnaryPostfix && p.expression === node)
|
29
|
+
) {
|
30
|
+
node = p;
|
31
|
+
} else {
|
32
|
+
return false;
|
33
|
+
}
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
// Returns whether the leftmost item in the expression is an object
|
38
|
+
function left_is_object(node) {
|
39
|
+
if (node instanceof AST_Object) return true;
|
40
|
+
if (node instanceof AST_Sequence) return left_is_object(node.expressions[0]);
|
41
|
+
if (node.TYPE === "Call") return left_is_object(node.expression);
|
42
|
+
if (node instanceof AST_PrefixedTemplateString) return left_is_object(node.prefix);
|
43
|
+
if (node instanceof AST_Dot || node instanceof AST_Sub) return left_is_object(node.expression);
|
44
|
+
if (node instanceof AST_Conditional) return left_is_object(node.condition);
|
45
|
+
if (node instanceof AST_Binary) return left_is_object(node.left);
|
46
|
+
if (node instanceof AST_UnaryPostfix) return left_is_object(node.expression);
|
47
|
+
return false;
|
48
|
+
}
|
49
|
+
|
50
|
+
export { first_in_statement, left_is_object };
|
@@ -0,0 +1,302 @@
|
|
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
|
+
"use strict";
|
45
|
+
|
46
|
+
function characters(str) {
|
47
|
+
return str.split("");
|
48
|
+
}
|
49
|
+
|
50
|
+
function member(name, array) {
|
51
|
+
return array.includes(name);
|
52
|
+
}
|
53
|
+
|
54
|
+
class DefaultsError extends Error {
|
55
|
+
constructor(msg, defs) {
|
56
|
+
super();
|
57
|
+
|
58
|
+
this.name = "DefaultsError";
|
59
|
+
this.message = msg;
|
60
|
+
this.defs = defs;
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
function defaults(args, defs, croak) {
|
65
|
+
if (args === true) {
|
66
|
+
args = {};
|
67
|
+
}
|
68
|
+
|
69
|
+
if (args != null && typeof args === "object") {
|
70
|
+
args = Object.assign({}, args);
|
71
|
+
}
|
72
|
+
|
73
|
+
const ret = args || {};
|
74
|
+
|
75
|
+
if (croak) for (const i in ret) if (HOP(ret, i) && !HOP(defs, i)) {
|
76
|
+
throw new DefaultsError("`" + i + "` is not a supported option", defs);
|
77
|
+
}
|
78
|
+
|
79
|
+
for (const i in defs) if (HOP(defs, i)) {
|
80
|
+
if (!args || !HOP(args, i)) {
|
81
|
+
ret[i] = defs[i];
|
82
|
+
} else if (i === "ecma") {
|
83
|
+
let ecma = args[i] | 0;
|
84
|
+
if (ecma > 5 && ecma < 2015) ecma += 2009;
|
85
|
+
ret[i] = ecma;
|
86
|
+
} else {
|
87
|
+
ret[i] = (args && HOP(args, i)) ? args[i] : defs[i];
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
return ret;
|
92
|
+
}
|
93
|
+
|
94
|
+
function noop() {}
|
95
|
+
function return_false() { return false; }
|
96
|
+
function return_true() { return true; }
|
97
|
+
function return_this() { return this; }
|
98
|
+
function return_null() { return null; }
|
99
|
+
|
100
|
+
var MAP = (function() {
|
101
|
+
function MAP(a, f, backwards) {
|
102
|
+
var ret = [], top = [], i;
|
103
|
+
function doit() {
|
104
|
+
var val = f(a[i], i);
|
105
|
+
var is_last = val instanceof Last;
|
106
|
+
if (is_last) val = val.v;
|
107
|
+
if (val instanceof AtTop) {
|
108
|
+
val = val.v;
|
109
|
+
if (val instanceof Splice) {
|
110
|
+
top.push.apply(top, backwards ? val.v.slice().reverse() : val.v);
|
111
|
+
} else {
|
112
|
+
top.push(val);
|
113
|
+
}
|
114
|
+
} else if (val !== skip) {
|
115
|
+
if (val instanceof Splice) {
|
116
|
+
ret.push.apply(ret, backwards ? val.v.slice().reverse() : val.v);
|
117
|
+
} else {
|
118
|
+
ret.push(val);
|
119
|
+
}
|
120
|
+
}
|
121
|
+
return is_last;
|
122
|
+
}
|
123
|
+
if (Array.isArray(a)) {
|
124
|
+
if (backwards) {
|
125
|
+
for (i = a.length; --i >= 0;) if (doit()) break;
|
126
|
+
ret.reverse();
|
127
|
+
top.reverse();
|
128
|
+
} else {
|
129
|
+
for (i = 0; i < a.length; ++i) if (doit()) break;
|
130
|
+
}
|
131
|
+
} else {
|
132
|
+
for (i in a) if (HOP(a, i)) if (doit()) break;
|
133
|
+
}
|
134
|
+
return top.concat(ret);
|
135
|
+
}
|
136
|
+
MAP.at_top = function(val) { return new AtTop(val); };
|
137
|
+
MAP.splice = function(val) { return new Splice(val); };
|
138
|
+
MAP.last = function(val) { return new Last(val); };
|
139
|
+
var skip = MAP.skip = {};
|
140
|
+
function AtTop(val) { this.v = val; }
|
141
|
+
function Splice(val) { this.v = val; }
|
142
|
+
function Last(val) { this.v = val; }
|
143
|
+
return MAP;
|
144
|
+
})();
|
145
|
+
|
146
|
+
function make_node(ctor, orig, props) {
|
147
|
+
if (!props) props = {};
|
148
|
+
if (orig) {
|
149
|
+
if (!props.start) props.start = orig.start;
|
150
|
+
if (!props.end) props.end = orig.end;
|
151
|
+
}
|
152
|
+
return new ctor(props);
|
153
|
+
}
|
154
|
+
|
155
|
+
function push_uniq(array, el) {
|
156
|
+
if (!array.includes(el))
|
157
|
+
array.push(el);
|
158
|
+
}
|
159
|
+
|
160
|
+
function string_template(text, props) {
|
161
|
+
return text.replace(/{(.+?)}/g, function(str, p) {
|
162
|
+
return props && props[p];
|
163
|
+
});
|
164
|
+
}
|
165
|
+
|
166
|
+
function remove(array, el) {
|
167
|
+
for (var i = array.length; --i >= 0;) {
|
168
|
+
if (array[i] === el) array.splice(i, 1);
|
169
|
+
}
|
170
|
+
}
|
171
|
+
|
172
|
+
function mergeSort(array, cmp) {
|
173
|
+
if (array.length < 2) return array.slice();
|
174
|
+
function merge(a, b) {
|
175
|
+
var r = [], ai = 0, bi = 0, i = 0;
|
176
|
+
while (ai < a.length && bi < b.length) {
|
177
|
+
cmp(a[ai], b[bi]) <= 0
|
178
|
+
? r[i++] = a[ai++]
|
179
|
+
: r[i++] = b[bi++];
|
180
|
+
}
|
181
|
+
if (ai < a.length) r.push.apply(r, a.slice(ai));
|
182
|
+
if (bi < b.length) r.push.apply(r, b.slice(bi));
|
183
|
+
return r;
|
184
|
+
}
|
185
|
+
function _ms(a) {
|
186
|
+
if (a.length <= 1)
|
187
|
+
return a;
|
188
|
+
var m = Math.floor(a.length / 2), left = a.slice(0, m), right = a.slice(m);
|
189
|
+
left = _ms(left);
|
190
|
+
right = _ms(right);
|
191
|
+
return merge(left, right);
|
192
|
+
}
|
193
|
+
return _ms(array);
|
194
|
+
}
|
195
|
+
|
196
|
+
function makePredicate(words) {
|
197
|
+
if (!Array.isArray(words)) words = words.split(" ");
|
198
|
+
|
199
|
+
return new Set(words);
|
200
|
+
}
|
201
|
+
|
202
|
+
function map_add(map, key, value) {
|
203
|
+
if (map.has(key)) {
|
204
|
+
map.get(key).push(value);
|
205
|
+
} else {
|
206
|
+
map.set(key, [ value ]);
|
207
|
+
}
|
208
|
+
}
|
209
|
+
|
210
|
+
function map_from_object(obj) {
|
211
|
+
var map = new Map();
|
212
|
+
for (var key in obj) {
|
213
|
+
if (HOP(obj, key) && key.charAt(0) === "$") {
|
214
|
+
map.set(key.substr(1), obj[key]);
|
215
|
+
}
|
216
|
+
}
|
217
|
+
return map;
|
218
|
+
}
|
219
|
+
|
220
|
+
function map_to_object(map) {
|
221
|
+
var obj = Object.create(null);
|
222
|
+
map.forEach(function (value, key) {
|
223
|
+
obj["$" + key] = value;
|
224
|
+
});
|
225
|
+
return obj;
|
226
|
+
}
|
227
|
+
|
228
|
+
function HOP(obj, prop) {
|
229
|
+
return Object.prototype.hasOwnProperty.call(obj, prop);
|
230
|
+
}
|
231
|
+
|
232
|
+
function keep_name(keep_setting, name) {
|
233
|
+
return keep_setting === true
|
234
|
+
|| (keep_setting instanceof RegExp && keep_setting.test(name));
|
235
|
+
}
|
236
|
+
|
237
|
+
var lineTerminatorEscape = {
|
238
|
+
"\n": "n",
|
239
|
+
"\r": "r",
|
240
|
+
"\u2028": "u2028",
|
241
|
+
"\u2029": "u2029",
|
242
|
+
};
|
243
|
+
function regexp_source_fix(source) {
|
244
|
+
// V8 does not escape line terminators in regexp patterns in node 12
|
245
|
+
return source.replace(/[\n\r\u2028\u2029]/g, function (match, offset) {
|
246
|
+
var escaped = source[offset - 1] == "\\"
|
247
|
+
&& (source[offset - 2] != "\\"
|
248
|
+
|| /(?:^|[^\\])(?:\\{2})*$/.test(source.slice(0, offset - 1)));
|
249
|
+
return (escaped ? "" : "\\") + lineTerminatorEscape[match];
|
250
|
+
});
|
251
|
+
}
|
252
|
+
const all_flags = "gimuy";
|
253
|
+
function sort_regexp_flags(flags) {
|
254
|
+
const existing_flags = new Set(flags.split(""));
|
255
|
+
let out = "";
|
256
|
+
for (const flag of all_flags) {
|
257
|
+
if (existing_flags.has(flag)) {
|
258
|
+
out += flag;
|
259
|
+
existing_flags.delete(flag);
|
260
|
+
}
|
261
|
+
}
|
262
|
+
if (existing_flags.size) {
|
263
|
+
// Flags Terser doesn't know about
|
264
|
+
existing_flags.forEach(flag => { out += flag; });
|
265
|
+
}
|
266
|
+
return out;
|
267
|
+
}
|
268
|
+
|
269
|
+
function has_annotation(node, annotation) {
|
270
|
+
return node._annotations & annotation;
|
271
|
+
}
|
272
|
+
|
273
|
+
function set_annotation(node, annotation) {
|
274
|
+
node._annotations |= annotation;
|
275
|
+
}
|
276
|
+
|
277
|
+
export {
|
278
|
+
characters,
|
279
|
+
defaults,
|
280
|
+
HOP,
|
281
|
+
keep_name,
|
282
|
+
make_node,
|
283
|
+
makePredicate,
|
284
|
+
map_add,
|
285
|
+
map_from_object,
|
286
|
+
map_to_object,
|
287
|
+
MAP,
|
288
|
+
member,
|
289
|
+
mergeSort,
|
290
|
+
noop,
|
291
|
+
push_uniq,
|
292
|
+
regexp_source_fix,
|
293
|
+
remove,
|
294
|
+
return_false,
|
295
|
+
return_null,
|
296
|
+
return_this,
|
297
|
+
return_true,
|
298
|
+
sort_regexp_flags,
|
299
|
+
string_template,
|
300
|
+
has_annotation,
|
301
|
+
set_annotation
|
302
|
+
};
|
package/main.js
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
import "./lib/transform.js";
|
2
|
+
import "./lib/mozilla-ast.js";
|
3
|
+
import { minify } from "./lib/minify.js";
|
4
|
+
|
5
|
+
export { minify } from "./lib/minify.js";
|
6
|
+
export { run_cli as _run_cli } from "./lib/cli.js";
|
7
|
+
|
8
|
+
export async function _default_options() {
|
9
|
+
const defs = {};
|
10
|
+
|
11
|
+
Object.keys(infer_options({ 0: 0 })).forEach((component) => {
|
12
|
+
const options = infer_options({
|
13
|
+
[component]: {0: 0}
|
14
|
+
});
|
15
|
+
|
16
|
+
if (options) defs[component] = options;
|
17
|
+
});
|
18
|
+
return defs;
|
19
|
+
}
|
20
|
+
|
21
|
+
async function infer_options(options) {
|
22
|
+
try {
|
23
|
+
await minify("", options);
|
24
|
+
} catch (error) {
|
25
|
+
return error.defs;
|
26
|
+
}
|
27
|
+
}
|