Rich-Harris / butternut

The fast, future-friendly minifier
https://butternut.now.sh
MIT License
1.17k stars 17 forks source link

Safari bug workaround #168

Open boopathi opened 7 years ago

boopathi commented 7 years ago

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: https://github.com/babel/babili/issues/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 7 years ago

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: https://github.com/mishoo/UglifyJS2/commit/fcd90db30d9af157a4a8e36490bcbb861a3a5884

Edit: fixed patch.