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

After executing my plug-in after the minify-dead-code-elimination, I still get the deleted dead code in my plug-in #1014

Open 951565664 opened 3 years ago

951565664 commented 3 years ago

After executing my plug-in after the minify-dead-code-elimination, I still get the deleted dead code in my plug-in

To Reproduce

Minimal code to reproduce the bug

babel.config.js

const path = require('path');

module.exports = {
  plugins: [
      "minify-dead-code-elimination",
      path.resolve(__dirname, 'plugin/my-plugin.js')
  ]
};

src/index.js

import strA from './a.js'
console.log('b')
if(false){
  console.log(strA)
}

my-plugin.js

```js
module.exports = function (babel) {
  return {
    visitor: {
      Identifier(path) {
        const { name } = path.node
        name === 'strA' && console.log(path.node)
      }
    }
  };
};

Actual Output

Print the result of console.log

Node {
  type: 'Identifier',
  start: 68,
  end: 72,
  loc: SourceLocation {
    start: Position { line: 4, column: 14 },
    end: Position { line: 4, column: 18 },
    filename: undefined,
    identifierName: 'strA'
  },
  range: undefined,
  leadingComments: undefined,
  trailingComments: undefined,
  innerComments: undefined,
  extra: undefined,
  name: 'strA'
}

so console.log(strA) in the if(false) code block is not deleted

Expected Output

When my plug-in is behind the deadcode plug-in, my plug-in should not get the dead code that was removed

"babel-plugin-minify-dead-code-elimination": "^0.5.1" "@babel/core": "7.14.6",