Webpack / Babel不删除“ const”

时间:2019-05-20 17:55:16

标签: javascript webpack babel polyfills babel-polyfill

我正在编译我的应用程序,并尝试使其支持IE。但是,polyfill在我的供应商填充中留下了const语句,这会破坏IE。

我的配置有问题吗?

Webpack:

{ 

    mode: "production",

    entry: {
        app: ["whatwg-fetch", "@babel/polyfill", "./src/app/app.js"]
    },

    output: {
        path: path.resolve(
            __dirname,
            "temp/" + envData.environment + "/app/js"
        ),
        filename: "[name].bundle.js",
        publicPath: "/"
    },

    optimization: {
        splitChunks: {
            cacheGroups: {
                commons: {
                    test: /[\\/]node_modules[\\/]/,
                    name: "vendor",
                    chunks: "initial"
                }
            }
        }
    },

    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                loader: "happypack/loader",
                options: { babelrc: true, cacheDirectory: "./cache" }
            }
        ]
    }
}

Babelrc:

{
    "presets": [
        [
            "@babel/preset-env",
            {
                "targets": {
                    "chrome": "55",
                    "ie": "8"
                }
            }
        ]
    ]
}

编辑:对不起,我忘了包括我快乐的加载器配置,它确实通过babel-loader运行我的代码:

let plugins = [
    new HardSourceWebpackPlugin(),
    new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
    new HappyPack({
        loaders: ["babel-loader"]
    }),
    new LiveReloadPlugin({
        hostname: "localhost"
    })
];

2 个答案:

答案 0 :(得分:1)

安装babel-loader并尝试以下配置:

module: {
  rules: [
    {
      test: /\.m?js$/,
      exclude: /(node_modules|bower_components)/,
      use: {
        loader: 'babel-loader',
        options: {
          presets: ['@babel/preset-env']
        }
      }
    }
  ]
}

答案 1 :(得分:0)

babel-polyfil对此进行了介绍。升级到> 7.4.4进行修复。

https://github.com/babel/babel/issues/9854