terser 5.20.0 → 5.21.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 +4 -0
- package/dist/bundle.min.js +103 -103
- package/lib/compress/common.js +1 -1
- package/lib/compress/drop-unused.js +8 -8
- package/lib/compress/global-defs.js +92 -0
- package/lib/compress/index.js +9 -17
- package/lib/compress/inference.js +1 -79
- package/lib/compress/inline.js +10 -3
- package/lib/output.js +2 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## v5.20.1
|
4
|
+
- Do not inline functions that would be retained in the toplevel (as this would cause code duplication).
|
5
|
+
- Fix precedence of arrow function and ternary operator when formatting output.
|
6
|
+
|
3
7
|
## v5.20.0
|
4
8
|
- Passing `minify()` zero files will now throw a clean exception (#1450)
|
5
9
|
- `drop_console` supports passing in an array of `console.*` method names (#1445)
|
package/dist/bundle.min.js
CHANGED
@@ -9742,7 +9742,8 @@ function OutputStream(options) {
|
|
9742
9742
|
) {
|
9743
9743
|
return true;
|
9744
9744
|
}
|
9745
|
-
return p instanceof AST_PropAccess && p.expression === this
|
9745
|
+
return p instanceof AST_PropAccess && p.expression === this
|
9746
|
+
|| p instanceof AST_Conditional && p.condition === this;
|
9746
9747
|
});
|
9747
9748
|
|
9748
9749
|
// same goes for an object literal (as in AST_Function), because
|
@@ -13190,7 +13191,7 @@ function retain_top_func(fn, compressor) {
|
|
13190
13191
|
&& fn instanceof AST_Defun
|
13191
13192
|
&& has_flag(fn, TOP)
|
13192
13193
|
&& fn.name
|
13193
|
-
&& compressor.top_retain(fn.name);
|
13194
|
+
&& compressor.top_retain(fn.name.definition());
|
13194
13195
|
}
|
13195
13196
|
|
13196
13197
|
/***********************************************************************
|
@@ -14039,80 +14040,6 @@ function is_lhs(node, parent) {
|
|
14039
14040
|
if (parent instanceof AST_ForIn && parent.init === node) return node;
|
14040
14041
|
}
|
14041
14042
|
|
14042
|
-
(function(def_find_defs) {
|
14043
|
-
function to_node(value, orig) {
|
14044
|
-
if (value instanceof AST_Node) {
|
14045
|
-
if (!(value instanceof AST_Constant)) {
|
14046
|
-
// Value may be a function, an array including functions and even a complex assign / block expression,
|
14047
|
-
// so it should never be shared in different places.
|
14048
|
-
// Otherwise wrong information may be used in the compression phase
|
14049
|
-
value = value.clone(true);
|
14050
|
-
}
|
14051
|
-
return make_node(value.CTOR, orig, value);
|
14052
|
-
}
|
14053
|
-
if (Array.isArray(value)) return make_node(AST_Array, orig, {
|
14054
|
-
elements: value.map(function(value) {
|
14055
|
-
return to_node(value, orig);
|
14056
|
-
})
|
14057
|
-
});
|
14058
|
-
if (value && typeof value == "object") {
|
14059
|
-
var props = [];
|
14060
|
-
for (var key in value) if (HOP(value, key)) {
|
14061
|
-
props.push(make_node(AST_ObjectKeyVal, orig, {
|
14062
|
-
key: key,
|
14063
|
-
value: to_node(value[key], orig)
|
14064
|
-
}));
|
14065
|
-
}
|
14066
|
-
return make_node(AST_Object, orig, {
|
14067
|
-
properties: props
|
14068
|
-
});
|
14069
|
-
}
|
14070
|
-
return make_node_from_constant(value, orig);
|
14071
|
-
}
|
14072
|
-
|
14073
|
-
AST_Toplevel.DEFMETHOD("resolve_defines", function(compressor) {
|
14074
|
-
if (!compressor.option("global_defs")) return this;
|
14075
|
-
this.figure_out_scope({ ie8: compressor.option("ie8") });
|
14076
|
-
return this.transform(new TreeTransformer(function(node) {
|
14077
|
-
var def = node._find_defs(compressor, "");
|
14078
|
-
if (!def) return;
|
14079
|
-
var level = 0, child = node, parent;
|
14080
|
-
while (parent = this.parent(level++)) {
|
14081
|
-
if (!(parent instanceof AST_PropAccess)) break;
|
14082
|
-
if (parent.expression !== child) break;
|
14083
|
-
child = parent;
|
14084
|
-
}
|
14085
|
-
if (is_lhs(child, parent)) {
|
14086
|
-
return;
|
14087
|
-
}
|
14088
|
-
return def;
|
14089
|
-
}));
|
14090
|
-
});
|
14091
|
-
def_find_defs(AST_Node, noop);
|
14092
|
-
def_find_defs(AST_Chain, function(compressor, suffix) {
|
14093
|
-
return this.expression._find_defs(compressor, suffix);
|
14094
|
-
});
|
14095
|
-
def_find_defs(AST_Dot, function(compressor, suffix) {
|
14096
|
-
return this.expression._find_defs(compressor, "." + this.property + suffix);
|
14097
|
-
});
|
14098
|
-
def_find_defs(AST_SymbolDeclaration, function() {
|
14099
|
-
if (!this.global()) return;
|
14100
|
-
});
|
14101
|
-
def_find_defs(AST_SymbolRef, function(compressor, suffix) {
|
14102
|
-
if (!this.global()) return;
|
14103
|
-
var defines = compressor.option("global_defs");
|
14104
|
-
var name = this.name + suffix;
|
14105
|
-
if (HOP(defines, name)) return to_node(defines[name], this);
|
14106
|
-
});
|
14107
|
-
def_find_defs(AST_ImportMeta, function(compressor, suffix) {
|
14108
|
-
var defines = compressor.option("global_defs");
|
14109
|
-
var name = "import.meta" + suffix;
|
14110
|
-
if (HOP(defines, name)) return to_node(defines[name], this);
|
14111
|
-
});
|
14112
|
-
})(function(node, func) {
|
14113
|
-
node.DEFMETHOD("_find_defs", func);
|
14114
|
-
});
|
14115
|
-
|
14116
14043
|
// method to negate an expression
|
14117
14044
|
(function(def_negate) {
|
14118
14045
|
function basic_negation(exp) {
|
@@ -15169,7 +15096,7 @@ AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
|
|
15169
15096
|
var fixed_ids = new Map();
|
15170
15097
|
if (self instanceof AST_Toplevel && compressor.top_retain) {
|
15171
15098
|
self.variables.forEach(function(def) {
|
15172
|
-
if (compressor.top_retain(def)
|
15099
|
+
if (compressor.top_retain(def)) {
|
15173
15100
|
in_use_ids.set(def.id, def);
|
15174
15101
|
}
|
15175
15102
|
});
|
@@ -15184,9 +15111,7 @@ AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
|
|
15184
15111
|
node.argnames.forEach(function(argname) {
|
15185
15112
|
if (!(argname instanceof AST_SymbolDeclaration)) return;
|
15186
15113
|
var def = argname.definition();
|
15187
|
-
|
15188
|
-
in_use_ids.set(def.id, def);
|
15189
|
-
}
|
15114
|
+
in_use_ids.set(def.id, def);
|
15190
15115
|
});
|
15191
15116
|
}
|
15192
15117
|
if (node === self) return;
|
@@ -15199,7 +15124,7 @@ AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
|
|
15199
15124
|
var node_def = node.name.definition();
|
15200
15125
|
const in_export = tw.parent() instanceof AST_Export;
|
15201
15126
|
if (in_export || !drop_funcs && scope === self) {
|
15202
|
-
if (node_def.global
|
15127
|
+
if (node_def.global) {
|
15203
15128
|
in_use_ids.set(node_def.id, node_def);
|
15204
15129
|
}
|
15205
15130
|
}
|
@@ -15207,10 +15132,12 @@ AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
|
|
15207
15132
|
map_add(initializations, node_def.id, node);
|
15208
15133
|
return true; // don't go in nested scopes
|
15209
15134
|
}
|
15210
|
-
|
15135
|
+
// In the root scope, we drop things. In inner scopes, we just check for uses.
|
15136
|
+
const in_root_scope = scope === self;
|
15137
|
+
if (node instanceof AST_SymbolFunarg && in_root_scope) {
|
15211
15138
|
map_add(var_defs_by_id, node.definition().id, node);
|
15212
15139
|
}
|
15213
|
-
if (node instanceof AST_Definitions &&
|
15140
|
+
if (node instanceof AST_Definitions && in_root_scope) {
|
15214
15141
|
const in_export = tw.parent() instanceof AST_Export;
|
15215
15142
|
node.definitions.forEach(function(def) {
|
15216
15143
|
if (def.name instanceof AST_SymbolVar) {
|
@@ -15220,7 +15147,7 @@ AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
|
|
15220
15147
|
walk(def.name, node => {
|
15221
15148
|
if (node instanceof AST_SymbolDeclaration) {
|
15222
15149
|
const def = node.definition();
|
15223
|
-
if (def.global
|
15150
|
+
if (def.global) {
|
15224
15151
|
in_use_ids.set(def.id, def);
|
15225
15152
|
}
|
15226
15153
|
}
|
@@ -17903,8 +17830,9 @@ function inline_into_symbolref(self, compressor) {
|
|
17903
17830
|
return self;
|
17904
17831
|
}
|
17905
17832
|
|
17906
|
-
function inline_into_call(self,
|
17833
|
+
function inline_into_call(self, compressor) {
|
17907
17834
|
var exp = self.expression;
|
17835
|
+
var fn = exp;
|
17908
17836
|
var simple_args = self.args.every((arg) => !(arg instanceof AST_Expansion));
|
17909
17837
|
|
17910
17838
|
if (compressor.option("reduce_vars")
|
@@ -17912,9 +17840,15 @@ function inline_into_call(self, fn, compressor) {
|
|
17912
17840
|
&& !has_annotation(self, _NOINLINE)
|
17913
17841
|
) {
|
17914
17842
|
const fixed = fn.fixed_value();
|
17915
|
-
|
17916
|
-
|
17843
|
+
|
17844
|
+
if (
|
17845
|
+
retain_top_func(fixed, compressor)
|
17846
|
+
|| !compressor.toplevel.funcs && exp.definition().global
|
17847
|
+
) {
|
17848
|
+
return self;
|
17917
17849
|
}
|
17850
|
+
|
17851
|
+
fn = fixed;
|
17918
17852
|
}
|
17919
17853
|
|
17920
17854
|
var is_func = fn instanceof AST_Lambda;
|
@@ -18245,6 +18179,80 @@ function inline_into_call(self, fn, compressor) {
|
|
18245
18179
|
}
|
18246
18180
|
}
|
18247
18181
|
|
18182
|
+
(function(def_find_defs) {
|
18183
|
+
function to_node(value, orig) {
|
18184
|
+
if (value instanceof AST_Node) {
|
18185
|
+
if (!(value instanceof AST_Constant)) {
|
18186
|
+
// Value may be a function, an array including functions and even a complex assign / block expression,
|
18187
|
+
// so it should never be shared in different places.
|
18188
|
+
// Otherwise wrong information may be used in the compression phase
|
18189
|
+
value = value.clone(true);
|
18190
|
+
}
|
18191
|
+
return make_node(value.CTOR, orig, value);
|
18192
|
+
}
|
18193
|
+
if (Array.isArray(value)) return make_node(AST_Array, orig, {
|
18194
|
+
elements: value.map(function(value) {
|
18195
|
+
return to_node(value, orig);
|
18196
|
+
})
|
18197
|
+
});
|
18198
|
+
if (value && typeof value == "object") {
|
18199
|
+
var props = [];
|
18200
|
+
for (var key in value) if (HOP(value, key)) {
|
18201
|
+
props.push(make_node(AST_ObjectKeyVal, orig, {
|
18202
|
+
key: key,
|
18203
|
+
value: to_node(value[key], orig)
|
18204
|
+
}));
|
18205
|
+
}
|
18206
|
+
return make_node(AST_Object, orig, {
|
18207
|
+
properties: props
|
18208
|
+
});
|
18209
|
+
}
|
18210
|
+
return make_node_from_constant(value, orig);
|
18211
|
+
}
|
18212
|
+
|
18213
|
+
AST_Toplevel.DEFMETHOD("resolve_defines", function(compressor) {
|
18214
|
+
if (!compressor.option("global_defs")) return this;
|
18215
|
+
this.figure_out_scope({ ie8: compressor.option("ie8") });
|
18216
|
+
return this.transform(new TreeTransformer(function(node) {
|
18217
|
+
var def = node._find_defs(compressor, "");
|
18218
|
+
if (!def) return;
|
18219
|
+
var level = 0, child = node, parent;
|
18220
|
+
while (parent = this.parent(level++)) {
|
18221
|
+
if (!(parent instanceof AST_PropAccess)) break;
|
18222
|
+
if (parent.expression !== child) break;
|
18223
|
+
child = parent;
|
18224
|
+
}
|
18225
|
+
if (is_lhs(child, parent)) {
|
18226
|
+
return;
|
18227
|
+
}
|
18228
|
+
return def;
|
18229
|
+
}));
|
18230
|
+
});
|
18231
|
+
def_find_defs(AST_Node, noop);
|
18232
|
+
def_find_defs(AST_Chain, function(compressor, suffix) {
|
18233
|
+
return this.expression._find_defs(compressor, suffix);
|
18234
|
+
});
|
18235
|
+
def_find_defs(AST_Dot, function(compressor, suffix) {
|
18236
|
+
return this.expression._find_defs(compressor, "." + this.property + suffix);
|
18237
|
+
});
|
18238
|
+
def_find_defs(AST_SymbolDeclaration, function() {
|
18239
|
+
if (!this.global()) return;
|
18240
|
+
});
|
18241
|
+
def_find_defs(AST_SymbolRef, function(compressor, suffix) {
|
18242
|
+
if (!this.global()) return;
|
18243
|
+
var defines = compressor.option("global_defs");
|
18244
|
+
var name = this.name + suffix;
|
18245
|
+
if (HOP(defines, name)) return to_node(defines[name], this);
|
18246
|
+
});
|
18247
|
+
def_find_defs(AST_ImportMeta, function(compressor, suffix) {
|
18248
|
+
var defines = compressor.option("global_defs");
|
18249
|
+
var name = "import.meta" + suffix;
|
18250
|
+
if (HOP(defines, name)) return to_node(defines[name], this);
|
18251
|
+
});
|
18252
|
+
})(function(node, func) {
|
18253
|
+
node.DEFMETHOD("_find_defs", func);
|
18254
|
+
});
|
18255
|
+
|
18248
18256
|
/***********************************************************************
|
18249
18257
|
|
18250
18258
|
A JavaScript tokenizer / parser / beautifier / compressor.
|
@@ -19687,18 +19695,10 @@ def_optimize(AST_Call, function(self, compressor) {
|
|
19687
19695
|
var exp = self.expression;
|
19688
19696
|
var fn = exp;
|
19689
19697
|
inline_array_like_spread(self.args);
|
19690
|
-
var simple_args = self.args.every((arg) =>
|
19691
|
-
!(arg instanceof AST_Expansion)
|
19692
|
-
);
|
19698
|
+
var simple_args = self.args.every((arg) => !(arg instanceof AST_Expansion));
|
19693
19699
|
|
19694
|
-
if (compressor.option("reduce_vars")
|
19695
|
-
|
19696
|
-
&& !has_annotation(self, _NOINLINE)
|
19697
|
-
) {
|
19698
|
-
const fixed = fn.fixed_value();
|
19699
|
-
if (!retain_top_func(fixed, compressor)) {
|
19700
|
-
fn = fixed;
|
19701
|
-
}
|
19700
|
+
if (compressor.option("reduce_vars") && fn instanceof AST_SymbolRef) {
|
19701
|
+
fn = fn.fixed_value();
|
19702
19702
|
}
|
19703
19703
|
|
19704
19704
|
var is_func = fn instanceof AST_Lambda;
|
@@ -20006,7 +20006,7 @@ def_optimize(AST_Call, function(self, compressor) {
|
|
20006
20006
|
}
|
20007
20007
|
}
|
20008
20008
|
|
20009
|
-
return inline_into_call(self,
|
20009
|
+
return inline_into_call(self, compressor);
|
20010
20010
|
});
|
20011
20011
|
|
20012
20012
|
def_optimize(AST_New, function(self, compressor) {
|
@@ -21839,10 +21839,10 @@ def_optimize(AST_Destructuring, function(self, compressor) {
|
|
21839
21839
|
if (def.references.length) return true;
|
21840
21840
|
if (!def.global) return false;
|
21841
21841
|
if (compressor.toplevel.vars) {
|
21842
|
-
|
21843
|
-
|
21844
|
-
|
21845
|
-
|
21842
|
+
if (compressor.top_retain) {
|
21843
|
+
return compressor.top_retain(def);
|
21844
|
+
}
|
21845
|
+
return false;
|
21846
21846
|
}
|
21847
21847
|
return true;
|
21848
21848
|
}
|
package/lib/compress/common.js
CHANGED
@@ -128,7 +128,7 @@ AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
|
|
128
128
|
var fixed_ids = new Map();
|
129
129
|
if (self instanceof AST_Toplevel && compressor.top_retain) {
|
130
130
|
self.variables.forEach(function(def) {
|
131
|
-
if (compressor.top_retain(def)
|
131
|
+
if (compressor.top_retain(def)) {
|
132
132
|
in_use_ids.set(def.id, def);
|
133
133
|
}
|
134
134
|
});
|
@@ -143,9 +143,7 @@ AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
|
|
143
143
|
node.argnames.forEach(function(argname) {
|
144
144
|
if (!(argname instanceof AST_SymbolDeclaration)) return;
|
145
145
|
var def = argname.definition();
|
146
|
-
|
147
|
-
in_use_ids.set(def.id, def);
|
148
|
-
}
|
146
|
+
in_use_ids.set(def.id, def);
|
149
147
|
});
|
150
148
|
}
|
151
149
|
if (node === self) return;
|
@@ -158,7 +156,7 @@ AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
|
|
158
156
|
var node_def = node.name.definition();
|
159
157
|
const in_export = tw.parent() instanceof AST_Export;
|
160
158
|
if (in_export || !drop_funcs && scope === self) {
|
161
|
-
if (node_def.global
|
159
|
+
if (node_def.global) {
|
162
160
|
in_use_ids.set(node_def.id, node_def);
|
163
161
|
}
|
164
162
|
}
|
@@ -166,10 +164,12 @@ AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
|
|
166
164
|
map_add(initializations, node_def.id, node);
|
167
165
|
return true; // don't go in nested scopes
|
168
166
|
}
|
169
|
-
|
167
|
+
// In the root scope, we drop things. In inner scopes, we just check for uses.
|
168
|
+
const in_root_scope = scope === self;
|
169
|
+
if (node instanceof AST_SymbolFunarg && in_root_scope) {
|
170
170
|
map_add(var_defs_by_id, node.definition().id, node);
|
171
171
|
}
|
172
|
-
if (node instanceof AST_Definitions &&
|
172
|
+
if (node instanceof AST_Definitions && in_root_scope) {
|
173
173
|
const in_export = tw.parent() instanceof AST_Export;
|
174
174
|
node.definitions.forEach(function(def) {
|
175
175
|
if (def.name instanceof AST_SymbolVar) {
|
@@ -179,7 +179,7 @@ AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
|
|
179
179
|
walk(def.name, node => {
|
180
180
|
if (node instanceof AST_SymbolDeclaration) {
|
181
181
|
const def = node.definition();
|
182
|
-
if (def.global
|
182
|
+
if (def.global) {
|
183
183
|
in_use_ids.set(def.id, def);
|
184
184
|
}
|
185
185
|
}
|
@@ -0,0 +1,92 @@
|
|
1
|
+
import {
|
2
|
+
AST_Array,
|
3
|
+
AST_Chain,
|
4
|
+
AST_Constant,
|
5
|
+
AST_Dot,
|
6
|
+
AST_ImportMeta,
|
7
|
+
AST_Node,
|
8
|
+
AST_Object,
|
9
|
+
AST_ObjectKeyVal,
|
10
|
+
AST_PropAccess,
|
11
|
+
AST_SymbolDeclaration,
|
12
|
+
AST_SymbolRef,
|
13
|
+
AST_Toplevel,
|
14
|
+
TreeTransformer,
|
15
|
+
} from "../ast.js";
|
16
|
+
import { make_node, noop, HOP } from "../utils/index.js";
|
17
|
+
import { make_node_from_constant } from "./common.js";
|
18
|
+
import { is_lhs } from "./inference.js";
|
19
|
+
|
20
|
+
(function(def_find_defs) {
|
21
|
+
function to_node(value, orig) {
|
22
|
+
if (value instanceof AST_Node) {
|
23
|
+
if (!(value instanceof AST_Constant)) {
|
24
|
+
// Value may be a function, an array including functions and even a complex assign / block expression,
|
25
|
+
// so it should never be shared in different places.
|
26
|
+
// Otherwise wrong information may be used in the compression phase
|
27
|
+
value = value.clone(true);
|
28
|
+
}
|
29
|
+
return make_node(value.CTOR, orig, value);
|
30
|
+
}
|
31
|
+
if (Array.isArray(value)) return make_node(AST_Array, orig, {
|
32
|
+
elements: value.map(function(value) {
|
33
|
+
return to_node(value, orig);
|
34
|
+
})
|
35
|
+
});
|
36
|
+
if (value && typeof value == "object") {
|
37
|
+
var props = [];
|
38
|
+
for (var key in value) if (HOP(value, key)) {
|
39
|
+
props.push(make_node(AST_ObjectKeyVal, orig, {
|
40
|
+
key: key,
|
41
|
+
value: to_node(value[key], orig)
|
42
|
+
}));
|
43
|
+
}
|
44
|
+
return make_node(AST_Object, orig, {
|
45
|
+
properties: props
|
46
|
+
});
|
47
|
+
}
|
48
|
+
return make_node_from_constant(value, orig);
|
49
|
+
}
|
50
|
+
|
51
|
+
AST_Toplevel.DEFMETHOD("resolve_defines", function(compressor) {
|
52
|
+
if (!compressor.option("global_defs")) return this;
|
53
|
+
this.figure_out_scope({ ie8: compressor.option("ie8") });
|
54
|
+
return this.transform(new TreeTransformer(function(node) {
|
55
|
+
var def = node._find_defs(compressor, "");
|
56
|
+
if (!def) return;
|
57
|
+
var level = 0, child = node, parent;
|
58
|
+
while (parent = this.parent(level++)) {
|
59
|
+
if (!(parent instanceof AST_PropAccess)) break;
|
60
|
+
if (parent.expression !== child) break;
|
61
|
+
child = parent;
|
62
|
+
}
|
63
|
+
if (is_lhs(child, parent)) {
|
64
|
+
return;
|
65
|
+
}
|
66
|
+
return def;
|
67
|
+
}));
|
68
|
+
});
|
69
|
+
def_find_defs(AST_Node, noop);
|
70
|
+
def_find_defs(AST_Chain, function(compressor, suffix) {
|
71
|
+
return this.expression._find_defs(compressor, suffix);
|
72
|
+
});
|
73
|
+
def_find_defs(AST_Dot, function(compressor, suffix) {
|
74
|
+
return this.expression._find_defs(compressor, "." + this.property + suffix);
|
75
|
+
});
|
76
|
+
def_find_defs(AST_SymbolDeclaration, function() {
|
77
|
+
if (!this.global()) return;
|
78
|
+
});
|
79
|
+
def_find_defs(AST_SymbolRef, function(compressor, suffix) {
|
80
|
+
if (!this.global()) return;
|
81
|
+
var defines = compressor.option("global_defs");
|
82
|
+
var name = this.name + suffix;
|
83
|
+
if (HOP(defines, name)) return to_node(defines[name], this);
|
84
|
+
});
|
85
|
+
def_find_defs(AST_ImportMeta, function(compressor, suffix) {
|
86
|
+
var defines = compressor.option("global_defs");
|
87
|
+
var name = "import.meta" + suffix;
|
88
|
+
if (HOP(defines, name)) return to_node(defines[name], this);
|
89
|
+
});
|
90
|
+
})(function(node, func) {
|
91
|
+
node.DEFMETHOD("_find_defs", func);
|
92
|
+
});
|
package/lib/compress/index.js
CHANGED
@@ -204,11 +204,11 @@ import {
|
|
204
204
|
is_reachable,
|
205
205
|
can_be_evicted_from_block,
|
206
206
|
as_statement_array,
|
207
|
-
retain_top_func,
|
208
207
|
is_func_expr,
|
209
208
|
} from "./common.js";
|
210
209
|
import { tighten_body, trim_unreachable_code } from "./tighten-body.js";
|
211
210
|
import { inline_into_symbolref, inline_into_call } from "./inline.js";
|
211
|
+
import "./global-defs.js";
|
212
212
|
|
213
213
|
class Compressor extends TreeWalker {
|
214
214
|
constructor(options, { false_by_default = false, mangle_options = false }) {
|
@@ -1609,18 +1609,10 @@ def_optimize(AST_Call, function(self, compressor) {
|
|
1609
1609
|
var exp = self.expression;
|
1610
1610
|
var fn = exp;
|
1611
1611
|
inline_array_like_spread(self.args);
|
1612
|
-
var simple_args = self.args.every((arg) =>
|
1613
|
-
!(arg instanceof AST_Expansion)
|
1614
|
-
);
|
1612
|
+
var simple_args = self.args.every((arg) => !(arg instanceof AST_Expansion));
|
1615
1613
|
|
1616
|
-
if (compressor.option("reduce_vars")
|
1617
|
-
|
1618
|
-
&& !has_annotation(self, _NOINLINE)
|
1619
|
-
) {
|
1620
|
-
const fixed = fn.fixed_value();
|
1621
|
-
if (!retain_top_func(fixed, compressor)) {
|
1622
|
-
fn = fixed;
|
1623
|
-
}
|
1614
|
+
if (compressor.option("reduce_vars") && fn instanceof AST_SymbolRef) {
|
1615
|
+
fn = fn.fixed_value();
|
1624
1616
|
}
|
1625
1617
|
|
1626
1618
|
var is_func = fn instanceof AST_Lambda;
|
@@ -1928,7 +1920,7 @@ def_optimize(AST_Call, function(self, compressor) {
|
|
1928
1920
|
}
|
1929
1921
|
}
|
1930
1922
|
|
1931
|
-
return inline_into_call(self,
|
1923
|
+
return inline_into_call(self, compressor);
|
1932
1924
|
});
|
1933
1925
|
|
1934
1926
|
def_optimize(AST_New, function(self, compressor) {
|
@@ -3761,10 +3753,10 @@ def_optimize(AST_Destructuring, function(self, compressor) {
|
|
3761
3753
|
if (def.references.length) return true;
|
3762
3754
|
if (!def.global) return false;
|
3763
3755
|
if (compressor.toplevel.vars) {
|
3764
|
-
|
3765
|
-
|
3766
|
-
|
3767
|
-
|
3756
|
+
if (compressor.top_retain) {
|
3757
|
+
return compressor.top_retain(def);
|
3758
|
+
}
|
3759
|
+
return false;
|
3768
3760
|
}
|
3769
3761
|
return true;
|
3770
3762
|
}
|
@@ -98,7 +98,6 @@ import {
|
|
98
98
|
AST_TemplateSegment,
|
99
99
|
AST_TemplateString,
|
100
100
|
AST_This,
|
101
|
-
AST_Toplevel,
|
102
101
|
AST_True,
|
103
102
|
AST_Try,
|
104
103
|
AST_Unary,
|
@@ -107,7 +106,6 @@ import {
|
|
107
106
|
AST_Undefined,
|
108
107
|
AST_VarDef,
|
109
108
|
|
110
|
-
TreeTransformer,
|
111
109
|
walk,
|
112
110
|
walk_abort,
|
113
111
|
|
@@ -121,11 +119,9 @@ import {
|
|
121
119
|
return_this,
|
122
120
|
make_node,
|
123
121
|
member,
|
124
|
-
noop,
|
125
122
|
has_annotation,
|
126
|
-
HOP
|
127
123
|
} from "../utils/index.js";
|
128
|
-
import {
|
124
|
+
import { make_sequence, best_of_expression, read_property } from "./common.js";
|
129
125
|
|
130
126
|
import { INLINED, UNDEFINED, has_flag } from "./compressor-flags.js";
|
131
127
|
import { pure_prop_access_globals, is_pure_native_fn, is_pure_native_method } from "./native-objects.js";
|
@@ -728,80 +724,6 @@ export function is_lhs(node, parent) {
|
|
728
724
|
if (parent instanceof AST_ForIn && parent.init === node) return node;
|
729
725
|
}
|
730
726
|
|
731
|
-
(function(def_find_defs) {
|
732
|
-
function to_node(value, orig) {
|
733
|
-
if (value instanceof AST_Node) {
|
734
|
-
if (!(value instanceof AST_Constant)) {
|
735
|
-
// Value may be a function, an array including functions and even a complex assign / block expression,
|
736
|
-
// so it should never be shared in different places.
|
737
|
-
// Otherwise wrong information may be used in the compression phase
|
738
|
-
value = value.clone(true);
|
739
|
-
}
|
740
|
-
return make_node(value.CTOR, orig, value);
|
741
|
-
}
|
742
|
-
if (Array.isArray(value)) return make_node(AST_Array, orig, {
|
743
|
-
elements: value.map(function(value) {
|
744
|
-
return to_node(value, orig);
|
745
|
-
})
|
746
|
-
});
|
747
|
-
if (value && typeof value == "object") {
|
748
|
-
var props = [];
|
749
|
-
for (var key in value) if (HOP(value, key)) {
|
750
|
-
props.push(make_node(AST_ObjectKeyVal, orig, {
|
751
|
-
key: key,
|
752
|
-
value: to_node(value[key], orig)
|
753
|
-
}));
|
754
|
-
}
|
755
|
-
return make_node(AST_Object, orig, {
|
756
|
-
properties: props
|
757
|
-
});
|
758
|
-
}
|
759
|
-
return make_node_from_constant(value, orig);
|
760
|
-
}
|
761
|
-
|
762
|
-
AST_Toplevel.DEFMETHOD("resolve_defines", function(compressor) {
|
763
|
-
if (!compressor.option("global_defs")) return this;
|
764
|
-
this.figure_out_scope({ ie8: compressor.option("ie8") });
|
765
|
-
return this.transform(new TreeTransformer(function(node) {
|
766
|
-
var def = node._find_defs(compressor, "");
|
767
|
-
if (!def) return;
|
768
|
-
var level = 0, child = node, parent;
|
769
|
-
while (parent = this.parent(level++)) {
|
770
|
-
if (!(parent instanceof AST_PropAccess)) break;
|
771
|
-
if (parent.expression !== child) break;
|
772
|
-
child = parent;
|
773
|
-
}
|
774
|
-
if (is_lhs(child, parent)) {
|
775
|
-
return;
|
776
|
-
}
|
777
|
-
return def;
|
778
|
-
}));
|
779
|
-
});
|
780
|
-
def_find_defs(AST_Node, noop);
|
781
|
-
def_find_defs(AST_Chain, function(compressor, suffix) {
|
782
|
-
return this.expression._find_defs(compressor, suffix);
|
783
|
-
});
|
784
|
-
def_find_defs(AST_Dot, function(compressor, suffix) {
|
785
|
-
return this.expression._find_defs(compressor, "." + this.property + suffix);
|
786
|
-
});
|
787
|
-
def_find_defs(AST_SymbolDeclaration, function() {
|
788
|
-
if (!this.global()) return;
|
789
|
-
});
|
790
|
-
def_find_defs(AST_SymbolRef, function(compressor, suffix) {
|
791
|
-
if (!this.global()) return;
|
792
|
-
var defines = compressor.option("global_defs");
|
793
|
-
var name = this.name + suffix;
|
794
|
-
if (HOP(defines, name)) return to_node(defines[name], this);
|
795
|
-
});
|
796
|
-
def_find_defs(AST_ImportMeta, function(compressor, suffix) {
|
797
|
-
var defines = compressor.option("global_defs");
|
798
|
-
var name = "import.meta" + suffix;
|
799
|
-
if (HOP(defines, name)) return to_node(defines[name], this);
|
800
|
-
});
|
801
|
-
})(function(node, func) {
|
802
|
-
node.DEFMETHOD("_find_defs", func);
|
803
|
-
});
|
804
|
-
|
805
727
|
// method to negate an expression
|
806
728
|
(function(def_negate) {
|
807
729
|
function basic_negation(exp) {
|
package/lib/compress/inline.js
CHANGED
@@ -290,8 +290,9 @@ export function inline_into_symbolref(self, compressor) {
|
|
290
290
|
return self;
|
291
291
|
}
|
292
292
|
|
293
|
-
export function inline_into_call(self,
|
293
|
+
export function inline_into_call(self, compressor) {
|
294
294
|
var exp = self.expression;
|
295
|
+
var fn = exp;
|
295
296
|
var simple_args = self.args.every((arg) => !(arg instanceof AST_Expansion));
|
296
297
|
|
297
298
|
if (compressor.option("reduce_vars")
|
@@ -299,9 +300,15 @@ export function inline_into_call(self, fn, compressor) {
|
|
299
300
|
&& !has_annotation(self, _NOINLINE)
|
300
301
|
) {
|
301
302
|
const fixed = fn.fixed_value();
|
302
|
-
|
303
|
-
|
303
|
+
|
304
|
+
if (
|
305
|
+
retain_top_func(fixed, compressor)
|
306
|
+
|| !compressor.toplevel.funcs && exp.definition().global
|
307
|
+
) {
|
308
|
+
return self;
|
304
309
|
}
|
310
|
+
|
311
|
+
fn = fixed;
|
305
312
|
}
|
306
313
|
|
307
314
|
var is_func = fn instanceof AST_Lambda;
|
package/lib/output.js
CHANGED
@@ -980,7 +980,8 @@ function OutputStream(options) {
|
|
980
980
|
) {
|
981
981
|
return true;
|
982
982
|
}
|
983
|
-
return p instanceof AST_PropAccess && p.expression === this
|
983
|
+
return p instanceof AST_PropAccess && p.expression === this
|
984
|
+
|| p instanceof AST_Conditional && p.condition === this;
|
984
985
|
});
|
985
986
|
|
986
987
|
// same goes for an object literal (as in AST_Function), because
|