Skip to content

Commit 17c3ae6

Browse files
authoredDec 4, 2022
improve usability (#5758)
1 parent 0cbd5ea commit 17c3ae6

18 files changed

+38
-76
lines changed
 

‎.github/workflows/build.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ jobs:
99
fail-fast: false
1010
matrix:
1111
options:
12-
- '-mb braces'
1312
- '--ie -c'
14-
- '-mc'
13+
- '-mb braces'
14+
- '--toplevel -mc'
1515
- '-p acorn -mco spidermonkey'
1616
- '-mc passes=3,pure_getters,unsafe'
1717
script:

‎README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,8 @@ if (result.error) throw result.error;
533533
Pass an object to specify custom [mangle property options](#mangle-properties-options).
534534

535535
- `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.
536+
`"use strict";` and support for top-level `await`. When explicitly specified,
537+
also enables `toplevel`.
538538

539539
- `nameCache` (default: `null`) — pass an empty object `{}` or a previously
540540
used `nameCache` object if you wish to cache mangled variable and
@@ -752,7 +752,7 @@ to be `false` and all symbol names will be omitted.
752752
- `merge_vars` (default: `true`) — combine and reuse variables.
753753

754754
- `module` (default: `false`) — set to `true` if you wish to process input as
755-
ES module, i.e. implicit `"use strict";` alongside with `toplevel` enabled.
755+
ES module, i.e. implicit `"use strict";`.
756756

757757
- `negate_iife` (default: `true`) — negate "Immediately-Called Function Expressions"
758758
where the return value is discarded, to avoid the parentheses that the
@@ -927,9 +927,8 @@ can pass additional arguments that control the code output:
927927
regexps (affects directives with non-ascii characters becoming invalid)
928928

929929
- `beautify` (default: `true`) — whether to actually beautify the output.
930-
Passing `-b` will set this to true, but you might need to pass `-b` even
931-
when you want to generate minified code, in order to specify additional
932-
arguments, so you can use `-b beautify=false` to override it.
930+
Passing `-b` will set this to true. Use `-O` if you want to generate minified
931+
code and specify additional arguments.
933932

934933
- `braces` (default: `false`) — always insert braces in `if`, `for`,
935934
`do`, `while` or `with` statements, even if their body is a single

‎bin/uglifyjs

-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ if (typeof options.sourceMap == "object" && "base" in options.sourceMap) {
272272
if (specified["self"]) {
273273
if (paths.length) UglifyJS.AST_Node.warn("Ignoring input files since --self was passed");
274274
if (!options.wrap) options.wrap = "UglifyJS";
275-
if (!("toplevel" in options)) options.toplevel = false;
276275
paths = UglifyJS.FILES;
277276
} else if (paths.length) {
278277
paths = simple_glob(paths);

‎lib/compress.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function Compressor(options, false_by_default) {
9898
switches : !false_by_default,
9999
templates : !false_by_default,
100100
top_retain : null,
101-
toplevel : !!(options && !options["expression"] && (options["module"] || options["top_retain"])),
101+
toplevel : !!(options && !options["expression"] && options["top_retain"]),
102102
typeofs : !false_by_default,
103103
unsafe : false,
104104
unsafe_comps : false,

‎lib/minify.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function minify(files, options) {
8989
rename: undefined,
9090
sourceMap: false,
9191
timings: false,
92-
toplevel: undefined,
92+
toplevel: options && !options["expression"] && options["module"] ? true : undefined,
9393
v8: false,
9494
validate: false,
9595
warnings: false,
@@ -106,7 +106,6 @@ function minify(files, options) {
106106
if (options.keep_fnames) set_shorthand("keep_fnames", options, [ "compress", "mangle", "rename" ]);
107107
if (options.module === undefined && !options.ie) options.module = true;
108108
if (options.module) set_shorthand("module", options, [ "compress", "parse" ]);
109-
if (options.toplevel === undefined && !options.expression && options.module) options.toplevel = true;
110109
if (options.toplevel !== undefined) set_shorthand("toplevel", options, [ "compress", "mangle", "rename" ]);
111110
if (options.v8) set_shorthand("v8", options, [ "mangle", "output", "rename" ]);
112111
if (options.webkit) set_shorthand("webkit", options, [ "compress", "mangle", "output", "rename" ]);

‎test/compress/functions.js

+1
Original file line numberDiff line numberDiff line change
@@ -8663,6 +8663,7 @@ module_inline: {
86638663
inline: true,
86648664
module: true,
86658665
reduce_vars: true,
8666+
toplevel: true,
86668667
}
86678668
input: {
86688669
var a = f;

‎test/jetstream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ if (typeof phantom == "undefined") {
1414
args.splice(debug, 1);
1515
debug = true;
1616
}
17-
if (!args.length) args.push("-mcb", "beautify=false,webkit");
17+
if (!args.length) args.push("-mcO", "webkit");
1818
args.unshift("bin/uglifyjs");
1919
args.push("--validate", "--timings");
2020
var child_process = require("child_process");

‎test/mocha/cli.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ describe("bin/uglifyjs", function() {
10681068
]).join("\n");
10691069
exec(uglifyjscmd + " -mc", function(err, stdout) {
10701070
if (err) throw err;
1071-
assert.strictEqual(stdout, "console.log({p:25}.p+{p:121}.p+{p:1024}.p);\n");
1071+
assert.strictEqual(stdout, "console.log(function(){var p={p:25},n={p:121},o={p:1024};return p.p+n.p+o.p}());\n");
10721072
assert.strictEqual(run_code(stdout), run_code(code));
10731073
done();
10741074
}).stdin.end(code);

‎test/mocha/comments.js

-6
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ describe("comments", function() {
117117
beautify: true,
118118
comments: "all",
119119
},
120-
toplevel: false,
121120
});
122121
if (result.error) throw result.error;
123122
assert.strictEqual(result.code, [
@@ -377,7 +376,6 @@ describe("comments", function() {
377376
var result = UglifyJS.minify(js, {
378377
compress: { collapse_vars: false, reduce_vars: false },
379378
output: { comments: true },
380-
toplevel: false,
381379
});
382380
assert.strictEqual(result.code, 'function f(){/*c1*/var/*c2*/n=/*c3*/!1;return n}');
383381
});
@@ -386,7 +384,6 @@ describe("comments", function() {
386384
var result = UglifyJS.minify(js, {
387385
compress: { collapse_vars: false, reduce_vars: false },
388386
output: { comments: false },
389-
toplevel: false,
390387
});
391388
assert.strictEqual(result.code, 'function f(){var n=!1;return n}');
392389
});
@@ -461,15 +458,13 @@ describe("comments", function() {
461458
it("Should handle shebang and preamble correctly", function() {
462459
var code = UglifyJS.minify("#!/usr/bin/node\nvar x = 10;", {
463460
output: { preamble: "/* Build */" },
464-
toplevel: false,
465461
}).code;
466462
assert.strictEqual(code, "#!/usr/bin/node\n/* Build */\nvar x=10;");
467463
});
468464

469465
it("Should handle preamble without shebang correctly", function() {
470466
var code = UglifyJS.minify("var x = 10;", {
471467
output: { preamble: "/* Build */" },
472-
toplevel: false,
473468
}).code;
474469
assert.strictEqual(code, "/* Build */\nvar x=10;");
475470
});
@@ -483,7 +478,6 @@ describe("comments", function() {
483478
js += "x; }";
484479
var result = UglifyJS.minify(js, {
485480
mangle: false,
486-
toplevel: false,
487481
});
488482
assert.strictEqual(result.code, "function lots_of_comments(x){return 7-x}");
489483
});

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

-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ describe("Input file as map", function() {
88
};
99
var result = UglifyJS.minify(jsMap, {
1010
sourceMap: true,
11-
toplevel: false,
1211
});
1312
if (result.error) throw result.error;
1413
var map = JSON.parse(result.map);
@@ -31,7 +30,6 @@ describe("Input file as map", function() {
3130
];
3231
var result = UglifyJS.minify(jsSeq, {
3332
sourceMap: true,
34-
toplevel: false,
3533
});
3634
if (result.error) throw result.error;
3735
var map = JSON.parse(result.map);
@@ -47,7 +45,6 @@ describe("Input file as map", function() {
4745
sourceMap: {
4846
includeSources: true,
4947
},
50-
toplevel: false,
5148
});
5249
if (result.error) throw result.error;
5350
var map = JSON.parse(result.map);

‎test/mocha/minify.js

+1-14
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ 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, {
15-
module: false,
16-
});
14+
var result = UglifyJS.minify(js);
1715
if (result.error) throw result.error;
1816
assert.strictEqual(result.code, "function foo(n){return n?3:7}");
1917
});
@@ -48,9 +46,6 @@ describe("minify", function() {
4846
].forEach(function(file) {
4947
var code = read("test/input/issue-1242/" + file);
5048
var result = UglifyJS.minify(code, {
51-
compress: {
52-
toplevel: false,
53-
},
5449
mangle: {
5550
cache: cache,
5651
toplevel: true,
@@ -83,9 +78,6 @@ describe("minify", function() {
8378
].forEach(function(file) {
8479
var code = read("test/input/issue-1242/" + file);
8580
var result = UglifyJS.minify(code, {
86-
compress: {
87-
toplevel: false,
88-
},
8981
mangle: {
9082
toplevel: true,
9183
},
@@ -172,7 +164,6 @@ describe("minify", function() {
172164
output: {
173165
keep_quoted_props: true,
174166
},
175-
toplevel: false,
176167
});
177168
assert.strictEqual(result.code, 'var foo={"x":1,y:2,"z":3};');
178169
});
@@ -183,7 +174,6 @@ describe("minify", function() {
183174
keep_quoted_props: true,
184175
quote_style: 3,
185176
},
186-
toplevel: false,
187177
});
188178
assert.strictEqual(result.code, 'var foo={"x":1,y:2,\'z\':3};');
189179
});
@@ -194,7 +184,6 @@ describe("minify", function() {
194184
keep_quoted_props: false,
195185
quote_style: 3,
196186
},
197-
toplevel: false,
198187
});
199188
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};');
200189
});
@@ -248,7 +237,6 @@ describe("minify", function() {
248237
comments: "all",
249238
beautify: false,
250239
},
251-
toplevel: false,
252240
});
253241
var code = result.code;
254242
assert.strictEqual(code, "var a=function(){foo()}();");
@@ -328,7 +316,6 @@ describe("minify", function() {
328316
UglifyJS.minify(ast, {
329317
compress: {
330318
sequences: false,
331-
toplevel: false,
332319
},
333320
mangle: false,
334321
});

‎test/mocha/reduce.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ describe("test/reduce.js", function() {
333333
unsafe_math: true,
334334
},
335335
mangle: false,
336+
module: false,
336337
});
337338
if (result.error) throw result.error;
338339
assert.strictEqual(result.code, [
@@ -346,7 +347,8 @@ describe("test/reduce.js", function() {
346347
'// "compress": {',
347348
'// "unsafe_math": true',
348349
'// },',
349-
'// "mangle": false',
350+
'// "mangle": false,',
351+
'// "module": false',
350352
"// }",
351353
].join("\n"));
352354
});

‎test/mocha/sourcemaps.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ describe("sourcemaps", function() {
109109
"}",
110110
].join("\n"), {
111111
sourceMap: true,
112-
toplevel: false,
113112
});
114113
if (result.error) throw result.error;
115114
assert.strictEqual(result.code, "class A{static P=42;set#q(s){}}");
@@ -188,7 +187,6 @@ describe("sourcemaps", function() {
188187
sourceMap: {
189188
content: "inline",
190189
},
191-
toplevel: false,
192190
warnings: true,
193191
});
194192
assert.strictEqual(result.code, "var bar=function(bar){return bar};");
@@ -203,7 +201,6 @@ describe("sourcemaps", function() {
203201
content: "inline",
204202
url: "inline",
205203
},
206-
toplevel: false,
207204
warnings: true,
208205
});
209206
if (result.error) throw result.error;
@@ -301,17 +298,14 @@ describe("sourcemaps", function() {
301298
sourceMap: {
302299
url: "inline",
303300
},
304-
toplevel: false,
305301
});
306302
if (result.error) throw result.error;
307303
var code = result.code;
308304
assert.strictEqual(code, "var a=function(n){return n};\n" +
309305
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjAiXSwibmFtZXMiOlsiYSIsImZvbyJdLCJtYXBwaW5ncyI6IkFBQUEsSUFBSUEsRUFBSSxTQUFTQyxHQUFPLE9BQU9BLENBQUsifQ==");
310306
});
311307
it("Should not append source map to output js when sourceMapInline is not enabled", function() {
312-
var result = UglifyJS.minify("var a = function(foo) { return foo; };", {
313-
module: false,
314-
});
308+
var result = UglifyJS.minify("var a = function(foo) { return foo; };");
315309
if (result.error) throw result.error;
316310
var code = result.code;
317311
assert.strictEqual(code, "var a=function(n){return n};");
@@ -327,7 +321,6 @@ describe("sourcemaps", function() {
327321
sourceMap: {
328322
url: "inline",
329323
},
330-
toplevel: false,
331324
});
332325
if (result.error) throw result.error;
333326
assert.strictEqual(result.code, read("test/input/issue-505/output.js"));
@@ -342,7 +335,6 @@ describe("sourcemaps", function() {
342335
includeSources: true,
343336
url: "inline",
344337
},
345-
toplevel: false,
346338
});
347339
if (result.error) throw result.error;
348340
var map = JSON.parse(result.map);
@@ -357,7 +349,6 @@ describe("sourcemaps", function() {
357349
content: "inline",
358350
includeSources: true,
359351
},
360-
toplevel: false,
361352
});
362353
if (result.error) throw result.error;
363354
map = JSON.parse(result.map);

‎test/reduce.js

+7-14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Error.stackTraceLimit = Infinity;
2020
module.exports = function reduce_test(testcase, minify_options, reduce_options) {
2121
minify_options = minify_options || {};
2222
reduce_options = reduce_options || {};
23+
var parse_options = {
24+
module: minify_options.module || minify_options.module === undefined,
25+
}
2326
var print_options = {};
2427
[
2528
"ie",
@@ -539,9 +542,7 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
539542
var before_iterations, diff_error_message, passes = 3, testcase_ast;
540543
for (var pass = 1; pass <= passes; pass++) {
541544
if (before_iterations !== testcase) {
542-
testcase_ast = U.parse(testcase, {
543-
module: minify_options.module,
544-
});
545+
testcase_ast = U.parse(testcase, parse_options);
545546
if (diff_error_message === testcase) {
546547
// only difference detected is in error message, so expose that and try again
547548
testcase_ast.transform(new U.TreeTransformer(function(node, descend) {
@@ -563,9 +564,7 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
563564
testcase = code;
564565
differs = diff;
565566
} else {
566-
testcase_ast = U.parse(testcase, {
567-
module: minify_options.module,
568-
});
567+
testcase_ast = U.parse(testcase, parse_options);
569568
}
570569
}
571570
diff_error_message = null;
@@ -778,7 +777,7 @@ function run_code(code, toplevel, result_cache, timeout) {
778777
if (!value) {
779778
var start = Date.now();
780779
result_cache[key] = value = {
781-
result: sandbox.run_code(sandbox.patch_module_statements(code), toplevel, timeout),
780+
result: sandbox.run_code(code, toplevel, timeout),
782781
elapsed: Date.now() - start,
783782
};
784783
}
@@ -806,13 +805,7 @@ function compare_run_code(code, minify_options, result_cache, max_timeout) {
806805
};
807806

808807
function run(code, timeout) {
809-
if (minify_options.module) code = [
810-
'"use strict";',
811-
"(async()=>{",
812-
code,
813-
'})().catch(e=>process.on("exit",()=>{throw e}));',
814-
].join("\n");
815-
return run_code(code, toplevel, result_cache, timeout);
808+
return run_code(sandbox.patch_module_statements(code, minify_options.module), toplevel, result_cache, timeout);
816809
}
817810
}
818811

‎test/release/benchmark.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
require("./run")([
2-
"-b",
3-
"-b braces",
4-
"-m",
5-
"-mc passes=3",
2+
"-mb braces",
3+
"--toplevel -c",
64
"--no-module -mc",
75
"-mc passes=3,unsafe",
8-
"-mc keep_fargs=false,passes=3",
96
"-mc keep_fargs=false,passes=3,pure_getters,unsafe,unsafe_comps,unsafe_math,unsafe_proto",
107
].map(function(options) {
118
var args = options.split(/ /);

‎test/release/jetstream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ require("./run")([
44
].map(function(options) {
55
var args = options.split(/ /);
66
args.unshift("test/jetstream.js");
7-
args.push("-b", "beautify=false,webkit");
7+
args.push("-O", "webkit");
88
args.push("--no-module");
99
return args;
1010
}));

‎test/sandbox.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,16 @@ exports.same_stdout = semver.satisfies(process.version, "0.12") ? function(expec
4848
} : function(expected, actual) {
4949
return typeof expected == typeof actual && strip_func_ids(expected) == strip_func_ids(actual);
5050
};
51-
exports.patch_module_statements = function(code) {
51+
exports.patch_module_statements = function(code, module) {
52+
if (module || module === undefined && /\bawait\b/.test(code)) {
53+
code = [
54+
"(async()=>{",
55+
code,
56+
'})().catch(e=>process.on("exit",()=>{throw e}));',
57+
];
58+
if (module) code.unshift('"use strict";');
59+
code = code.join("\n");
60+
}
5261
var count = 0, has_default = "", imports = [], strict_mode = "";
5362
code = code.replace(/^\s*("|')use strict\1\s*;?/, function(match) {
5463
strict_mode = match;

‎test/ufuzz/index.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -2113,13 +2113,7 @@ if (require.main !== module) {
21132113
}
21142114

21152115
function run_code(code, toplevel, timeout) {
2116-
if (async && has_await) code = [
2117-
'"use strict";',
2118-
"(async()=>{",
2119-
code,
2120-
'})().catch(e=>process.on("exit",()=>{throw e}));',
2121-
].join("\n");
2122-
return sandbox.run_code(sandbox.patch_module_statements(code), toplevel, timeout);
2116+
return sandbox.run_code(sandbox.patch_module_statements(code, async && has_await), toplevel, timeout);
21232117
}
21242118

21252119
function writeln(stream, msg) {

0 commit comments

Comments
 (0)
Please sign in to comment.