enable --module by default (#5738)

- fix corner case in `mangle`
This commit is contained in:
Alex Lam S.L
2022-11-21 18:25:34 +00:00
committed by GitHub
parent 21aff996a5
commit 68d62a8a31
17 changed files with 294 additions and 92 deletions

View File

@@ -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,

View File

@@ -206,7 +206,13 @@ describe("bin/uglifyjs", function() {
}, 1000);
});
it("Should work with --keep-fargs (mangle only)", function(done) {
var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fargs -m';
var command = [
uglifyjscmd,
"test/input/issue-1431/sample.js",
"--keep-fargs",
"--mangle",
"--no-module",
].join(" ");
exec(command, function(err, stdout) {
if (err) throw err;
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");
@@ -214,7 +220,14 @@ describe("bin/uglifyjs", function() {
});
});
it("Should work with --keep-fargs (mangle & compress)", function(done) {
var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fargs -m -c';
var command = [
uglifyjscmd,
"test/input/issue-1431/sample.js",
"--keep-fargs",
"--mangle",
"--no-module",
"--compress",
].join(" ");
exec(command, function(err, stdout) {
if (err) throw err;
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");
@@ -222,7 +235,12 @@ describe("bin/uglifyjs", function() {
});
});
it("Should work with keep_fargs under mangler options", function(done) {
var command = uglifyjscmd + ' test/input/issue-1431/sample.js -m keep_fargs=true';
var command = [
uglifyjscmd,
"test/input/issue-1431/sample.js",
"--mangle", "keep_fargs=true",
"--no-module",
].join(" ");
exec(command, function(err, stdout) {
if (err) throw err;
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");
@@ -230,7 +248,12 @@ describe("bin/uglifyjs", function() {
});
});
it("Should work with --keep-fnames (mangle only)", function(done) {
var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m';
var command = [
uglifyjscmd,
"test/input/issue-1431/sample.js",
"--keep-fnames",
"--mangle",
].join(" ");
exec(command, function(err, stdout) {
if (err) throw err;
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");
@@ -238,7 +261,14 @@ describe("bin/uglifyjs", function() {
});
});
it("Should work with --keep-fnames (mangle & compress)", function(done) {
var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m -c';
var command = [
uglifyjscmd,
"test/input/issue-1431/sample.js",
"--keep-fnames",
"--mangle",
"--no-module",
"--compress",
].join(" ");
exec(command, function(err, stdout) {
if (err) throw err;
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() {
});
});
it("Should throw syntax error (delete x)", function(done) {
var command = uglifyjscmd + " test/input/invalid/delete.js";
var command = [
uglifyjscmd,
"test/input/invalid/delete.js",
"--no-module",
].join(" ");
exec(command, function(err, stdout, stderr) {
assert.ok(err);
assert.strictEqual(stdout, "");
@@ -571,7 +605,11 @@ describe("bin/uglifyjs", function() {
});
});
it("Should throw syntax error (function g(arguments))", function(done) {
var command = uglifyjscmd + " test/input/invalid/function_1.js";
var command = [
uglifyjscmd,
"test/input/invalid/function_1.js",
"--no-module",
].join(" ");
exec(command, function(err, stdout, stderr) {
assert.ok(err);
assert.strictEqual(stdout, "");
@@ -585,7 +623,11 @@ describe("bin/uglifyjs", function() {
});
});
it("Should throw syntax error (function eval())", function(done) {
var command = uglifyjscmd + " test/input/invalid/function_2.js";
var command = [
uglifyjscmd,
"test/input/invalid/function_2.js",
"--no-module",
].join(" ");
exec(command, function(err, stdout, stderr) {
assert.ok(err);
assert.strictEqual(stdout, "");
@@ -599,7 +641,11 @@ describe("bin/uglifyjs", function() {
});
});
it("Should throw syntax error (iife arguments())", function(done) {
var command = uglifyjscmd + " test/input/invalid/function_3.js";
var command = [
uglifyjscmd,
"test/input/invalid/function_3.js",
"--no-module",
].join(" ");
exec(command, function(err, stdout, stderr) {
assert.ok(err);
assert.strictEqual(stdout, "");
@@ -613,7 +659,11 @@ describe("bin/uglifyjs", function() {
});
});
it("Should throw syntax error (catch (eval))", function(done) {
var command = uglifyjscmd + " test/input/invalid/try.js";
var command = [
uglifyjscmd,
"test/input/invalid/try.js",
"--no-module",
].join(" ");
exec(command, function(err, stdout, stderr) {
assert.ok(err);
assert.strictEqual(stdout, "");
@@ -627,7 +677,11 @@ describe("bin/uglifyjs", function() {
});
});
it("Should throw syntax error (var eval)", function(done) {
var command = uglifyjscmd + " test/input/invalid/var.js";
var command = [
uglifyjscmd,
"test/input/invalid/var.js",
"--no-module",
].join(" ");
exec(command, function(err, stdout, stderr) {
assert.ok(err);
assert.strictEqual(stdout, "");
@@ -641,7 +695,11 @@ describe("bin/uglifyjs", function() {
});
});
it("Should throw syntax error (var { eval })", function(done) {
var command = uglifyjscmd + " test/input/invalid/destructured_var.js";
var command = [
uglifyjscmd,
"test/input/invalid/destructured_var.js",
"--no-module",
].join(" ");
exec(command, function(err, stdout, stderr) {
assert.ok(err);
assert.strictEqual(stdout, "");
@@ -807,7 +865,8 @@ describe("bin/uglifyjs", function() {
var command = [
uglifyjscmd,
"test/input/issue-2310/input.js",
"-c",
"--compress",
"--no-module",
"--source-map", "url=inline",
].join(" ");
exec(command, function(err, stdout, stderr) {
@@ -840,7 +899,12 @@ describe("bin/uglifyjs", function() {
});
});
it("Should work with --mangle reserved=[]", function(done) {
var command = uglifyjscmd + " test/input/issue-505/input.js -m reserved=[callback]";
var command = [
uglifyjscmd,
"test/input/issue-505/input.js",
"--mangle", "reserved=[callback]",
"--no-module",
].join(" ");
exec(command, function(err, stdout) {
if (err) throw err;
assert.strictEqual(stdout, 'function test(callback){"aaaaaaaaaaaaaaaa";callback(err,data);callback(err,data)}\n');
@@ -848,7 +912,12 @@ describe("bin/uglifyjs", function() {
});
});
it("Should work with --mangle reserved=false", function(done) {
var command = uglifyjscmd + " test/input/issue-505/input.js -m reserved=false";
var command = [
uglifyjscmd,
"test/input/issue-505/input.js",
"--mangle", "reserved=false",
"--no-module",
].join(" ");
exec(command, function(err, stdout) {
if (err) throw err;
assert.strictEqual(stdout, 'function test(a){"aaaaaaaaaaaaaaaa";a(err,data);a(err,data)}\n');
@@ -865,7 +934,12 @@ describe("bin/uglifyjs", function() {
});
});
it("Should work with mangle.properties.regex from --config-file", function(done) {
var command = uglifyjscmd + " test/input/issue-3315/input.js --config-file test/input/issue-3315/config.json";
var command = [
uglifyjscmd,
"test/input/issue-3315/input.js",
"--config-file", "test/input/issue-3315/config.json",
"--no-module",
].join(" ");
exec(command, function(err, stdout) {
if (err) throw err;
assert.strictEqual(stdout, 'function f(){"aaaaaaaaaa";var a={prop:1,t:2};return a.prop+a.t}\n');
@@ -882,7 +956,12 @@ describe("bin/uglifyjs", function() {
});
});
it("Should work with explicit --rename", function(done) {
var command = uglifyjscmd + " test/input/rename/input.js --rename";
var command = [
uglifyjscmd,
"test/input/rename/input.js",
"--no-module",
"--rename",
].join(" ");
exec(command, function(err, stdout, stderr) {
if (err) throw err;
assert.strictEqual(stdout, "function f(a){return b(a);function b(c){return c+c}}\n");
@@ -890,7 +969,14 @@ describe("bin/uglifyjs", function() {
});
});
it("Should work with explicit --no-rename", function(done) {
var command = uglifyjscmd + " test/input/rename/input.js -mc passes=2 --no-rename";
var command = [
uglifyjscmd,
"test/input/rename/input.js",
"--compress", "passes=2",
"--mangle",
"--no-module",
"--no-rename",
].join(" ");
exec(command, function(err, stdout, stderr) {
if (err) throw err;
assert.strictEqual(stdout, "function f(n){return function(n){return n+n}(n)}\n");
@@ -898,7 +984,13 @@ describe("bin/uglifyjs", function() {
});
});
it("Should work with implicit --rename", function(done) {
var command = uglifyjscmd + " test/input/rename/input.js -mc passes=2";
var command = [
uglifyjscmd,
"test/input/rename/input.js",
"--compress", "passes=2",
"--mangle",
"--no-module",
].join(" ");
exec(command, function(err, stdout, stderr) {
if (err) throw err;
assert.strictEqual(stdout, "function f(n){return n+n}\n");
@@ -906,7 +998,12 @@ describe("bin/uglifyjs", function() {
});
});
it("Should work with implicit --no-rename", function(done) {
var command = uglifyjscmd + " test/input/rename/input.js -c passes=2";
var command = [
uglifyjscmd,
"test/input/rename/input.js",
"--compress", "passes=2",
"--no-module",
].join(" ");
exec(command, function(err, stdout, stderr) {
if (err) throw err;
assert.strictEqual(stdout, "function f(x){return function(x){return x+x}(x)}\n");
@@ -971,12 +1068,7 @@ describe("bin/uglifyjs", function() {
]).join("\n");
exec(uglifyjscmd + " -mc", function(err, stdout) {
if (err) throw err;
assert.strictEqual(stdout, [
"console.log(function(){",
"var p={p:25},n={p:121},o={p:1024};",
"return p.p+n.p+o.p",
"}());\n",
].join(""));
assert.strictEqual(stdout, "console.log({p:25}.p+{p:121}.p+{p:1024}.p);\n");
assert.strictEqual(run_code(stdout), run_code(code));
done();
}).stdin.end(code);

View File

@@ -117,6 +117,7 @@ describe("comments", function() {
beautify: true,
comments: "all",
},
toplevel: false,
});
if (result.error) throw result.error;
assert.strictEqual(result.code, [
@@ -376,6 +377,7 @@ describe("comments", function() {
var result = UglifyJS.minify(js, {
compress: { collapse_vars: false, reduce_vars: false },
output: { comments: true },
toplevel: false,
});
assert.strictEqual(result.code, 'function f(){/*c1*/var/*c2*/n=/*c3*/!1;return n}');
});
@@ -384,6 +386,7 @@ describe("comments", function() {
var result = UglifyJS.minify(js, {
compress: { collapse_vars: false, reduce_vars: false },
output: { comments: false },
toplevel: false,
});
assert.strictEqual(result.code, 'function f(){var n=!1;return n}');
});
@@ -458,6 +461,7 @@ describe("comments", function() {
it("Should handle shebang and preamble correctly", function() {
var code = UglifyJS.minify("#!/usr/bin/node\nvar x = 10;", {
output: { preamble: "/* Build */" },
toplevel: false,
}).code;
assert.strictEqual(code, "#!/usr/bin/node\n/* Build */\nvar x=10;");
});
@@ -465,6 +469,7 @@ describe("comments", function() {
it("Should handle preamble without shebang correctly", function() {
var code = UglifyJS.minify("var x = 10;", {
output: { preamble: "/* Build */" },
toplevel: false,
}).code;
assert.strictEqual(code, "/* Build */\nvar x=10;");
});
@@ -476,7 +481,10 @@ describe("comments", function() {
for (var i = 1; i <= 5000; ++i) js += "// " + i + "\n";
for (; i <= 10000; ++i) js += "/* " + i + " */ /**/";
js += "x; }";
var result = UglifyJS.minify(js, { mangle: false });
var result = UglifyJS.minify(js, {
mangle: false,
toplevel: false,
});
assert.strictEqual(result.code, "function lots_of_comments(x){return 7-x}");
});
});

View File

@@ -373,7 +373,9 @@ describe("Directives", function() {
'function f(){}'
],
].forEach(function(test) {
var result = UglifyJS.minify(test[0]);
var result = UglifyJS.minify(test[0], {
module: false,
});
if (result.error) throw result.error;
assert.strictEqual(result.code, test[1], test[0]);
});

View File

@@ -5,31 +5,44 @@ var path = require("path");
describe("bin/uglifyjs with input file globs", function() {
var uglifyjscmd = '"' + process.argv[0] + '" bin/uglifyjs';
it("bin/uglifyjs with one input file extension glob.", function(done) {
var command = uglifyjscmd + ' "test/input/issue-1242/foo.*" -cm';
var command = [
uglifyjscmd,
'"test/input/issue-1242/foo.*"',
"--compress",
"--mangle",
"--no-module",
].join(" ");
exec(command, function(err, stdout) {
if (err) throw err;
assert.strictEqual(stdout, 'var print=console.log.bind(console);function foo(o){print("Foo:",2*o)}\n');
done();
});
});
it("bin/uglifyjs with one input file name glob.", function(done) {
var command = uglifyjscmd + ' "test/input/issue-1242/b*.es5" -cm';
var command = [
uglifyjscmd,
'"test/input/issue-1242/b*.es5"',
"--compress",
"--mangle",
"--no-module",
].join(" ");
exec(command, function(err, stdout) {
if (err) throw err;
assert.strictEqual(stdout, 'function bar(n){return 3*n}function baz(n){return n/2}\n');
done();
});
});
it("bin/uglifyjs with multiple input file globs.", function(done) {
var command = uglifyjscmd + ' "test/input/issue-1242/???.es5" "test/input/issue-1242/*.js" -mc toplevel,passes=3';
var command = [
uglifyjscmd,
'"test/input/issue-1242/???.es5"',
'"test/input/issue-1242/*.js"',
"--compress", "toplevel,passes=3",
"--mangle",
"--no-module",
].join(" ");
exec(command, function(err, stdout) {
if (err) throw err;
assert.strictEqual(stdout, 'var print=console.log.bind(console);print("qux",9,6),print("Foo:",22);\n');
done();
});

View File

@@ -6,8 +6,11 @@ describe("Input file as map", function() {
var jsMap = {
'/scripts/foo.js': 'var foo = {"x": 1, y: 2, \'z\': 3};'
};
var result = UglifyJS.minify(jsMap, {sourceMap: true});
var result = UglifyJS.minify(jsMap, {
sourceMap: true,
toplevel: false,
});
if (result.error) throw result.error;
var map = JSON.parse(result.map);
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};');
assert.deepEqual(map.sources, ['/scripts/foo.js']);
@@ -26,8 +29,11 @@ describe("Input file as map", function() {
'var foo = {"x": 1, y: 2, \'z\': 3};',
'var bar = 15;'
];
var result = UglifyJS.minify(jsSeq, {sourceMap: true});
var result = UglifyJS.minify(jsSeq, {
sourceMap: true,
toplevel: false,
});
if (result.error) throw result.error;
var map = JSON.parse(result.map);
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3},bar=15;');
assert.deepEqual(map.sources, ['0', '1']);
@@ -37,8 +43,13 @@ describe("Input file as map", function() {
var jsMap = {
'/scripts/foo.js': 'var foo = {"x": 1, y: 2, \'z\': 3};'
};
var result = UglifyJS.minify(jsMap, {sourceMap: {includeSources: true}});
var result = UglifyJS.minify(jsMap, {
sourceMap: {
includeSources: true,
},
toplevel: false,
});
if (result.error) throw result.error;
var map = JSON.parse(result.map);
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};');
assert.deepEqual(map.sourcesContent, ['var foo = {"x": 1, y: 2, \'z\': 3};']);

View File

@@ -11,7 +11,9 @@ function read(path) {
describe("minify", function() {
it("Should test basic sanity of minify with default options", function() {
var js = "function foo(bar) { if (bar) return 3; else return 7; var u = not_called(); }";
var result = UglifyJS.minify(js);
var result = UglifyJS.minify(js, {
module: false,
});
if (result.error) throw result.error;
assert.strictEqual(result.code, "function foo(n){return n?3:7}");
});
@@ -46,10 +48,13 @@ describe("minify", function() {
].forEach(function(file) {
var code = read("test/input/issue-1242/" + file);
var result = UglifyJS.minify(code, {
compress: {
toplevel: false,
},
mangle: {
cache: cache,
toplevel: true
}
toplevel: true,
},
});
if (result.error) throw result.error;
original += code;
@@ -78,10 +83,13 @@ describe("minify", function() {
].forEach(function(file) {
var code = read("test/input/issue-1242/" + file);
var result = UglifyJS.minify(code, {
mangle: {
toplevel: true
compress: {
toplevel: false,
},
nameCache: cache
mangle: {
toplevel: true,
},
nameCache: cache,
});
if (result.error) throw result.error;
original += code;
@@ -162,8 +170,10 @@ describe("minify", function() {
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
var result = UglifyJS.minify(js, {
output: {
keep_quoted_props: true
}});
keep_quoted_props: true,
},
toplevel: false,
});
assert.strictEqual(result.code, 'var foo={"x":1,y:2,"z":3};');
});
it("Should preserve quote styles when quote_style is 3", function() {
@@ -171,8 +181,10 @@ describe("minify", function() {
var result = UglifyJS.minify(js, {
output: {
keep_quoted_props: true,
quote_style: 3
}});
quote_style: 3,
},
toplevel: false,
});
assert.strictEqual(result.code, 'var foo={"x":1,y:2,\'z\':3};');
});
it("Should not preserve quotes in object literals when disabled", function() {
@@ -180,8 +192,10 @@ describe("minify", function() {
var result = UglifyJS.minify(js, {
output: {
keep_quoted_props: false,
quote_style: 3
}});
quote_style: 3,
},
toplevel: false,
});
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};');
});
});
@@ -223,7 +237,7 @@ describe("minify", function() {
output: {
comments: "all",
beautify: false,
}
},
});
var code = result.code;
assert.strictEqual(code, "// comment1 comment2\nbar();");
@@ -233,7 +247,8 @@ describe("minify", function() {
output: {
comments: "all",
beautify: false,
}
},
toplevel: false,
});
var code = result.code;
assert.strictEqual(code, "var a=function(){foo()}();");
@@ -301,7 +316,7 @@ describe("minify", function() {
compress: false,
mangle: false,
output: {
ast: true
ast: true,
},
}).ast;
assert.strictEqual(ast.TYPE, "Toplevel");
@@ -312,9 +327,10 @@ describe("minify", function() {
var stat = ast.body[0].body[0];
UglifyJS.minify(ast, {
compress: {
sequences: false
sequences: false,
toplevel: false,
},
mangle: false
mangle: false,
});
assert.ok(stat.body);
assert.strictEqual(stat.print_to_string(), "a=x()");

View File

@@ -109,6 +109,7 @@ describe("sourcemaps", function() {
"}",
].join("\n"), {
sourceMap: true,
toplevel: false,
});
if (result.error) throw result.error;
assert.strictEqual(result.code, "class A{static P=42;set#q(s){}}");
@@ -187,6 +188,7 @@ describe("sourcemaps", function() {
sourceMap: {
content: "inline",
},
toplevel: false,
warnings: true,
});
assert.strictEqual(result.code, "var bar=function(bar){return bar};");
@@ -201,6 +203,7 @@ describe("sourcemaps", function() {
content: "inline",
url: "inline",
},
toplevel: false,
warnings: true,
});
if (result.error) throw result.error;
@@ -296,8 +299,9 @@ describe("sourcemaps", function() {
it("Should append source map to output js when sourceMapInline is enabled", function() {
var result = UglifyJS.minify('var a = function(foo) { return foo; };', {
sourceMap: {
url: "inline"
}
url: "inline",
},
toplevel: false,
});
if (result.error) throw result.error;
var code = result.code;
@@ -305,7 +309,9 @@ describe("sourcemaps", function() {
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjAiXSwibmFtZXMiOlsiYSIsImZvbyJdLCJtYXBwaW5ncyI6IkFBQUEsSUFBSUEsRUFBSSxTQUFTQyxHQUFPLE9BQU9BLENBQUsifQ==");
});
it("Should not append source map to output js when sourceMapInline is not enabled", function() {
var result = UglifyJS.minify('var a = function(foo) { return foo; };');
var result = UglifyJS.minify("var a = function(foo) { return foo; };", {
module: false,
});
if (result.error) throw result.error;
var code = result.code;
assert.strictEqual(code, "var a=function(n){return n};");
@@ -316,11 +322,12 @@ describe("sourcemaps", function() {
directives: false,
},
output: {
max_line_len: 20
max_line_len: 20,
},
sourceMap: {
url: "inline"
}
url: "inline",
},
toplevel: false,
});
if (result.error) throw result.error;
assert.strictEqual(result.code, read("test/input/issue-505/output.js"));
@@ -334,7 +341,8 @@ describe("sourcemaps", function() {
sourceMap: {
includeSources: true,
url: "inline",
}
},
toplevel: false,
});
if (result.error) throw result.error;
var map = JSON.parse(result.map);
@@ -348,7 +356,8 @@ describe("sourcemaps", function() {
sourceMap: {
content: "inline",
includeSources: true,
}
},
toplevel: false,
});
if (result.error) throw result.error;
map = JSON.parse(result.map);

View File

@@ -3,7 +3,7 @@ require("./run")([
"-b braces",
"-m",
"-mc passes=3",
"-mc passes=3,toplevel",
"--no-module -mc",
"-mc passes=3,unsafe",
"-mc keep_fargs=false,passes=3",
"-mc keep_fargs=false,passes=3,pure_getters,unsafe,unsafe_comps,unsafe_math,unsafe_proto",

View File

@@ -5,5 +5,6 @@ require("./run")([
var args = options.split(/ /);
args.unshift("test/jetstream.js");
args.push("-b", "beautify=false,webkit");
args.push("--no-module");
return args;
}));

View File

@@ -2,6 +2,7 @@
{
"compress": false,
"mangle": false,
"module": false,
"output": {
"beautify": true,
"braces": true
@@ -9,14 +10,19 @@
"rename": true
},
{
"compress": false
"compress": false,
"module": false
},
{
"mangle": false
"mangle": false,
"module": false
},
{
"module": false
},
{},
{
"ie": true,
"module": false,
"toplevel": true
},
{
@@ -28,6 +34,7 @@
},
"keep_fargs": true,
"keep_fnames": true,
"module": false,
"toplevel": true
},
{
@@ -39,6 +46,7 @@
"unsafe_math": true,
"unsafe_proto": true,
"unsafe_regexp": true
}
},
"module": false
}
]