Skip to content

Commit

Permalink
enable --module by default (#5738)
Browse files Browse the repository at this point in the history
- fix corner case in `mangle`
  • Loading branch information
alexlamsl authored Nov 21, 2022
1 parent 21aff99 commit 68d62a8
Show file tree
Hide file tree
Showing 17 changed files with 293 additions and 91 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ jobs:
- '-mb braces'
- '--ie -c'
- '-mc'
- '-p acorn --toplevel -mco spidermonkey'
- '--toplevel -mc passes=3,pure_getters,unsafe'
- '-p acorn -mco spidermonkey'
- '-mc passes=3,pure_getters,unsafe'
script:
- acorn.sh
- bootstrap.sh
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ a double dash to prevent input files being used as option arguments:
--keep-fnames Do not mangle/drop function names. Useful for
code relying on Function.prototype.name.
--module Process input as ES module (implies --toplevel)
--no-module Avoid optimizations which may alter runtime behavior
under prior versions of JavaScript.
--name-cache <file> File to hold mangled name mappings.
--self Build UglifyJS as a library (implies --wrap UglifyJS)
--source-map [options] Enable source map/specify source map options:
Expand Down Expand Up @@ -530,9 +532,9 @@ if (result.error) throw result.error;
- `mangle.properties` (default: `false`) — a subcategory of the mangle option.
Pass an object to specify custom [mangle property options](#mangle-properties-options).

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

- `nameCache` (default: `null`) — pass an empty object `{}` or a previously
used `nameCache` object if you wish to cache mangled variable and
Expand Down
10 changes: 8 additions & 2 deletions bin/uglifyjs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ function process_option(name, no_value) {
" --ie Support non-standard Internet Explorer.",
" --keep-fargs Do not mangle/drop function arguments.",
" --keep-fnames Do not mangle/drop function names. Useful for code relying on Function.prototype.name.",
" --module Process input as ES module (implies --toplevel)",
" --module Process input as ES module (implies --toplevel).",
" --no-module Process input with improved JavaScript compatibility.",
" --name-cache <file> File to hold mangled name mappings.",
" --rename Force symbol expansion.",
" --no-rename Disable symbol expansion.",
Expand Down Expand Up @@ -155,7 +156,6 @@ function process_option(name, no_value) {
case "expression":
case "ie":
case "ie8":
case "module":
case "timings":
case "toplevel":
case "v8":
Expand Down Expand Up @@ -201,6 +201,12 @@ function process_option(name, no_value) {
if (typeof options.mangle != "object") options.mangle = {};
options.mangle.properties = parse_js(read_value(), options.mangle.properties);
break;
case "module":
options.module = true;
break;
case "no-module":
options.module = false;
break;
case "name-cache":
nameCache = read_value(true);
options.nameCache = JSON.parse(read_file(nameCache, "{}"));
Expand Down
10 changes: 6 additions & 4 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -10234,22 +10234,24 @@ Compressor.prototype.compress = function(node) {
}

function varify(self, compressor) {
return all(self.definitions, function(defn) {
if (all(self.definitions, function(defn) {
return !defn.name.match_symbol(function(node) {
if (node instanceof AST_SymbolDeclaration) return !can_varify(compressor, node);
}, true);
}) ? to_var(self, compressor.find_parent(AST_Scope)) : self;
})) return to_var(self, compressor.find_parent(AST_Scope));
}

OPT(AST_Const, function(self, compressor) {
if (!compressor.option("varify")) return self;
var decl = varify(self, compressor);
if (decl) return decl;
if (can_letify(self, compressor, 0)) return to_let(self);
return varify(self, compressor);
return self;
});

OPT(AST_Let, function(self, compressor) {
if (!compressor.option("varify")) return self;
return varify(self, compressor);
return varify(self, compressor) || self;
});

function trim_optional_chain(node, compressor) {
Expand Down
8 changes: 5 additions & 3 deletions lib/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ function minify(files, options) {
keep_fargs: false,
keep_fnames: false,
mangle: {},
module: false,
module: undefined,
nameCache: null,
output: {},
parse: {},
rename: undefined,
sourceMap: false,
timings: false,
toplevel: !!(options && !options["expression"] && options["module"]),
toplevel: undefined,
v8: false,
validate: false,
warnings: false,
Expand All @@ -104,8 +104,10 @@ function minify(files, options) {
if (options.ie) set_shorthand("ie", options, [ "compress", "mangle", "output", "rename" ]);
if (options.keep_fargs) set_shorthand("keep_fargs", options, [ "compress", "mangle", "rename" ]);
if (options.keep_fnames) set_shorthand("keep_fnames", options, [ "compress", "mangle", "rename" ]);
if (options.module === undefined && !options.ie) options.module = true;
if (options.module) set_shorthand("module", options, [ "compress", "parse" ]);
if (options.toplevel) set_shorthand("toplevel", options, [ "compress", "mangle", "rename" ]);
if (options.toplevel === undefined && !options.expression && options.module) options.toplevel = true;
if (options.toplevel !== undefined) set_shorthand("toplevel", options, [ "compress", "mangle", "rename" ]);
if (options.v8) set_shorthand("v8", options, [ "mangle", "output", "rename" ]);
if (options.webkit) set_shorthand("webkit", options, [ "compress", "mangle", "output", "rename" ]);
var quoted_props;
Expand Down
12 changes: 7 additions & 5 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ SymbolDef.prototype = {
var cache = this.global && options.cache && options.cache.props;
if (cache && cache.has(this.name)) {
this.mangled_name = cache.get(this.name);
} else if (this.unmangleable(options)) {
names_in_use(this.scope, options).set(this.name, true);
} else {
} else if (!this.unmangleable(options)) {
var def = this.redefined();
if (def) {
this.mangled_name = def.mangled_name || def.name;
Expand Down Expand Up @@ -651,8 +649,12 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) {
}, true);
}
var to_mangle = node.to_mangle = [];
node.variables.each(function(def) {
if (!defer_redef(def)) to_mangle.push(def);
node.variables.each(function(def, name) {
if (def.unmangleable(options)) {
names_in_use(node, options).set(name, true);
} else if (!defer_redef(def)) {
to_mangle.push(def);
}
});
descend();
if (options.cache && node instanceof AST_Toplevel) {
Expand Down
28 changes: 28 additions & 0 deletions test/compress/imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,34 @@ rename_mangle: {
}
}

mangle_export_import: {
mangle = {
toplevel: true,
}
input: {
export let o = A;
import { p as A } from "foo";
}
expect: {
export let o = p;
import { p } from "foo";
}
}

mangle_import_export: {
mangle = {
toplevel: true,
}
input: {
import { p as A } from "foo";
export let o = A;
}
expect: {
import { p } from "foo";
export let o = p;
}
}

keep_ref: {
options = {
reduce_vars: true,
Expand Down
Loading

0 comments on commit 68d62a8

Please sign in to comment.