babel / minify

:scissors: An ES6+ aware minifier based on the Babel toolchain (beta)
https://babeljs.io/repl
MIT License
4.39k stars 225 forks source link

simplify pulls VariableDeclaration into loop, then mangle fails to mangle same variable after loop #999

Open jleven opened 3 years ago

jleven commented 3 years ago

Describe the bug

Minification produces output where a variable is only minified sometimes within a function. Thanks for your help!

To Reproduce

Minimal code to reproduce the bug

function foo() {
  let varName = [];
  while (false) {}
  varName;
}

Actual Output

"use strict";
function foo(){
  for(var a=[];false;);
  varName
}

Expected Output

I'm not sure if it's expected to pull the varName variable into the loop in this circumstance, so the expected output is one of the following:

"use strict";
function foo(){
  for(var a=[];false;);
  a
}

or

"use strict";
function foo(){
  var a=[];
  for(;false;);
  a
}

Configuration

Turning off all minify plugins except for mangle and simplify (bug requires both).

npx babel ./src --out-dir ./lib

babel-minify version: 0.5.1

babel core : 7.11.6

babel-minify-config:

  {
      "mangle": true,
      "simplify": true,
      "evaluate": false,
      "replace": false,
      "booleans": false,
      "builtIns": false,
      "consecutiveAdds": false,
      "deadcode": false,
      "flipComparisons": false,
      "guards": false,
      "infinity": false,
      "memberExpressions": false,
      "mergeVars": false,
      "numericLiterals": false,
      "propertyLiterals": false,
      "regexpConstructors": false,
      "removeConsole": false,
      "removeDebugger": false,
      "removeUndefined": false,
      "simplifyComparisons": false,
      "typeConstructors": false,
      "undefinedToVoid": false
  }

babelrc:

module.exports = {
  presets: [
    '@babel/preset-env',
    ['minify', {
      "mangle": true,
      "simplify": true,
      "evaluate": false,
      "replace": false,
      "booleans": false,
      "builtIns": false,
      "consecutiveAdds": false,
      "deadcode": false,
      "flipComparisons": false,
      "guards": false,
      "infinity": false,
      "memberExpressions": false,
      "mergeVars": false,
      "numericLiterals": false,
      "propertyLiterals": false,
      "regexpConstructors": false,
      "removeConsole": false,
      "removeDebugger": false,
      "removeUndefined": false,
      "simplifyComparisons": false,
      "typeConstructors": false,
      "undefinedToVoid": false
    }]
  ],
  plugins: [
  ],
}

Possible solution

Not sure. But it appears the logic in this region may need to be improved, when it decides if a variable is referenced outside the loop.

ehoogeveen-medweb commented 3 years ago

I just ran into this one too. Here's my reproduction (but it's very similar): https://babeljs.io/repl#?browsers=&build=&builtIns=false&corejs=3.6&spec=false&loose=false&code_lz=EQVwzgpgBGAuBOBLAxrYBuAUJgZiAdqogPb5SwRwCqYi-A5gDISwCMAFAJRQDemUUADYsoANwCG8AHLiAttAC8UAAxYBAdwAWiYVHYTpc6AB4orbgGoLBmfLVQA9A6gBlTcRCCAJlHzFYUODQBLLiDMI-NkYAdPxQyKRgxMLRgsT0-pK2JmacWAC-2HiEsCRkFNS0DMywAExcvHHCAVF2cTjE8HqtiiroYllGUKas_VY9eXEJ-EkpaRk9w7kFRQREpOSUsDR09ABqkg18AgYDhvJQSqpxWjrQmec55lDjg20C07MQqekP2UvmApAA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=true&fileSize=false&timeTravel=false&sourceType=script&lineWrap=true&presets=env&prettier=false&targets=&version=7.15.6&externalPlugins=babel-plugin-minify-mangle-names%400.5.0%2Cbabel-plugin-minify-simplify%400.5.1&assumptions=%7B%7D