使用 Babel 为 IE11 转译 ES6

geo*_*rtz 2 webpack babeljs

我是 babel 的新手,并试图将我的 es6 代码转换为与 IE11 一起使用。但是当我在 IE11 中运行代码时,我的代码出现 js 错误forEach。从我读过的内容来看,我需要添加预设@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']
            }
        }]
    }
}
Run Code Online (Sandbox Code Playgroud)

我想也许我需要额外引用这里讨论的 babel polyfill.js,所以我将它添加到我的页面,但是,我收到了关于Object does not 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": {}
}
Run Code Online (Sandbox Code Playgroud)

min*_*are 11

自 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'
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

或包含在您的 js 顶部

import 'core-js/stable';
import 'regenerator-runtime/runtime';
Run Code Online (Sandbox Code Playgroud)