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

[Bug] DCE is failed with block expression #1035

Open SoloJiang opened 2 years ago

SoloJiang commented 2 years ago

Code

const { transform } = require('@babel/core');

console.log(
  transform(
    `let isWeb = true;
    {
      if (isWeb) {
        console.log('xyz');
      } else {
        console.log('abcd');
      }
    }
`,
    {
      plugins: ['babel-plugin-minify-dead-code-elimination'],
    }
  )
);

If code with a block expression, it will not remove the console.log('abcd');

Output

let isWeb = true;
    {
      if (isWeb) {
        console.log('xyz');
      } else {
        console.log('abcd');
      }
    }

Expected

let isWeb = true;
    {
      console.log('xyz');
    }
SoloJiang commented 2 years ago

babel-plugin-minify-dead-code-elimination v0.3.0 is the expected output.

SoloJiang commented 2 years ago

The reason of I updated the babel-plugin-minify-dead-code-elimination is the latest version @babel/traverse will remove the ThisExpression https://github.com/babel/minify/issues/1028

SoloJiang commented 2 years ago

In my view, is it a break change for @babel/traverse ?