Skip to content

Commit 68d62a8

Browse files
authored
enable --module by default (#5738)
- fix corner case in `mangle`
1 parent 21aff99 commit 68d62a8

File tree

17 files changed

+293
-91
lines changed

17 files changed

+293
-91
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ jobs:
1212
- '-mb braces'
1313
- '--ie -c'
1414
- '-mc'
15-
- '-p acorn --toplevel -mco spidermonkey'
16-
- '--toplevel -mc passes=3,pure_getters,unsafe'
15+
- '-p acorn -mco spidermonkey'
16+
- '-mc passes=3,pure_getters,unsafe'
1717
script:
1818
- acorn.sh
1919
- bootstrap.sh

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ a double dash to prevent input files being used as option arguments:
119119
--keep-fnames Do not mangle/drop function names. Useful for
120120
code relying on Function.prototype.name.
121121
--module Process input as ES module (implies --toplevel)
122+
--no-module Avoid optimizations which may alter runtime behavior
123+
under prior versions of JavaScript.
122124
--name-cache <file> File to hold mangled name mappings.
123125
--self Build UglifyJS as a library (implies --wrap UglifyJS)
124126
--source-map [options] Enable source map/specify source map options:
@@ -530,9 +532,9 @@ if (result.error) throw result.error;
530532
- `mangle.properties` (default: `false`) — a subcategory of the mangle option.
531533
Pass an object to specify custom [mangle property options](#mangle-properties-options).
532534

533-
- `module` (default: `false`) — set to `true` if you wish to process input as
534-
ES module, i.e. implicit `"use strict";` and support for top-level `await`,
535-
alongside with `toplevel` enabled.
535+
- `module` (default: `true`) — process input as ES module, i.e. implicit
536+
`"use strict";` and support for top-level `await`, alongside with `toplevel`
537+
enabled.
536538

537539
- `nameCache` (default: `null`) — pass an empty object `{}` or a previously
538540
used `nameCache` object if you wish to cache mangled variable and

bin/uglifyjs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ function process_option(name, no_value) {
108108
" --ie Support non-standard Internet Explorer.",
109109
" --keep-fargs Do not mangle/drop function arguments.",
110110
" --keep-fnames Do not mangle/drop function names. Useful for code relying on Function.prototype.name.",
111-
" --module Process input as ES module (implies --toplevel)",
111+
" --module Process input as ES module (implies --toplevel).",
112+
" --no-module Process input with improved JavaScript compatibility.",
112113
" --name-cache <file> File to hold mangled name mappings.",
113114
" --rename Force symbol expansion.",
114115
" --no-rename Disable symbol expansion.",
@@ -155,7 +156,6 @@ function process_option(name, no_value) {
155156
case "expression":
156157
case "ie":
157158
case "ie8":
158-
case "module":
159159
case "timings":
160160
case "toplevel":
161161
case "v8":
@@ -201,6 +201,12 @@ function process_option(name, no_value) {
201201
if (typeof options.mangle != "object") options.mangle = {};
202202
options.mangle.properties = parse_js(read_value(), options.mangle.properties);
203203
break;
204+
case "module":
205+
options.module = true;
206+
break;
207+
case "no-module":
208+
options.module = false;
209+
break;
204210
case "name-cache":
205211
nameCache = read_value(true);
206212
options.nameCache = JSON.parse(read_file(nameCache, "{}"));

lib/compress.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10234,22 +10234,24 @@ Compressor.prototype.compress = function(node) {
1023410234
}
1023510235

1023610236
function varify(self, compressor) {
10237-
return all(self.definitions, function(defn) {
10237+
if (all(self.definitions, function(defn) {
1023810238
return !defn.name.match_symbol(function(node) {
1023910239
if (node instanceof AST_SymbolDeclaration) return !can_varify(compressor, node);
1024010240
}, true);
10241-
}) ? to_var(self, compressor.find_parent(AST_Scope)) : self;
10241+
})) return to_var(self, compressor.find_parent(AST_Scope));
1024210242
}
1024310243

1024410244
OPT(AST_Const, function(self, compressor) {
1024510245
if (!compressor.option("varify")) return self;
10246+
var decl = varify(self, compressor);
10247+
if (decl) return decl;
1024610248
if (can_letify(self, compressor, 0)) return to_let(self);
10247-
return varify(self, compressor);
10249+
return self;
1024810250
});
1024910251

1025010252
OPT(AST_Let, function(self, compressor) {
1025110253
if (!compressor.option("varify")) return self;
10252-
return varify(self, compressor);
10254+
return varify(self, compressor) || self;
1025310255
});
1025410256

1025510257
function trim_optional_chain(node, compressor) {

lib/minify.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ function minify(files, options) {
8282
keep_fargs: false,
8383
keep_fnames: false,
8484
mangle: {},
85-
module: false,
85+
module: undefined,
8686
nameCache: null,
8787
output: {},
8888
parse: {},
8989
rename: undefined,
9090
sourceMap: false,
9191
timings: false,
92-
toplevel: !!(options && !options["expression"] && options["module"]),
92+
toplevel: undefined,
9393
v8: false,
9494
validate: false,
9595
warnings: false,
@@ -104,8 +104,10 @@ function minify(files, options) {
104104
if (options.ie) set_shorthand("ie", options, [ "compress", "mangle", "output", "rename" ]);
105105
if (options.keep_fargs) set_shorthand("keep_fargs", options, [ "compress", "mangle", "rename" ]);
106106
if (options.keep_fnames) set_shorthand("keep_fnames", options, [ "compress", "mangle", "rename" ]);
107+
if (options.module === undefined && !options.ie) options.module = true;
107108
if (options.module) set_shorthand("module", options, [ "compress", "parse" ]);
108-
if (options.toplevel) set_shorthand("toplevel", options, [ "compress", "mangle", "rename" ]);
109+
if (options.toplevel === undefined && !options.expression && options.module) options.toplevel = true;
110+
if (options.toplevel !== undefined) set_shorthand("toplevel", options, [ "compress", "mangle", "rename" ]);
109111
if (options.v8) set_shorthand("v8", options, [ "mangle", "output", "rename" ]);
110112
if (options.webkit) set_shorthand("webkit", options, [ "compress", "mangle", "output", "rename" ]);
111113
var quoted_props;

lib/scope.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ SymbolDef.prototype = {
6868
var cache = this.global && options.cache && options.cache.props;
6969
if (cache && cache.has(this.name)) {
7070
this.mangled_name = cache.get(this.name);
71-
} else if (this.unmangleable(options)) {
72-
names_in_use(this.scope, options).set(this.name, true);
73-
} else {
71+
} else if (!this.unmangleable(options)) {
7472
var def = this.redefined();
7573
if (def) {
7674
this.mangled_name = def.mangled_name || def.name;
@@ -651,8 +649,12 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) {
651649
}, true);
652650
}
653651
var to_mangle = node.to_mangle = [];
654-
node.variables.each(function(def) {
655-
if (!defer_redef(def)) to_mangle.push(def);
652+
node.variables.each(function(def, name) {
653+
if (def.unmangleable(options)) {
654+
names_in_use(node, options).set(name, true);
655+
} else if (!defer_redef(def)) {
656+
to_mangle.push(def);
657+
}
656658
});
657659
descend();
658660
if (options.cache && node instanceof AST_Toplevel) {

test/compress/imports.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,34 @@ rename_mangle: {
167167
}
168168
}
169169

170+
mangle_export_import: {
171+
mangle = {
172+
toplevel: true,
173+
}
174+
input: {
175+
export let o = A;
176+
import { p as A } from "foo";
177+
}
178+
expect: {
179+
export let o = p;
180+
import { p } from "foo";
181+
}
182+
}
183+
184+
mangle_import_export: {
185+
mangle = {
186+
toplevel: true,
187+
}
188+
input: {
189+
import { p as A } from "foo";
190+
export let o = A;
191+
}
192+
expect: {
193+
import { p } from "foo";
194+
export let o = p;
195+
}
196+
}
197+
170198
keep_ref: {
171199
options = {
172200
reduce_vars: true,

0 commit comments

Comments
 (0)