Rich-Harris/butternut

Safari bug workaround

boopathi opened this issue · 1 comments

Safari throws a syntax error when declaring a top level for-loop iteration variable the same as a fn parameter

function foo(a){
  for(let b=1;;) console.log(b);
  console.log(a);
}

minifies to

function foo(o){
  for(let o=1;;) console.log(o);
  console.log(o)
}

with o reused in for scope.

Refs:

  1. Safari Bug Fix
  2. Related Issues: babel/minify#559

The bug is already shipping in Safari and minifiers have to workaround this bug for these versions of Safari. What do you think?

kzc commented

Butternut uglify test harness fix for Safari 10 loop bug:

--- a/test/test.js
+++ b/test/test.js
@@ -68,7 +68,12 @@ describe('butternut', function () {
                                                        equal(butternut.squash(code, sample.options).code, code, 'failed idempotency check');
                                                }
 
-                                               const uglified = UglifyJS.minify(sample.input);
+                                               const uglifyOptions = {
+                                                       mangle: {
+                                                               safari10: true,
+                                                       }
+                                               };
+                                               const uglified = UglifyJS.minify(sample.input, uglifyOptions);
                                                if ('code' in uglified) {
                                                        if (uglified.code.length < code.length) {
                                                                console.warn(`⚠️   uglify-es generated smaller output:\n      butternut: ${code}\n      uglify-es: ${uglified.code}`);

Related: mishoo/UglifyJS@fcd90db

Edit: fixed patch.