Webpack树摇可以移除未使用的babel-polyfills吗?

10
我将尝试让webpack tree-shaking 移除未使用的 babel-polyfillindex.js 文件包含:
import 'babel-polyfill'
const add4 = n => n + 4
const add5 = n => n + 5
add4(6)

console.log('boom', add4(4))

在这个文件中,没有任何需要使用es2015+ polyfill的代码,因此我期望通过摇树优化来删除捆绑输出中未使用的babel-polyfills。但事实并非如此,打包仍包含它们(经过了缩小)。 这里是一个具有此代码的git存储库
webpack配置:
const MinifyPlugin = require('babel-minify-webpack-plugin')

module.exports = {
  entry: './index.js',
  output: {
    filename: 'bundle-webpack.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      }
    ]
  },
  plugins: [
    new MinifyPlugin({ mangle: { topLevel: true } }, { comments: false })
  ]
}

.babelrc

{
  "presets": [
    [
      "env",
      {
        "targets": {
          "browsers": ["chrome >= 50", "iOS >= 10", "ie >= 8"]
        },
        "useBuiltIns": true,
        "modules": false,
        "loose": true
      }
    ]
  ]
}

我尝试使用uglifyjs-webpack-plugin替换babel-minify-webpack-plugin,但结果相同。
如何使树摇动起来,并且输出不包含在原始代码中未使用的babel-polyfills
1个回答

2
现在我意识到babel-polyfill修改了全局范围,而树摇可能没有效果......这是个愚蠢的问题 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接