Skip to content

Commit 68d62a8

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

17 files changed

+293
-91
lines changed
 

‎.github/workflows/build.yml

+2-2
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

+5-3
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

+8-2
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

+6-4
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

+5-3
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

+7-5
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

+28
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,

‎test/mocha/cli.js

+118-26
Original file line numberDiff line numberDiff line change
@@ -206,39 +206,69 @@ describe("bin/uglifyjs", function() {
206206
}, 1000);
207207
});
208208
it("Should work with --keep-fargs (mangle only)", function(done) {
209-
var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fargs -m';
209+
var command = [
210+
uglifyjscmd,
211+
"test/input/issue-1431/sample.js",
212+
"--keep-fargs",
213+
"--mangle",
214+
"--no-module",
215+
].join(" ");
210216
exec(command, function(err, stdout) {
211217
if (err) throw err;
212218
assert.strictEqual(stdout, "function f(x){return function(){function n(a){return a*a}return x(n)}}function g(op){return op(1)+op(2)}console.log(f(g)()==5);\n");
213219
done();
214220
});
215221
});
216222
it("Should work with --keep-fargs (mangle & compress)", function(done) {
217-
var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fargs -m -c';
223+
var command = [
224+
uglifyjscmd,
225+
"test/input/issue-1431/sample.js",
226+
"--keep-fargs",
227+
"--mangle",
228+
"--no-module",
229+
"--compress",
230+
].join(" ");
218231
exec(command, function(err, stdout) {
219232
if (err) throw err;
220233
assert.strictEqual(stdout, "function f(x){return function(){return x(function(a){return a*a})}}function g(op){return op(1)+op(2)}console.log(5==f(g)());\n");
221234
done();
222235
});
223236
});
224237
it("Should work with keep_fargs under mangler options", function(done) {
225-
var command = uglifyjscmd + ' test/input/issue-1431/sample.js -m keep_fargs=true';
238+
var command = [
239+
uglifyjscmd,
240+
"test/input/issue-1431/sample.js",
241+
"--mangle", "keep_fargs=true",
242+
"--no-module",
243+
].join(" ");
226244
exec(command, function(err, stdout) {
227245
if (err) throw err;
228246
assert.strictEqual(stdout, "function f(x){return function(){function n(a){return a*a}return x(n)}}function g(op){return op(1)+op(2)}console.log(f(g)()==5);\n");
229247
done();
230248
});
231249
});
232250
it("Should work with --keep-fnames (mangle only)", function(done) {
233-
var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m';
251+
var command = [
252+
uglifyjscmd,
253+
"test/input/issue-1431/sample.js",
254+
"--keep-fnames",
255+
"--mangle",
256+
].join(" ");
234257
exec(command, function(err, stdout) {
235258
if (err) throw err;
236259
assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(f(g)()==5);\n");
237260
done();
238261
});
239262
});
240263
it("Should work with --keep-fnames (mangle & compress)", function(done) {
241-
var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m -c';
264+
var command = [
265+
uglifyjscmd,
266+
"test/input/issue-1431/sample.js",
267+
"--keep-fnames",
268+
"--mangle",
269+
"--no-module",
270+
"--compress",
271+
].join(" ");
242272
exec(command, function(err, stdout) {
243273
if (err) throw err;
244274
assert.strictEqual(stdout, "function f(n){return function(){return n(function n(r){return r*r})}}function g(n){return n(1)+n(2)}console.log(5==f(g)());\n");
@@ -557,7 +587,11 @@ describe("bin/uglifyjs", function() {
557587
});
558588
});
559589
it("Should throw syntax error (delete x)", function(done) {
560-
var command = uglifyjscmd + " test/input/invalid/delete.js";
590+
var command = [
591+
uglifyjscmd,
592+
"test/input/invalid/delete.js",
593+
"--no-module",
594+
].join(" ");
561595
exec(command, function(err, stdout, stderr) {
562596
assert.ok(err);
563597
assert.strictEqual(stdout, "");
@@ -571,7 +605,11 @@ describe("bin/uglifyjs", function() {
571605
});
572606
});
573607
it("Should throw syntax error (function g(arguments))", function(done) {
574-
var command = uglifyjscmd + " test/input/invalid/function_1.js";
608+
var command = [
609+
uglifyjscmd,
610+
"test/input/invalid/function_1.js",
611+
"--no-module",
612+
].join(" ");
575613
exec(command, function(err, stdout, stderr) {
576614
assert.ok(err);
577615
assert.strictEqual(stdout, "");
@@ -585,7 +623,11 @@ describe("bin/uglifyjs", function() {
585623
});
586624
});
587625
it("Should throw syntax error (function eval())", function(done) {
588-
var command = uglifyjscmd + " test/input/invalid/function_2.js";
626+
var command = [
627+
uglifyjscmd,
628+
"test/input/invalid/function_2.js",
629+
"--no-module",
630+
].join(" ");
589631
exec(command, function(err, stdout, stderr) {
590632
assert.ok(err);
591633
assert.strictEqual(stdout, "");
@@ -599,7 +641,11 @@ describe("bin/uglifyjs", function() {
599641
});
600642
});
601643
it("Should throw syntax error (iife arguments())", function(done) {
602-
var command = uglifyjscmd + " test/input/invalid/function_3.js";
644+
var command = [
645+
uglifyjscmd,
646+
"test/input/invalid/function_3.js",
647+
"--no-module",
648+
].join(" ");
603649
exec(command, function(err, stdout, stderr) {
604650
assert.ok(err);
605651
assert.strictEqual(stdout, "");
@@ -613,7 +659,11 @@ describe("bin/uglifyjs", function() {
613659
});
614660
});
615661
it("Should throw syntax error (catch (eval))", function(done) {
616-
var command = uglifyjscmd + " test/input/invalid/try.js";
662+
var command = [
663+
uglifyjscmd,
664+
"test/input/invalid/try.js",
665+
"--no-module",
666+
].join(" ");
617667
exec(command, function(err, stdout, stderr) {
618668
assert.ok(err);
619669
assert.strictEqual(stdout, "");
@@ -627,7 +677,11 @@ describe("bin/uglifyjs", function() {
627677
});
628678
});
629679
it("Should throw syntax error (var eval)", function(done) {
630-
var command = uglifyjscmd + " test/input/invalid/var.js";
680+
var command = [
681+
uglifyjscmd,
682+
"test/input/invalid/var.js",
683+
"--no-module",
684+
].join(" ");
631685
exec(command, function(err, stdout, stderr) {
632686
assert.ok(err);
633687
assert.strictEqual(stdout, "");
@@ -641,7 +695,11 @@ describe("bin/uglifyjs", function() {
641695
});
642696
});
643697
it("Should throw syntax error (var { eval })", function(done) {
644-
var command = uglifyjscmd + " test/input/invalid/destructured_var.js";
698+
var command = [
699+
uglifyjscmd,
700+
"test/input/invalid/destructured_var.js",
701+
"--no-module",
702+
].join(" ");
645703
exec(command, function(err, stdout, stderr) {
646704
assert.ok(err);
647705
assert.strictEqual(stdout, "");
@@ -807,7 +865,8 @@ describe("bin/uglifyjs", function() {
807865
var command = [
808866
uglifyjscmd,
809867
"test/input/issue-2310/input.js",
810-
"-c",
868+
"--compress",
869+
"--no-module",
811870
"--source-map", "url=inline",
812871
].join(" ");
813872
exec(command, function(err, stdout, stderr) {
@@ -840,15 +899,25 @@ describe("bin/uglifyjs", function() {
840899
});
841900
});
842901
it("Should work with --mangle reserved=[]", function(done) {
843-
var command = uglifyjscmd + " test/input/issue-505/input.js -m reserved=[callback]";
902+
var command = [
903+
uglifyjscmd,
904+
"test/input/issue-505/input.js",
905+
"--mangle", "reserved=[callback]",
906+
"--no-module",
907+
].join(" ");
844908
exec(command, function(err, stdout) {
845909
if (err) throw err;
846910
assert.strictEqual(stdout, 'function test(callback){"aaaaaaaaaaaaaaaa";callback(err,data);callback(err,data)}\n');
847911
done();
848912
});
849913
});
850914
it("Should work with --mangle reserved=false", function(done) {
851-
var command = uglifyjscmd + " test/input/issue-505/input.js -m reserved=false";
915+
var command = [
916+
uglifyjscmd,
917+
"test/input/issue-505/input.js",
918+
"--mangle", "reserved=false",
919+
"--no-module",
920+
].join(" ");
852921
exec(command, function(err, stdout) {
853922
if (err) throw err;
854923
assert.strictEqual(stdout, 'function test(a){"aaaaaaaaaaaaaaaa";a(err,data);a(err,data)}\n');
@@ -865,7 +934,12 @@ describe("bin/uglifyjs", function() {
865934
});
866935
});
867936
it("Should work with mangle.properties.regex from --config-file", function(done) {
868-
var command = uglifyjscmd + " test/input/issue-3315/input.js --config-file test/input/issue-3315/config.json";
937+
var command = [
938+
uglifyjscmd,
939+
"test/input/issue-3315/input.js",
940+
"--config-file", "test/input/issue-3315/config.json",
941+
"--no-module",
942+
].join(" ");
869943
exec(command, function(err, stdout) {
870944
if (err) throw err;
871945
assert.strictEqual(stdout, 'function f(){"aaaaaaaaaa";var a={prop:1,t:2};return a.prop+a.t}\n');
@@ -882,31 +956,54 @@ describe("bin/uglifyjs", function() {
882956
});
883957
});
884958
it("Should work with explicit --rename", function(done) {
885-
var command = uglifyjscmd + " test/input/rename/input.js --rename";
959+
var command = [
960+
uglifyjscmd,
961+
"test/input/rename/input.js",
962+
"--no-module",
963+
"--rename",
964+
].join(" ");
886965
exec(command, function(err, stdout, stderr) {
887966
if (err) throw err;
888967
assert.strictEqual(stdout, "function f(a){return b(a);function b(c){return c+c}}\n");
889968
done();
890969
});
891970
});
892971
it("Should work with explicit --no-rename", function(done) {
893-
var command = uglifyjscmd + " test/input/rename/input.js -mc passes=2 --no-rename";
972+
var command = [
973+
uglifyjscmd,
974+
"test/input/rename/input.js",
975+
"--compress", "passes=2",
976+
"--mangle",
977+
"--no-module",
978+
"--no-rename",
979+
].join(" ");
894980
exec(command, function(err, stdout, stderr) {
895981
if (err) throw err;
896982
assert.strictEqual(stdout, "function f(n){return function(n){return n+n}(n)}\n");
897983
done();
898984
});
899985
});
900986
it("Should work with implicit --rename", function(done) {
901-
var command = uglifyjscmd + " test/input/rename/input.js -mc passes=2";
987+
var command = [
988+
uglifyjscmd,
989+
"test/input/rename/input.js",
990+
"--compress", "passes=2",
991+
"--mangle",
992+
"--no-module",
993+
].join(" ");
902994
exec(command, function(err, stdout, stderr) {
903995
if (err) throw err;
904996
assert.strictEqual(stdout, "function f(n){return n+n}\n");
905997
done();
906998
});
907999
});
9081000
it("Should work with implicit --no-rename", function(done) {
909-
var command = uglifyjscmd + " test/input/rename/input.js -c passes=2";
1001+
var command = [
1002+
uglifyjscmd,
1003+
"test/input/rename/input.js",
1004+
"--compress", "passes=2",
1005+
"--no-module",
1006+
].join(" ");
9101007
exec(command, function(err, stdout, stderr) {
9111008
if (err) throw err;
9121009
assert.strictEqual(stdout, "function f(x){return function(x){return x+x}(x)}\n");
@@ -971,12 +1068,7 @@ describe("bin/uglifyjs", function() {
9711068
]).join("\n");
9721069
exec(uglifyjscmd + " -mc", function(err, stdout) {
9731070
if (err) throw err;
974-
assert.strictEqual(stdout, [
975-
"console.log(function(){",
976-
"var p={p:25},n={p:121},o={p:1024};",
977-
"return p.p+n.p+o.p",
978-
"}());\n",
979-
].join(""));
1071+
assert.strictEqual(stdout, "console.log({p:25}.p+{p:121}.p+{p:1024}.p);\n");
9801072
assert.strictEqual(run_code(stdout), run_code(code));
9811073
done();
9821074
}).stdin.end(code);

‎test/mocha/comments.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ describe("comments", function() {
117117
beautify: true,
118118
comments: "all",
119119
},
120+
toplevel: false,
120121
});
121122
if (result.error) throw result.error;
122123
assert.strictEqual(result.code, [
@@ -376,6 +377,7 @@ describe("comments", function() {
376377
var result = UglifyJS.minify(js, {
377378
compress: { collapse_vars: false, reduce_vars: false },
378379
output: { comments: true },
380+
toplevel: false,
379381
});
380382
assert.strictEqual(result.code, 'function f(){/*c1*/var/*c2*/n=/*c3*/!1;return n}');
381383
});
@@ -384,6 +386,7 @@ describe("comments", function() {
384386
var result = UglifyJS.minify(js, {
385387
compress: { collapse_vars: false, reduce_vars: false },
386388
output: { comments: false },
389+
toplevel: false,
387390
});
388391
assert.strictEqual(result.code, 'function f(){var n=!1;return n}');
389392
});
@@ -458,13 +461,15 @@ describe("comments", function() {
458461
it("Should handle shebang and preamble correctly", function() {
459462
var code = UglifyJS.minify("#!/usr/bin/node\nvar x = 10;", {
460463
output: { preamble: "/* Build */" },
464+
toplevel: false,
461465
}).code;
462466
assert.strictEqual(code, "#!/usr/bin/node\n/* Build */\nvar x=10;");
463467
});
464468

465469
it("Should handle preamble without shebang correctly", function() {
466470
var code = UglifyJS.minify("var x = 10;", {
467471
output: { preamble: "/* Build */" },
472+
toplevel: false,
468473
}).code;
469474
assert.strictEqual(code, "/* Build */\nvar x=10;");
470475
});
@@ -476,7 +481,10 @@ describe("comments", function() {
476481
for (var i = 1; i <= 5000; ++i) js += "// " + i + "\n";
477482
for (; i <= 10000; ++i) js += "/* " + i + " */ /**/";
478483
js += "x; }";
479-
var result = UglifyJS.minify(js, { mangle: false });
484+
var result = UglifyJS.minify(js, {
485+
mangle: false,
486+
toplevel: false,
487+
});
480488
assert.strictEqual(result.code, "function lots_of_comments(x){return 7-x}");
481489
});
482490
});

‎test/mocha/directives.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,9 @@ describe("Directives", function() {
373373
'function f(){}'
374374
],
375375
].forEach(function(test) {
376-
var result = UglifyJS.minify(test[0]);
376+
var result = UglifyJS.minify(test[0], {
377+
module: false,
378+
});
377379
if (result.error) throw result.error;
378380
assert.strictEqual(result.code, test[1], test[0]);
379381
});

‎test/mocha/glob.js

+22-9
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,44 @@ var path = require("path");
55
describe("bin/uglifyjs with input file globs", function() {
66
var uglifyjscmd = '"' + process.argv[0] + '" bin/uglifyjs';
77
it("bin/uglifyjs with one input file extension glob.", function(done) {
8-
var command = uglifyjscmd + ' "test/input/issue-1242/foo.*" -cm';
9-
8+
var command = [
9+
uglifyjscmd,
10+
'"test/input/issue-1242/foo.*"',
11+
"--compress",
12+
"--mangle",
13+
"--no-module",
14+
].join(" ");
1015
exec(command, function(err, stdout) {
1116
if (err) throw err;
12-
1317
assert.strictEqual(stdout, 'var print=console.log.bind(console);function foo(o){print("Foo:",2*o)}\n');
1418
done();
1519
});
1620
});
1721
it("bin/uglifyjs with one input file name glob.", function(done) {
18-
var command = uglifyjscmd + ' "test/input/issue-1242/b*.es5" -cm';
19-
22+
var command = [
23+
uglifyjscmd,
24+
'"test/input/issue-1242/b*.es5"',
25+
"--compress",
26+
"--mangle",
27+
"--no-module",
28+
].join(" ");
2029
exec(command, function(err, stdout) {
2130
if (err) throw err;
22-
2331
assert.strictEqual(stdout, 'function bar(n){return 3*n}function baz(n){return n/2}\n');
2432
done();
2533
});
2634
});
2735
it("bin/uglifyjs with multiple input file globs.", function(done) {
28-
var command = uglifyjscmd + ' "test/input/issue-1242/???.es5" "test/input/issue-1242/*.js" -mc toplevel,passes=3';
29-
36+
var command = [
37+
uglifyjscmd,
38+
'"test/input/issue-1242/???.es5"',
39+
'"test/input/issue-1242/*.js"',
40+
"--compress", "toplevel,passes=3",
41+
"--mangle",
42+
"--no-module",
43+
].join(" ");
3044
exec(command, function(err, stdout) {
3145
if (err) throw err;
32-
3346
assert.strictEqual(stdout, 'var print=console.log.bind(console);print("qux",9,6),print("Foo:",22);\n');
3447
done();
3548
});

‎test/mocha/minify-file-map.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ describe("Input file as map", function() {
66
var jsMap = {
77
'/scripts/foo.js': 'var foo = {"x": 1, y: 2, \'z\': 3};'
88
};
9-
var result = UglifyJS.minify(jsMap, {sourceMap: true});
10-
9+
var result = UglifyJS.minify(jsMap, {
10+
sourceMap: true,
11+
toplevel: false,
12+
});
13+
if (result.error) throw result.error;
1114
var map = JSON.parse(result.map);
1215
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};');
1316
assert.deepEqual(map.sources, ['/scripts/foo.js']);
@@ -26,8 +29,11 @@ describe("Input file as map", function() {
2629
'var foo = {"x": 1, y: 2, \'z\': 3};',
2730
'var bar = 15;'
2831
];
29-
var result = UglifyJS.minify(jsSeq, {sourceMap: true});
30-
32+
var result = UglifyJS.minify(jsSeq, {
33+
sourceMap: true,
34+
toplevel: false,
35+
});
36+
if (result.error) throw result.error;
3137
var map = JSON.parse(result.map);
3238
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3},bar=15;');
3339
assert.deepEqual(map.sources, ['0', '1']);
@@ -37,8 +43,13 @@ describe("Input file as map", function() {
3743
var jsMap = {
3844
'/scripts/foo.js': 'var foo = {"x": 1, y: 2, \'z\': 3};'
3945
};
40-
var result = UglifyJS.minify(jsMap, {sourceMap: {includeSources: true}});
41-
46+
var result = UglifyJS.minify(jsMap, {
47+
sourceMap: {
48+
includeSources: true,
49+
},
50+
toplevel: false,
51+
});
52+
if (result.error) throw result.error;
4253
var map = JSON.parse(result.map);
4354
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};');
4455
assert.deepEqual(map.sourcesContent, ['var foo = {"x": 1, y: 2, \'z\': 3};']);

‎test/mocha/minify.js

+32-16
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ function read(path) {
1111
describe("minify", function() {
1212
it("Should test basic sanity of minify with default options", function() {
1313
var js = "function foo(bar) { if (bar) return 3; else return 7; var u = not_called(); }";
14-
var result = UglifyJS.minify(js);
14+
var result = UglifyJS.minify(js, {
15+
module: false,
16+
});
1517
if (result.error) throw result.error;
1618
assert.strictEqual(result.code, "function foo(n){return n?3:7}");
1719
});
@@ -46,10 +48,13 @@ describe("minify", function() {
4648
].forEach(function(file) {
4749
var code = read("test/input/issue-1242/" + file);
4850
var result = UglifyJS.minify(code, {
51+
compress: {
52+
toplevel: false,
53+
},
4954
mangle: {
5055
cache: cache,
51-
toplevel: true
52-
}
56+
toplevel: true,
57+
},
5358
});
5459
if (result.error) throw result.error;
5560
original += code;
@@ -78,10 +83,13 @@ describe("minify", function() {
7883
].forEach(function(file) {
7984
var code = read("test/input/issue-1242/" + file);
8085
var result = UglifyJS.minify(code, {
86+
compress: {
87+
toplevel: false,
88+
},
8189
mangle: {
82-
toplevel: true
90+
toplevel: true,
8391
},
84-
nameCache: cache
92+
nameCache: cache,
8593
});
8694
if (result.error) throw result.error;
8795
original += code;
@@ -162,26 +170,32 @@ describe("minify", function() {
162170
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
163171
var result = UglifyJS.minify(js, {
164172
output: {
165-
keep_quoted_props: true
166-
}});
173+
keep_quoted_props: true,
174+
},
175+
toplevel: false,
176+
});
167177
assert.strictEqual(result.code, 'var foo={"x":1,y:2,"z":3};');
168178
});
169179
it("Should preserve quote styles when quote_style is 3", function() {
170180
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
171181
var result = UglifyJS.minify(js, {
172182
output: {
173183
keep_quoted_props: true,
174-
quote_style: 3
175-
}});
184+
quote_style: 3,
185+
},
186+
toplevel: false,
187+
});
176188
assert.strictEqual(result.code, 'var foo={"x":1,y:2,\'z\':3};');
177189
});
178190
it("Should not preserve quotes in object literals when disabled", function() {
179191
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
180192
var result = UglifyJS.minify(js, {
181193
output: {
182194
keep_quoted_props: false,
183-
quote_style: 3
184-
}});
195+
quote_style: 3,
196+
},
197+
toplevel: false,
198+
});
185199
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};');
186200
});
187201
});
@@ -223,7 +237,7 @@ describe("minify", function() {
223237
output: {
224238
comments: "all",
225239
beautify: false,
226-
}
240+
},
227241
});
228242
var code = result.code;
229243
assert.strictEqual(code, "// comment1 comment2\nbar();");
@@ -233,7 +247,8 @@ describe("minify", function() {
233247
output: {
234248
comments: "all",
235249
beautify: false,
236-
}
250+
},
251+
toplevel: false,
237252
});
238253
var code = result.code;
239254
assert.strictEqual(code, "var a=function(){foo()}();");
@@ -301,7 +316,7 @@ describe("minify", function() {
301316
compress: false,
302317
mangle: false,
303318
output: {
304-
ast: true
319+
ast: true,
305320
},
306321
}).ast;
307322
assert.strictEqual(ast.TYPE, "Toplevel");
@@ -312,9 +327,10 @@ describe("minify", function() {
312327
var stat = ast.body[0].body[0];
313328
UglifyJS.minify(ast, {
314329
compress: {
315-
sequences: false
330+
sequences: false,
331+
toplevel: false,
316332
},
317-
mangle: false
333+
mangle: false,
318334
});
319335
assert.ok(stat.body);
320336
assert.strictEqual(stat.print_to_string(), "a=x()");

‎test/mocha/sourcemaps.js

+17-8
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ describe("sourcemaps", function() {
109109
"}",
110110
].join("\n"), {
111111
sourceMap: true,
112+
toplevel: false,
112113
});
113114
if (result.error) throw result.error;
114115
assert.strictEqual(result.code, "class A{static P=42;set#q(s){}}");
@@ -187,6 +188,7 @@ describe("sourcemaps", function() {
187188
sourceMap: {
188189
content: "inline",
189190
},
191+
toplevel: false,
190192
warnings: true,
191193
});
192194
assert.strictEqual(result.code, "var bar=function(bar){return bar};");
@@ -201,6 +203,7 @@ describe("sourcemaps", function() {
201203
content: "inline",
202204
url: "inline",
203205
},
206+
toplevel: false,
204207
warnings: true,
205208
});
206209
if (result.error) throw result.error;
@@ -296,16 +299,19 @@ describe("sourcemaps", function() {
296299
it("Should append source map to output js when sourceMapInline is enabled", function() {
297300
var result = UglifyJS.minify('var a = function(foo) { return foo; };', {
298301
sourceMap: {
299-
url: "inline"
300-
}
302+
url: "inline",
303+
},
304+
toplevel: false,
301305
});
302306
if (result.error) throw result.error;
303307
var code = result.code;
304308
assert.strictEqual(code, "var a=function(n){return n};\n" +
305309
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjAiXSwibmFtZXMiOlsiYSIsImZvbyJdLCJtYXBwaW5ncyI6IkFBQUEsSUFBSUEsRUFBSSxTQUFTQyxHQUFPLE9BQU9BLENBQUsifQ==");
306310
});
307311
it("Should not append source map to output js when sourceMapInline is not enabled", function() {
308-
var result = UglifyJS.minify('var a = function(foo) { return foo; };');
312+
var result = UglifyJS.minify("var a = function(foo) { return foo; };", {
313+
module: false,
314+
});
309315
if (result.error) throw result.error;
310316
var code = result.code;
311317
assert.strictEqual(code, "var a=function(n){return n};");
@@ -316,11 +322,12 @@ describe("sourcemaps", function() {
316322
directives: false,
317323
},
318324
output: {
319-
max_line_len: 20
325+
max_line_len: 20,
320326
},
321327
sourceMap: {
322-
url: "inline"
323-
}
328+
url: "inline",
329+
},
330+
toplevel: false,
324331
});
325332
if (result.error) throw result.error;
326333
assert.strictEqual(result.code, read("test/input/issue-505/output.js"));
@@ -334,7 +341,8 @@ describe("sourcemaps", function() {
334341
sourceMap: {
335342
includeSources: true,
336343
url: "inline",
337-
}
344+
},
345+
toplevel: false,
338346
});
339347
if (result.error) throw result.error;
340348
var map = JSON.parse(result.map);
@@ -348,7 +356,8 @@ describe("sourcemaps", function() {
348356
sourceMap: {
349357
content: "inline",
350358
includeSources: true,
351-
}
359+
},
360+
toplevel: false,
352361
});
353362
if (result.error) throw result.error;
354363
map = JSON.parse(result.map);

‎test/release/benchmark.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ require("./run")([
33
"-b braces",
44
"-m",
55
"-mc passes=3",
6-
"-mc passes=3,toplevel",
6+
"--no-module -mc",
77
"-mc passes=3,unsafe",
88
"-mc keep_fargs=false,passes=3",
99
"-mc keep_fargs=false,passes=3,pure_getters,unsafe,unsafe_comps,unsafe_math,unsafe_proto",

‎test/release/jetstream.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ require("./run")([
55
var args = options.split(/ /);
66
args.unshift("test/jetstream.js");
77
args.push("-b", "beautify=false,webkit");
8+
args.push("--no-module");
89
return args;
910
}));

‎test/ufuzz/options.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,27 @@
22
{
33
"compress": false,
44
"mangle": false,
5+
"module": false,
56
"output": {
67
"beautify": true,
78
"braces": true
89
},
910
"rename": true
1011
},
1112
{
12-
"compress": false
13+
"compress": false,
14+
"module": false
1315
},
1416
{
15-
"mangle": false
17+
"mangle": false,
18+
"module": false
19+
},
20+
{
21+
"module": false
1622
},
17-
{},
1823
{
1924
"ie": true,
25+
"module": false,
2026
"toplevel": true
2127
},
2228
{
@@ -28,6 +34,7 @@
2834
},
2935
"keep_fargs": true,
3036
"keep_fnames": true,
37+
"module": false,
3138
"toplevel": true
3239
},
3340
{
@@ -39,6 +46,7 @@
3946
"unsafe_math": true,
4047
"unsafe_proto": true,
4148
"unsafe_regexp": true
42-
}
49+
},
50+
"module": false
4351
}
4452
]

0 commit comments

Comments
 (0)
Please sign in to comment.