【问题标题】:Webpack / Babel not removing "const"Webpack / Babel 没有删除“const”
【发布时间】:2019-05-20 17:55:16
【问题描述】:

我正在编译我的应用程序并试图让它支持 IE。然而,polyfill 在我的供应商填充中留下了一个 const 语句,它破坏了 IE。

我的配置有问题吗?

网页包:

{ 

    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"
    })
];

【问题讨论】:

  • 尝试使用加载器:'babel-loader'。可能会对你有所帮助。

标签: javascript webpack babeljs polyfills babel-polyfill


【解决方案1】:

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

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

【讨论】:

  • Babel loader 似乎没有解决这个问题
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-17
    • 2019-10-18
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 2017-04-27
    • 1970-01-01
    相关资源
    最近更新 更多