javascript - Webpack/Babel 未删除 "const"

标签 javascript webpack babeljs polyfills babel-polyfill

我正在转译我的应用程序并尝试使其支持 IE。然而,polyfill 在我的 vendor 填充中留下了一个 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 并尝试以下配置:

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

关于javascript - Webpack/Babel 未删除 "const",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56225937/

相关文章:

javascript - 获取数组中的对象,该对象具有一个属性,该属性是一个包含匹配值的数组

node.js - 为 vue-cli 运行 "npm run dev"时出现跨环境问题

reactjs - React Js需要 'fs'

node.js - NPM 模块 : react-csv-downloader on install gives Babel not found error

reactjs - Webpack:是否可以仅在生产中隐藏源 map ?

javascript - 使用 babel 和 webpack 减少转译代码助手

javascript - 数组元素的未定义值

javascript - 禁用 Csrf 进行 ajax 查询

javascript - 待完成的高亮区域

javascript - 将函数传递给使用 webpack 导入类的 web worker