workaround Safari quirks (#4314)

fixes #1753
This commit is contained in:
Alex Lam S.L
2020-11-21 02:30:46 +00:00
committed by GitHub
parent cf120c7cea
commit 645d5a348b
5 changed files with 70 additions and 3 deletions

View File

@@ -135,6 +135,10 @@ a double dash to prevent input files being used as option arguments:
--toplevel Compress and/or mangle variables in top level scope. --toplevel Compress and/or mangle variables in top level scope.
--verbose Print diagnostic messages. --verbose Print diagnostic messages.
--warn Print warning messages. --warn Print warning messages.
--webkit Support non-standard Safari/Webkit.
Equivalent to setting `webkit: true` in `minify()`
for `mangle` and `output` options.
By default UglifyJS will not try to be Safari-proof.
--wrap <name> Embed everything in a big function, making the --wrap <name> Embed everything in a big function, making the
“exports” and “global” variables available. You “exports” and “global” variables available. You
need to pass an argument to this option to need to pass an argument to this option to
@@ -519,6 +523,9 @@ if (result.error) throw result.error;
- `warnings` (default `false`) — pass `true` to return compressor warnings - `warnings` (default `false`) — pass `true` to return compressor warnings
in `result.warnings`. Use the value `"verbose"` for more detailed warnings. in `result.warnings`. Use the value `"verbose"` for more detailed warnings.
- `webkit` (default `false`) -- enable workarounds for Safari/WebKit bugs.
PhantomJS users should set this option to `true`.
## Minify options structure ## Minify options structure
```javascript ```javascript
@@ -910,9 +917,6 @@ can pass additional arguments that control the code output:
- `v8` (default `false`) -- enable workarounds for Chrome & Node.js bugs - `v8` (default `false`) -- enable workarounds for Chrome & Node.js bugs
- `webkit` (default `false`) -- enable workarounds for WebKit bugs.
PhantomJS users should set this option to `true`.
- `width` (default `80`) -- only takes effect when beautification is on, this - `width` (default `80`) -- only takes effect when beautification is on, this
specifies an (orientative) line width that the beautifier will try to specifies an (orientative) line width that the beautifier will try to
obey. It refers to the width of the line text (excluding indentation). obey. It refers to the width of the line text (excluding indentation).

View File

@@ -111,6 +111,7 @@ function process_option(name, no_value) {
" --validate Perform validation during AST manipulations.", " --validate Perform validation during AST manipulations.",
" --verbose Print diagnostic messages.", " --verbose Print diagnostic messages.",
" --warn Print warning messages.", " --warn Print warning messages.",
" --webkit Support non-standard Safari/Webkit.",
" --wrap <name> Embed everything as a function with “exports” corresponding to “name” globally.", " --wrap <name> Embed everything as a function with “exports” corresponding to “name” globally.",
" --reduce-test Reduce a standalone test case (assumes cloned repository).", " --reduce-test Reduce a standalone test case (assumes cloned repository).",
].join("\n")); ].join("\n"));
@@ -142,6 +143,7 @@ function process_option(name, no_value) {
case "timings": case "timings":
case "toplevel": case "toplevel":
case "validate": case "validate":
case "webkit":
options[name] = true; options[name] = true;
break; break;
case "keep-fnames": case "keep-fnames":

View File

@@ -89,6 +89,7 @@ function minify(files, options) {
toplevel: false, toplevel: false,
validate: false, validate: false,
warnings: false, warnings: false,
webkit: false,
wrap: false, wrap: false,
}, true); }, true);
if (options.validate) AST_Node.enable_validation(); if (options.validate) AST_Node.enable_validation();
@@ -101,6 +102,7 @@ function minify(files, options) {
set_shorthand("ie8", options, [ "compress", "mangle", "output" ]); set_shorthand("ie8", options, [ "compress", "mangle", "output" ]);
set_shorthand("keep_fnames", options, [ "compress", "mangle" ]); set_shorthand("keep_fnames", options, [ "compress", "mangle" ]);
set_shorthand("toplevel", options, [ "compress", "mangle" ]); set_shorthand("toplevel", options, [ "compress", "mangle" ]);
set_shorthand("webkit", options, [ "mangle", "output" ]);
var quoted_props; var quoted_props;
if (options.mangle) { if (options.mangle) {
options.mangle = defaults(options.mangle, { options.mangle = defaults(options.mangle, {
@@ -111,6 +113,7 @@ function minify(files, options) {
properties: false, properties: false,
reserved: [], reserved: [],
toplevel: false, toplevel: false,
webkit: false,
}, true); }, true);
if (options.mangle.properties) { if (options.mangle.properties) {
if (typeof options.mangle.properties != "object") { if (typeof options.mangle.properties != "object") {

View File

@@ -486,6 +486,7 @@ function _default_mangler_options(options) {
keep_fnames : false, keep_fnames : false,
reserved : [], reserved : [],
toplevel : false, toplevel : false,
webkit : false,
}); });
if (!Array.isArray(options.reserved)) options.reserved = []; if (!Array.isArray(options.reserved)) options.reserved = [];
// Never mangle arguments // Never mangle arguments
@@ -520,6 +521,21 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) {
return true; return true;
} }
if (node instanceof AST_BlockScope) { if (node instanceof AST_BlockScope) {
if (options.webkit && node instanceof AST_IterationStatement && node.init instanceof AST_Let) {
node.init.definitions.forEach(function(defn) {
defn.name.match_symbol(function(sym) {
if (!(sym instanceof AST_SymbolLet)) return;
var def = sym.definition();
var scope = sym.scope.parent_scope;
var redef = scope.def_variable(sym);
sym.thedef = def;
scope.to_mangle.push(redef);
def.redefined = function() {
return redef;
};
});
}, true);
}
node.to_mangle = []; node.to_mangle = [];
node.variables.each(function(def) { node.variables.each(function(def) {
if (!defer_redef(def)) node.to_mangle.push(def); if (!defer_redef(def)) node.to_mangle.push(def);

View File

@@ -1186,3 +1186,45 @@ issue_4305_2: {
expect_stdout: true expect_stdout: true
node_version: ">=4" node_version: ">=4"
} }
issue_1753: {
mangle = {
toplevel: false,
webkit: true,
}
input: {
"use strict";
let l = null;
for (let i = 0; i < 1; i++)
console.log(i);
}
expect: {
"use strict";
let l = null;
for (let i = 0; i < 1; i++)
console.log(i);
}
expect_stdout: "0"
node_version: ">=4"
}
issue_1753_toplevel: {
mangle = {
toplevel: true,
webkit: true,
}
input: {
"use strict";
let l = null;
for (let i = 0; i < 1; i++)
console.log(i);
}
expect: {
"use strict";
let l = null;
for (let e = 0; e < 1; e++)
console.log(e);
}
expect_stdout: "0"
node_version: ">=4"
}