make enclose & wrap work with sourceMap (#3396)

fixes #3313
This commit is contained in:
Alex Lam S.L
2019-05-04 20:25:52 +08:00
committed by GitHub
parent 11cdab745d
commit 354fec8a9c
6 changed files with 43 additions and 34 deletions

View File

@@ -767,9 +767,6 @@ If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.u
- `unused` (default: `true`) -- drop unreferenced functions and variables (simple - `unused` (default: `true`) -- drop unreferenced functions and variables (simple
direct variable assignments do not count as references unless set to `"keep_assign"`) direct variable assignments do not count as references unless set to `"keep_assign"`)
- `warnings` (default: `false`) -- display warnings when dropping unreachable
code or unused declarations etc.
## Mangle options ## Mangle options
- `eval` (default `false`) -- Pass `true` to mangle names visible in scopes - `eval` (default `false`) -- Pass `true` to mangle names visible in scopes

View File

@@ -215,8 +215,10 @@ function run() {
var ex = result.error; var ex = result.error;
if (ex.name == "SyntaxError") { if (ex.name == "SyntaxError") {
print_error("Parse error at " + ex.filename + ":" + ex.line + "," + ex.col); print_error("Parse error at " + ex.filename + ":" + ex.line + "," + ex.col);
var file = files[ex.filename];
if (file) {
var col = ex.col; var col = ex.col;
var lines = files[ex.filename].split(/\r?\n/); var lines = file.split(/\r?\n/);
var line = lines[ex.line - 1]; var line = lines[ex.line - 1];
if (!line && !col) { if (!line && !col) {
line = lines[ex.line - 2]; line = lines[ex.line - 2];
@@ -232,7 +234,7 @@ function run() {
print_error(line.slice(0, col).replace(/\S/g, " ") + "^"); print_error(line.slice(0, col).replace(/\S/g, " ") + "^");
} }
} }
if (ex.defs) { } else if (ex.defs) {
print_error("Supported options:"); print_error("Supported options:");
print_error(format_object(ex.defs)); print_error(format_object(ex.defs));
} }

View File

@@ -337,18 +337,25 @@ var AST_Toplevel = DEFNODE("Toplevel", "globals", {
$propdoc: { $propdoc: {
globals: "[Object/S] a map of name -> SymbolDef for all undeclared names", globals: "[Object/S] a map of name -> SymbolDef for all undeclared names",
}, },
wrap_commonjs: function(name) { wrap: function(name) {
var body = this.body; var body = this.body;
var wrapped_tl = "(function(exports){'$ORIG';})(typeof " + name + "=='undefined'?(" + name + "={}):" + name + ");"; return parse([
wrapped_tl = parse(wrapped_tl); "(function(exports){'$ORIG';})(typeof ",
wrapped_tl = wrapped_tl.transform(new TreeTransformer(function(node) { name,
"=='undefined'?(",
name,
"={}):",
name,
");"
].join(""), {
filename: "wrap=" + JSON.stringify(name)
}).transform(new TreeTransformer(function(node) {
if (node instanceof AST_Directive && node.value == "$ORIG") { if (node instanceof AST_Directive && node.value == "$ORIG") {
return MAP.splice(body); return MAP.splice(body);
} }
})); }));
return wrapped_tl;
}, },
wrap_enclose: function(args_values) { enclose: function(args_values) {
if (typeof args_values != "string") args_values = ""; if (typeof args_values != "string") args_values = "";
var index = args_values.indexOf(":"); var index = args_values.indexOf(":");
if (index < 0) index = args_values.length; if (index < 0) index = args_values.length;
@@ -359,7 +366,9 @@ var AST_Toplevel = DEFNODE("Toplevel", "globals", {
'){"$ORIG"})(', '){"$ORIG"})(',
args_values.slice(index + 1), args_values.slice(index + 1),
")" ")"
].join("")).transform(new TreeTransformer(function(node) { ].join(""), {
filename: "enclose=" + JSON.stringify(args_values)
}).transform(new TreeTransformer(function(node) {
if (node instanceof AST_Directive && node.value == "$ORIG") { if (node instanceof AST_Directive && node.value == "$ORIG") {
return MAP.splice(body); return MAP.splice(body);
} }

View File

@@ -151,12 +151,13 @@ function minify(files, options) {
if (quoted_props) { if (quoted_props) {
reserve_quoted_keys(toplevel, quoted_props); reserve_quoted_keys(toplevel, quoted_props);
} }
if (options.wrap) { [ "enclose", "wrap" ].forEach(function(action) {
toplevel = toplevel.wrap_commonjs(options.wrap); var option = options[action];
} if (!option) return;
if (options.enclose) { var orig = toplevel.print_to_string().slice(0, -1);
toplevel = toplevel.wrap_enclose(options.enclose); toplevel = toplevel[action](option);
} files[toplevel.start.file] = toplevel.print_to_string().replace(orig, "");
});
if (timings) timings.rename = Date.now(); if (timings) timings.rename = Date.now();
if (options.rename) { if (options.rename) {
toplevel.figure_out_scope(options.mangle); toplevel.figure_out_scope(options.mangle);

View File

@@ -712,7 +712,7 @@ describe("bin/uglifyjs", function() {
var command = uglifyjscmd + " test/input/enclose/input.js --enclose window,undefined:window --wrap exports"; var command = uglifyjscmd + " test/input/enclose/input.js --enclose window,undefined:window --wrap exports";
exec(command, function(err, stdout, stderr) { exec(command, function(err, stdout, stderr) {
if (err) throw err; if (err) throw err;
assert.strictEqual(stdout, '(function(window,undefined){(function(exports){function enclose(){console.log("test enclose")}enclose()})(typeof exports=="undefined"?exports={}:exports)})(window);\n'); assert.strictEqual(stdout, '(function(exports){(function(window,undefined){function enclose(){console.log("test enclose")}enclose()})(window)})(typeof exports=="undefined"?exports={}:exports);\n');
done(); done();
}); });
}); });

View File

@@ -365,7 +365,7 @@ describe("minify", function() {
wrap: 'exports', wrap: 'exports',
}); });
if (result.error) throw result.error; if (result.error) throw result.error;
assert.strictEqual(result.code, '(function(window,undefined){(function(exports){function enclose(){console.log("test enclose")}enclose()})(typeof exports=="undefined"?exports={}:exports)})(window);'); assert.strictEqual(result.code, '(function(exports){(function(window,undefined){function enclose(){console.log("test enclose")}enclose()})(window)})(typeof exports=="undefined"?exports={}:exports);');
}); });
}); });
}); });