Skip to content

Commit

Permalink
workaround various IE quirks (#5084)
Browse files Browse the repository at this point in the history
fixes #5081
  • Loading branch information
alexlamsl committed Jul 17, 2021
1 parent 902997b commit ef5f7fc
Show file tree
Hide file tree
Showing 33 changed files with 477 additions and 311 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
matrix:
options:
- '-mb braces'
- '--ie8 -c'
- '--ie -c'
- '-mc'
- '-p acorn --toplevel -mco spidermonkey'
- '--toplevel -mc passes=3,pure_getters,unsafe'
Expand Down
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ a double dash to prevent input files being used as option arguments:
-d, --define <expr>[=value] Global definitions.
-e, --enclose [arg[:value]] Embed everything in a big function, with configurable
argument(s) & value(s).
--ie8 Support non-standard Internet Explorer 8.
Equivalent to setting `ie8: true` in `minify()`
--ie Support non-standard Internet Explorer.
Equivalent to setting `ie: true` in `minify()`
for `compress`, `mangle` and `output` options.
By default UglifyJS will not try to be IE-proof.
--keep-fnames Do not mangle/drop function names. Useful for
Expand Down Expand Up @@ -502,7 +502,7 @@ if (result.error) throw result.error;
- `compress` (default: `{}`) — pass `false` to skip compressing entirely.
Pass an object to specify custom [compress options](#compress-options).

- `ie8` (default: `false`) — set to `true` to support IE8.
- `ie` (default: `false`) — enable workarounds for Internet Explorer bugs.

- `keep_fnames` (default: `false`) — pass `true` to prevent discarding or mangling
of function names. Useful for code relying on `Function.prototype.name`.
Expand Down Expand Up @@ -566,7 +566,6 @@ if (result.error) throw result.error;
},
nameCache: null, // or specify a name cache object
toplevel: false,
ie8: false,
warnings: false,
}
```
Expand Down Expand Up @@ -795,9 +794,8 @@ to be `false` and all symbol names will be omitted.
variables (`"vars"`) in the top level scope (`false` by default, `true` to drop
both unreferenced functions and variables)

- `typeofs` (default: `true`) — Transforms `typeof foo == "undefined"` into
`foo === void 0`. Note: recommend to set this value to `false` for IE10 and
earlier versions due to known issues.
- `typeofs` (default: `true`) — compress `typeof` expressions, e.g.
`typeof foo == "undefined" → void 0 === foo`

- `unsafe` (default: `false`) — apply "unsafe" transformations (discussion below)

Expand Down
5 changes: 4 additions & 1 deletion bin/uglifyjs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function process_option(name, no_value) {
" --config-file <file> Read minify() options from JSON file.",
" -d, --define <expr>[=value] Global definitions.",
" -e, --enclose [arg[,...][:value[,...]]] Embed everything in a big function, with configurable argument(s) & value(s).",
" --ie8 Support non-standard Internet Explorer 8.",
" --ie Support non-standard Internet Explorer.",
" --keep-fnames Do not mangle/drop function names. Useful for code relying on Function.prototype.name.",
" --name-cache <file> File to hold mangled name mappings.",
" --rename Force symbol expansion.",
Expand All @@ -110,6 +110,7 @@ function process_option(name, no_value) {
" --source-map [options] Enable source map/specify source map options.",
" --timings Display operations run time on STDERR.",
" --toplevel Compress and/or mangle variables in toplevel scope.",
" --v8 Support non-standard Chrome & Node.js.",
" --validate Perform validation during AST manipulations.",
" --verbose Print diagnostic messages.",
" --warn Print warning messages.",
Expand Down Expand Up @@ -145,9 +146,11 @@ function process_option(name, no_value) {
options[name] = read_value();
break;
case "annotations":
case "ie":
case "ie8":
case "timings":
case "toplevel":
case "v8":
case "validate":
case "webkit":
options[name] = true;
Expand Down
42 changes: 26 additions & 16 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function Compressor(options, false_by_default) {
hoist_funs : false,
hoist_props : !false_by_default,
hoist_vars : false,
ie8 : false,
ie : false,
if_return : !false_by_default,
imports : !false_by_default,
inline : !false_by_default,
Expand Down Expand Up @@ -206,7 +206,7 @@ merge(Compressor.prototype, {
var passes = +this.options.passes || 1;
var min_count = 1 / 0;
var stopping = false;
var mangle = { ie8: this.option("ie8") };
var mangle = { ie: this.option("ie") };
for (var pass = 0; pass < passes; pass++) {
node.figure_out_scope(mangle);
if (pass > 0 || this.option("reduce_vars"))
Expand Down Expand Up @@ -513,7 +513,7 @@ merge(Compressor.prototype, {
if (scope.uses_arguments) scope.each_argname(function(node) {
node.definition().last_ref = false;
});
if (compressor.option("ie8")) scope.variables.each(function(def) {
if (compressor.option("ie")) scope.variables.each(function(def) {
var d = def.orig[0].definition();
if (d !== def) d.fixed = false;
});
Expand Down Expand Up @@ -2249,7 +2249,7 @@ merge(Compressor.prototype, {
return side_effects || lhs instanceof AST_PropAccess || may_modify(lhs);
}
if (node instanceof AST_Function) {
return compressor.option("ie8") && node.name && lvalues.has(node.name.name);
return compressor.option("ie") && node.name && lvalues.has(node.name.name);
}
if (node instanceof AST_ObjectIdentity) return symbol_in_lvalues(node, parent);
if (node instanceof AST_PropAccess) {
Expand Down Expand Up @@ -4208,7 +4208,7 @@ merge(Compressor.prototype, {

AST_Toplevel.DEFMETHOD("resolve_defines", function(compressor) {
if (!compressor.option("global_defs")) return this;
this.figure_out_scope({ ie8: compressor.option("ie8") });
this.figure_out_scope({ ie: compressor.option("ie") });
return this.transform(new TreeTransformer(function(node) {
var def = node._find_defs(compressor, "");
if (!def) return;
Expand Down Expand Up @@ -5672,7 +5672,7 @@ merge(Compressor.prototype, {
if (node instanceof AST_Call) {
var exp = node.expression;
var tail = exp.tail_node();
if (!(tail instanceof AST_LambdaExpression)) return;
if (!(tail instanceof AST_LambdaExpression)) return walk_node_with_expr(node);
if (exp !== tail) exp.expressions.slice(0, -1).forEach(function(node) {
node.walk(tw);
});
Expand Down Expand Up @@ -5788,6 +5788,7 @@ merge(Compressor.prototype, {
pop();
return true;
}
if (node instanceof AST_Sub) return walk_node_with_expr(node);
if (node instanceof AST_Switch) {
node.expression.walk(tw);
var save = segment;
Expand Down Expand Up @@ -5886,6 +5887,15 @@ merge(Compressor.prototype, {
pop();
return true;
}

function walk_node_with_expr(node) {
descend();
if (compressor.option("ie")) {
var sym = root_expr(node.expression);
if (sym instanceof AST_SymbolRef) sym.walk(tw);
}
return true;
}
});
tw.directives = Object.create(compressor.directives);
self.walk(tw);
Expand Down Expand Up @@ -6235,7 +6245,7 @@ merge(Compressor.prototype, {
});
tw.directives = Object.create(compressor.directives);
self.walk(tw);
var drop_fn_name = compressor.option("keep_fnames") ? return_false : compressor.option("ie8") ? function(def) {
var drop_fn_name = compressor.option("keep_fnames") ? return_false : compressor.option("ie") ? function(def) {
return !compressor.exposed(def) && def.references.length == def.replaced;
} : function(def) {
if (!(def.id in in_use_ids)) return true;
Expand All @@ -6247,7 +6257,7 @@ merge(Compressor.prototype, {
return !ref.in_arg;
});
};
if (compressor.option("ie8")) initializations.each(function(init, id) {
if (compressor.option("ie")) initializations.each(function(init, id) {
if (id in in_use_ids) return;
init.forEach(function(init) {
init.walk(new TreeWalker(function(node) {
Expand Down Expand Up @@ -6535,7 +6545,7 @@ merge(Compressor.prototype, {
head.push(def);
}
} else if (compressor.option("functions")
&& !compressor.option("ie8")
&& !compressor.option("ie")
&& drop_sym
&& var_defs[sym.id] == 1
&& sym.assignments == 0
Expand Down Expand Up @@ -7652,7 +7662,7 @@ merge(Compressor.prototype, {
});

function fn_name_unused(fn, compressor) {
if (!fn.name || !compressor.option("ie8")) return true;
if (!fn.name || !compressor.option("ie")) return true;
var def = fn.name.definition();
if (compressor.exposed(def)) return false;
return all(def.references, function(sym) {
Expand Down Expand Up @@ -7969,7 +7979,7 @@ merge(Compressor.prototype, {
var alternative = this.alternative.drop_side_effect_free(compressor);
if (consequent === this.consequent && alternative === this.alternative) return this;
var exprs;
if (compressor.option("ie8")) {
if (compressor.option("ie")) {
exprs = [];
if (consequent instanceof AST_Function) {
exprs.push(consequent);
Expand Down Expand Up @@ -7998,7 +8008,7 @@ merge(Compressor.prototype, {
node.consequent = consequent;
node.alternative = alternative;
}
if (!compressor.option("ie8")) return node;
if (!compressor.option("ie")) return node;
if (node) exprs.push(node);
return exprs.length == 0 ? null : make_sequence(this, exprs);
});
Expand Down Expand Up @@ -9411,7 +9421,7 @@ merge(Compressor.prototype, {
return arg.value;
}).join() + "){" + self.args[self.args.length - 1].value + "})";
var ast = parse(code);
var mangle = { ie8: compressor.option("ie8") };
var mangle = { ie: compressor.option("ie") };
ast.figure_out_scope(mangle);
var comp = new Compressor(compressor.options);
ast = ast.transform(comp);
Expand Down Expand Up @@ -10465,7 +10475,7 @@ merge(Compressor.prototype, {
&& self.right.operator == "typeof") {
var expr = self.right.expression;
if (expr instanceof AST_SymbolRef ? expr.is_declared(compressor)
: !(expr instanceof AST_PropAccess && compressor.option("ie8"))) {
: !(expr instanceof AST_PropAccess && compressor.option("ie"))) {
self.right = expr;
self.left = make_node(AST_Undefined, self.left).optimize(compressor);
if (self.operator.length == 2) self.operator += "=";
Expand Down Expand Up @@ -11079,7 +11089,7 @@ merge(Compressor.prototype, {
}

OPT(AST_SymbolRef, function(self, compressor) {
if (!compressor.option("ie8")
if (!compressor.option("ie")
&& is_undeclared_ref(self)
// testing against `self.scope.uses_with` is an optimization
&& !(self.scope.resolve().uses_with && compressor.find_parent(AST_With))) {
Expand Down Expand Up @@ -11121,7 +11131,7 @@ merge(Compressor.prototype, {
single_use = false;
} else if (fixed.has_side_effects(compressor)) {
single_use = false;
} else if (compressor.option("ie8") && fixed instanceof AST_Class) {
} else if (compressor.option("ie") && fixed instanceof AST_Class) {
single_use = false;
}
if (single_use) fixed.parent_scope = self.scope;
Expand Down
5 changes: 3 additions & 2 deletions lib/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function minify(files, options) {
annotations: undefined,
compress: {},
enclose: false,
ie: false,
ie8: false,
keep_fnames: false,
mangle: {},
Expand All @@ -96,7 +97,7 @@ function minify(files, options) {
var timings = options.timings && { start: Date.now() };
if (options.rename === undefined) options.rename = options.compress && options.mangle;
if (options.annotations !== undefined) set_shorthand("annotations", options, [ "compress", "output" ]);
if (options.ie8) set_shorthand("ie8", options, [ "compress", "mangle", "output" ]);
if (options.ie || options.ie8) set_shorthand("ie", options, [ "compress", "mangle", "output" ]);
if (options.keep_fnames) set_shorthand("keep_fnames", options, [ "compress", "mangle" ]);
if (options.toplevel) set_shorthand("toplevel", options, [ "compress", "mangle" ]);
if (options.v8) set_shorthand("v8", options, [ "mangle", "output" ]);
Expand All @@ -106,7 +107,7 @@ function minify(files, options) {
options.mangle = defaults(options.mangle, {
cache: options.nameCache && (options.nameCache.vars || {}),
eval: false,
ie8: false,
ie: false,
keep_fnames: false,
properties: false,
reserved: [],
Expand Down
10 changes: 5 additions & 5 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function OutputStream(options) {
braces : false,
comments : false,
galio : false,
ie8 : false,
ie : false,
indent_level : 4,
indent_start : 0,
inline_script : true,
Expand Down Expand Up @@ -193,7 +193,7 @@ function OutputStream(options) {
case "\t": return "\\t";
case "\b": return "\\b";
case "\f": return "\\f";
case "\x0B": return options.ie8 ? "\\x0B" : "\\v";
case "\x0B": return options.ie ? "\\x0B" : "\\v";
case "\u2028": return "\\u2028";
case "\u2029": return "\\u2029";
case "\ufeff": return "\\ufeff";
Expand Down Expand Up @@ -1290,7 +1290,7 @@ function OutputStream(options) {
function make_then(self, output) {
var b = self.body;
if (output.option("braces") && !(b instanceof AST_Const || b instanceof AST_Let)
|| output.option("ie8") && b instanceof AST_Do)
|| output.option("ie") && b instanceof AST_Do)
return make_block(b, output);
// The squeezer replaces "block"-s that contain only a single
// statement with the statement itself; technically, the AST
Expand Down Expand Up @@ -1515,7 +1515,7 @@ function OutputStream(options) {
var expr = self.expression;
expr.print(output);
var prop = self.property;
if (output.option("ie8") && RESERVED_WORDS[prop]) {
if (output.option("ie") && RESERVED_WORDS[prop]) {
output.print(self.optional ? "?.[" : "[");
output.add_mapping(self.end);
output.print_string(prop);
Expand Down Expand Up @@ -1702,7 +1702,7 @@ function OutputStream(options) {
var quote = self.start && self.start.quote;
if (self.private) {
output.print_name(key);
} else if (RESERVED_WORDS[key] ? !output.option("ie8") : is_identifier_string(key)) {
} else if (RESERVED_WORDS[key] ? !output.option("ie") : is_identifier_string(key)) {
if (quote && output.option("keep_quoted_props")) {
output.print_string(key, quote);
} else {
Expand Down
8 changes: 4 additions & 4 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function is_lhs(node, parent) {
AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
options = defaults(options, {
cache: null,
ie8: false,
ie: false,
});

// pass 1: setup scope chaining and handle definitions
Expand Down Expand Up @@ -211,7 +211,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
entangle(defun, scope);
} else if (node instanceof AST_SymbolLambda) {
var def = defun.def_function(node, node.name == "arguments" ? undefined : defun);
if (options.ie8) def.defun = defun.parent_scope.resolve();
if (options.ie) def.defun = defun.parent_scope.resolve();
} else if (node instanceof AST_SymbolLet) {
var def = scope.def_variable(node);
if (exported) def.exported = true;
Expand Down Expand Up @@ -351,7 +351,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
self.walk(tw);

// pass 3: fix up any scoping issue with IE8
if (options.ie8) self.walk(new TreeWalker(function(node) {
if (options.ie) self.walk(new TreeWalker(function(node) {
if (node instanceof AST_SymbolCatch) {
var scope = node.thedef.defun;
if (scope.name instanceof AST_SymbolLambda && scope.name.name == node.name) {
Expand Down Expand Up @@ -572,7 +572,7 @@ AST_Symbol.DEFMETHOD("definition", function() {
function _default_mangler_options(options) {
options = defaults(options, {
eval : false,
ie8 : false,
ie : false,
keep_fnames : false,
reserved : [],
toplevel : false,
Expand Down
6 changes: 3 additions & 3 deletions test/compress/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ issue_4705: {

issue_4720: {
options = {
ie8: true,
ie: true,
reduce_vars: true,
toplevel: true,
unused: true,
Expand Down Expand Up @@ -1629,7 +1629,7 @@ issue_4951_2: {

issue_4962_1: {
options = {
ie8: true,
ie: true,
inline: true,
reduce_vars: true,
unused: true,
Expand All @@ -1656,7 +1656,7 @@ issue_4962_1: {

issue_4962_2: {
options = {
ie8: true,
ie: true,
inline: true,
reduce_vars: true,
unused: true,
Expand Down

0 comments on commit ef5f7fc

Please sign in to comment.