webpack - 使用 Babel 为 IE11 转译 ES6

标签 webpack babeljs

我是 babel 的新手,正在尝试转换我的 es6 代码以与 IE11 一起使用。但是当我在 IE11 中运行代码时,我收到关于我的 forEach 代码的 js 错误。根据我的阅读,我需要添加预设 @babel/preset-env。我将它添加到我的配置文件中,所以我不确定为什么它不转换那些 forEach 调用。

const path = require('path');

module.exports = {
    entry: {
        setupForm: "./Scripts/es6/setupForm.js",
        prelimForm: "./Scripts/es6/prelimForm.js"
    },
    output: {
        filename: '[name].js',
        path: path.resolve(__dirname, './Scripts/build'),
    },
    module: {
        rules: [{
            loader: 'babel-loader',
            test: /\.js$/,
            exclude: /node_modules/,
            query: {
                presets: ['@babel/preset-env']
            }
        }]
    }
}

我认为也许我需要额外引用 babel polyfill.js 作为 discussed here所以我将它添加到我的页面,但是,我收到关于 Object doesn't support property or method 'forEach' 的相同错误。

这是我的 package.json 文件。

{
  "name": "OurSite",
  "version": "1.0.0",
  "description": "",
  "main": "map_embed.js",
  "directories": {
    "doc": "docs"
  },
  "scripts": {
    "build": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.4.5",
    "@babel/preset-env": "^7.4.5",
    "babel-core": "^6.26.3",
    "babel-loader": "^8.0.6",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.7.0",
    "webpack": "^4.32.2",
    "webpack-cli": "^3.3.2"
  },
  "babel": {
    "presets": [
      "env"
    ]
  },
  "dependencies": {}
}

最佳答案

从 Babel 7.4.0 开始,@babel/polyfill 已被弃用,取而代之的是 core-jsregenerator 包。

https://babeljs.io/docs/en/babel-polyfill#docsNav

要么包含在webpack中

module.exports = {
  entry: {
    main: [
      'core-js/stable',
      'regenerator-runtime/runtime',
      'src/main.js'
    ]
  }
}

或者在你的 js 中包含在顶部

import 'core-js/stable';
import 'regenerator-runtime/runtime';

关于webpack - 使用 Babel 为 IE11 转译 ES6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56446904/

相关文章:

javascript - 在 Node.js 上使用动态 import() 函数

reactjs - 跟踪 : The node type SpreadProperty has been renamed to SpreadElement at Object. isSpreadProperty

javascript - requestAnimationFrame 仅在不使用类的情况下工作,我该如何修复它?

node.js - 如何在Docker容器中运行我的React应用

node.js - Webpack 生产 process.env.PORT=在服务器编译时未定义(React 应用程序)

webpack - 如何根据环境变量自定义 Service Worker?

reactjs - 如何在没有 Webpack 的情况下使用 Babel

javascript - Webpack 4 从输出散列中排除条目

javascript - 模块解析失败 : Unexpected token - react-refresh-webpack-plugin/loader

babeljs - 如何为 vue 项目添加 Babel 对 nullishCoalescingOperator 的支持?