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

@@ -12,8 +12,8 @@ jobs:
- '-mb braces' - '-mb braces'
- '--ie -c' - '--ie -c'
- '-mc' - '-mc'
- '-p acorn --toplevel -mco spidermonkey' - '-p acorn -mco spidermonkey'
- '--toplevel -mc passes=3,pure_getters,unsafe' - '-mc passes=3,pure_getters,unsafe'
script: script:
- acorn.sh - acorn.sh
- bootstrap.sh - bootstrap.sh

View File

@@ -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 --keep-fnames Do not mangle/drop function names. Useful for
code relying on Function.prototype.name. code relying on Function.prototype.name.
--module Process input as ES module (implies --toplevel) --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. --name-cache <file> File to hold mangled name mappings.
--self Build UglifyJS as a library (implies --wrap UglifyJS) --self Build UglifyJS as a library (implies --wrap UglifyJS)
--source-map [options] Enable source map/specify source map options: --source-map [options] Enable source map/specify source map options:
@@ -530,9 +532,9 @@ if (result.error) throw result.error;
- `mangle.properties` (default: `false`) — a subcategory of the mangle option. - `mangle.properties` (default: `false`) — a subcategory of the mangle option.
Pass an object to specify custom [mangle property options](#mangle-properties-options). 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 - `module` (default: `true`) — process input as ES module, i.e. implicit
ES module, i.e. implicit `"use strict";` and support for top-level `await`, `"use strict";` and support for top-level `await`, alongside with `toplevel`
alongside with `toplevel` enabled. enabled.
- `nameCache` (default: `null`) — pass an empty object `{}` or a previously - `nameCache` (default: `null`) — pass an empty object `{}` or a previously
used `nameCache` object if you wish to cache mangled variable and used `nameCache` object if you wish to cache mangled variable and

View File

@@ -108,7 +108,8 @@ function process_option(name, no_value) {
" --ie Support non-standard Internet Explorer.", " --ie Support non-standard Internet Explorer.",
" --keep-fargs Do not mangle/drop function arguments.", " --keep-fargs Do not mangle/drop function arguments.",
" --keep-fnames Do not mangle/drop function names. Useful for code relying on Function.prototype.name.", " --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.", " --name-cache <file> File to hold mangled name mappings.",
" --rename Force symbol expansion.", " --rename Force symbol expansion.",
" --no-rename Disable symbol expansion.", " --no-rename Disable symbol expansion.",
@@ -155,7 +156,6 @@ function process_option(name, no_value) {
case "expression": case "expression":
case "ie": case "ie":
case "ie8": case "ie8":
case "module":
case "timings": case "timings":
case "toplevel": case "toplevel":
case "v8": case "v8":
@@ -201,6 +201,12 @@ function process_option(name, no_value) {
if (typeof options.mangle != "object") options.mangle = {}; if (typeof options.mangle != "object") options.mangle = {};
options.mangle.properties = parse_js(read_value(), options.mangle.properties); options.mangle.properties = parse_js(read_value(), options.mangle.properties);
break; break;
case "module":
options.module = true;
break;
case "no-module":
options.module = false;
break;
case "name-cache": case "name-cache":
nameCache = read_value(true); nameCache = read_value(true);
options.nameCache = JSON.parse(read_file(nameCache, "{}")); options.nameCache = JSON.parse(read_file(nameCache, "{}"));

View File

@@ -10234,22 +10234,24 @@ Compressor.prototype.compress = function(node) {
} }
function varify(self, compressor) { function varify(self, compressor) {
return all(self.definitions, function(defn) { if (all(self.definitions, function(defn) {
return !defn.name.match_symbol(function(node) { return !defn.name.match_symbol(function(node) {
if (node instanceof AST_SymbolDeclaration) return !can_varify(compressor, node); if (node instanceof AST_SymbolDeclaration) return !can_varify(compressor, node);
}, true); }, 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) { OPT(AST_Const, function(self, compressor) {
if (!compressor.option("varify")) return self; 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); if (can_letify(self, compressor, 0)) return to_let(self);
return varify(self, compressor); return self;
}); });
OPT(AST_Let, function(self, compressor) { OPT(AST_Let, function(self, compressor) {
if (!compressor.option("varify")) return self; if (!compressor.option("varify")) return self;
return varify(self, compressor); return varify(self, compressor) || self;
}); });
function trim_optional_chain(node, compressor) { function trim_optional_chain(node, compressor) {

View File

@@ -82,14 +82,14 @@ function minify(files, options) {
keep_fargs: false, keep_fargs: false,
keep_fnames: false, keep_fnames: false,
mangle: {}, mangle: {},
module: false, module: undefined,
nameCache: null, nameCache: null,
output: {}, output: {},
parse: {}, parse: {},
rename: undefined, rename: undefined,
sourceMap: false, sourceMap: false,
timings: false, timings: false,
toplevel: !!(options && !options["expression"] && options["module"]), toplevel: undefined,
v8: false, v8: false,
validate: false, validate: false,
warnings: false, warnings: false,
@@ -104,8 +104,10 @@ function minify(files, options) {
if (options.ie) set_shorthand("ie", options, [ "compress", "mangle", "output", "rename" ]); 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_fargs) set_shorthand("keep_fargs", options, [ "compress", "mangle", "rename" ]);
if (options.keep_fnames) set_shorthand("keep_fnames", 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.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.v8) set_shorthand("v8", options, [ "mangle", "output", "rename" ]);
if (options.webkit) set_shorthand("webkit", options, [ "compress", "mangle", "output", "rename" ]); if (options.webkit) set_shorthand("webkit", options, [ "compress", "mangle", "output", "rename" ]);
var quoted_props; var quoted_props;

View File

@@ -68,9 +68,7 @@ SymbolDef.prototype = {
var cache = this.global && options.cache && options.cache.props; var cache = this.global && options.cache && options.cache.props;
if (cache && cache.has(this.name)) { if (cache && cache.has(this.name)) {
this.mangled_name = cache.get(this.name); this.mangled_name = cache.get(this.name);
} else if (this.unmangleable(options)) { } else if (!this.unmangleable(options)) {
names_in_use(this.scope, options).set(this.name, true);
} else {
var def = this.redefined(); var def = this.redefined();
if (def) { if (def) {
this.mangled_name = def.mangled_name || def.name; this.mangled_name = def.mangled_name || def.name;
@@ -651,8 +649,12 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) {
}, true); }, true);
} }
var to_mangle = node.to_mangle = []; var to_mangle = node.to_mangle = [];
node.variables.each(function(def) { node.variables.each(function(def, name) {
if (!defer_redef(def)) to_mangle.push(def); if (def.unmangleable(options)) {
names_in_use(node, options).set(name, true);
} else if (!defer_redef(def)) {
to_mangle.push(def);
}
}); });
descend(); descend();
if (options.cache && node instanceof AST_Toplevel) { if (options.cache && node instanceof AST_Toplevel) {

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: { keep_ref: {
options = { options = {
reduce_vars: true, reduce_vars: true,

View File

@@ -206,7 +206,13 @@ describe("bin/uglifyjs", function() {
}, 1000); }, 1000);
}); });
it("Should work with --keep-fargs (mangle only)", function(done) { 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) { exec(command, function(err, stdout) {
if (err) throw err; 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"); 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) { 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) { exec(command, function(err, stdout) {
if (err) throw err; 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"); 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) { 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) { exec(command, function(err, stdout) {
if (err) throw err; 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"); 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) { 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) { exec(command, function(err, stdout) {
if (err) throw err; 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"); 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) { 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) { exec(command, function(err, stdout) {
if (err) throw err; 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"); 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) { 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) { exec(command, function(err, stdout, stderr) {
assert.ok(err); assert.ok(err);
assert.strictEqual(stdout, ""); assert.strictEqual(stdout, "");
@@ -571,7 +605,11 @@ describe("bin/uglifyjs", function() {
}); });
}); });
it("Should throw syntax error (function g(arguments))", function(done) { 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) { exec(command, function(err, stdout, stderr) {
assert.ok(err); assert.ok(err);
assert.strictEqual(stdout, ""); assert.strictEqual(stdout, "");
@@ -585,7 +623,11 @@ describe("bin/uglifyjs", function() {
}); });
}); });
it("Should throw syntax error (function eval())", function(done) { 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) { exec(command, function(err, stdout, stderr) {
assert.ok(err); assert.ok(err);
assert.strictEqual(stdout, ""); assert.strictEqual(stdout, "");
@@ -599,7 +641,11 @@ describe("bin/uglifyjs", function() {
}); });
}); });
it("Should throw syntax error (iife arguments())", function(done) { 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) { exec(command, function(err, stdout, stderr) {
assert.ok(err); assert.ok(err);
assert.strictEqual(stdout, ""); assert.strictEqual(stdout, "");
@@ -613,7 +659,11 @@ describe("bin/uglifyjs", function() {
}); });
}); });
it("Should throw syntax error (catch (eval))", function(done) { 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) { exec(command, function(err, stdout, stderr) {
assert.ok(err); assert.ok(err);
assert.strictEqual(stdout, ""); assert.strictEqual(stdout, "");
@@ -627,7 +677,11 @@ describe("bin/uglifyjs", function() {
}); });
}); });
it("Should throw syntax error (var eval)", function(done) { 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) { exec(command, function(err, stdout, stderr) {
assert.ok(err); assert.ok(err);
assert.strictEqual(stdout, ""); assert.strictEqual(stdout, "");
@@ -641,7 +695,11 @@ describe("bin/uglifyjs", function() {
}); });
}); });
it("Should throw syntax error (var { eval })", function(done) { 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) { exec(command, function(err, stdout, stderr) {
assert.ok(err); assert.ok(err);
assert.strictEqual(stdout, ""); assert.strictEqual(stdout, "");
@@ -807,7 +865,8 @@ describe("bin/uglifyjs", function() {
var command = [ var command = [
uglifyjscmd, uglifyjscmd,
"test/input/issue-2310/input.js", "test/input/issue-2310/input.js",
"-c", "--compress",
"--no-module",
"--source-map", "url=inline", "--source-map", "url=inline",
].join(" "); ].join(" ");
exec(command, function(err, stdout, stderr) { exec(command, function(err, stdout, stderr) {
@@ -840,7 +899,12 @@ describe("bin/uglifyjs", function() {
}); });
}); });
it("Should work with --mangle reserved=[]", function(done) { 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) { exec(command, function(err, stdout) {
if (err) throw err; if (err) throw err;
assert.strictEqual(stdout, 'function test(callback){"aaaaaaaaaaaaaaaa";callback(err,data);callback(err,data)}\n'); 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) { 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) { exec(command, function(err, stdout) {
if (err) throw err; if (err) throw err;
assert.strictEqual(stdout, 'function test(a){"aaaaaaaaaaaaaaaa";a(err,data);a(err,data)}\n'); 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) { 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) { exec(command, function(err, stdout) {
if (err) throw err; if (err) throw err;
assert.strictEqual(stdout, 'function f(){"aaaaaaaaaa";var a={prop:1,t:2};return a.prop+a.t}\n'); 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) { 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) { exec(command, function(err, stdout, stderr) {
if (err) throw err; if (err) throw err;
assert.strictEqual(stdout, "function f(a){return b(a);function b(c){return c+c}}\n"); 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) { 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) { exec(command, function(err, stdout, stderr) {
if (err) throw err; if (err) throw err;
assert.strictEqual(stdout, "function f(n){return function(n){return n+n}(n)}\n"); 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) { 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) { exec(command, function(err, stdout, stderr) {
if (err) throw err; if (err) throw err;
assert.strictEqual(stdout, "function f(n){return n+n}\n"); 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) { 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) { exec(command, function(err, stdout, stderr) {
if (err) throw err; if (err) throw err;
assert.strictEqual(stdout, "function f(x){return function(x){return x+x}(x)}\n"); assert.strictEqual(stdout, "function f(x){return function(x){return x+x}(x)}\n");
@@ -971,12 +1068,7 @@ describe("bin/uglifyjs", function() {
]).join("\n"); ]).join("\n");
exec(uglifyjscmd + " -mc", function(err, stdout) { exec(uglifyjscmd + " -mc", function(err, stdout) {
if (err) throw err; if (err) throw err;
assert.strictEqual(stdout, [ assert.strictEqual(stdout, "console.log({p:25}.p+{p:121}.p+{p:1024}.p);\n");
"console.log(function(){",
"var p={p:25},n={p:121},o={p:1024};",
"return p.p+n.p+o.p",
"}());\n",
].join(""));
assert.strictEqual(run_code(stdout), run_code(code)); assert.strictEqual(run_code(stdout), run_code(code));
done(); done();
}).stdin.end(code); }).stdin.end(code);

View File

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

View File

@@ -373,7 +373,9 @@ describe("Directives", function() {
'function f(){}' 'function f(){}'
], ],
].forEach(function(test) { ].forEach(function(test) {
var result = UglifyJS.minify(test[0]); var result = UglifyJS.minify(test[0], {
module: false,
});
if (result.error) throw result.error; if (result.error) throw result.error;
assert.strictEqual(result.code, test[1], test[0]); 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() { describe("bin/uglifyjs with input file globs", function() {
var uglifyjscmd = '"' + process.argv[0] + '" bin/uglifyjs'; var uglifyjscmd = '"' + process.argv[0] + '" bin/uglifyjs';
it("bin/uglifyjs with one input file extension glob.", function(done) { 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) { exec(command, function(err, stdout) {
if (err) throw err; if (err) throw err;
assert.strictEqual(stdout, 'var print=console.log.bind(console);function foo(o){print("Foo:",2*o)}\n'); assert.strictEqual(stdout, 'var print=console.log.bind(console);function foo(o){print("Foo:",2*o)}\n');
done(); done();
}); });
}); });
it("bin/uglifyjs with one input file name glob.", function(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) { exec(command, function(err, stdout) {
if (err) throw err; if (err) throw err;
assert.strictEqual(stdout, 'function bar(n){return 3*n}function baz(n){return n/2}\n'); assert.strictEqual(stdout, 'function bar(n){return 3*n}function baz(n){return n/2}\n');
done(); done();
}); });
}); });
it("bin/uglifyjs with multiple input file globs.", function(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) { exec(command, function(err, stdout) {
if (err) throw err; if (err) throw err;
assert.strictEqual(stdout, 'var print=console.log.bind(console);print("qux",9,6),print("Foo:",22);\n'); assert.strictEqual(stdout, 'var print=console.log.bind(console);print("qux",9,6),print("Foo:",22);\n');
done(); done();
}); });

View File

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

View File

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

View File

@@ -3,7 +3,7 @@ require("./run")([
"-b braces", "-b braces",
"-m", "-m",
"-mc passes=3", "-mc passes=3",
"-mc passes=3,toplevel", "--no-module -mc",
"-mc passes=3,unsafe", "-mc passes=3,unsafe",
"-mc keep_fargs=false,passes=3", "-mc keep_fargs=false,passes=3",
"-mc keep_fargs=false,passes=3,pure_getters,unsafe,unsafe_comps,unsafe_math,unsafe_proto", "-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(/ /); var args = options.split(/ /);
args.unshift("test/jetstream.js"); args.unshift("test/jetstream.js");
args.push("-b", "beautify=false,webkit"); args.push("-b", "beautify=false,webkit");
args.push("--no-module");
return args; return args;
})); }));

View File

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