【问题标题】:babel polyfill being included, but forEach still doesn't work in IE11 on NodeLists包含 babel polyfill,但 forEach 在 NodeLists 上的 IE11 中仍然不起作用
【发布时间】:2018-11-16 03:52:42
【问题描述】:

我已经让 Webpack 与 Babel 一起工作,并包含 @babel/polyfill,但 IE11 在尝试在 NodeList 上使用 .forEach 时仍然抛出 SCRIPT438 错误。

这是我的package.json

{
  ...
  "scripts": {
    "build:js": "webpack --config ./_build/webpack.config.js"
  },
  ...
  "browserslist": [
    "IE 11",
    "last 3 versions",
    "not IE < 11"
  ],
  "babel": {
    "presets": [
      [
        "@babel/preset-env",
        {
          "useBuiltIns": "usage"
        }
      ]
    ]
  },
  "devDependencies": {
    "@babel/core": "^7.1.6",
    "@babel/preset-env": "^7.1.6",
    "babel-loader": "^8.0.4",
    "webpack": "^4.25.1",
    "webpack-cli": "^3.1.2"
  },
  "dependencies": {
    "@babel/polyfill": "^7.0.0"
  }
}

我的webpack.config.js

const path = require('path');
const webpack = require('webpack');

module.exports = (env, argv) => {

  const javascript = {
    test: /\.js$/,
    use: {
      loader: 'babel-loader'
    }
  };

  // config object
  const config = {
    entry: {
      main: './_src/js/main.js',
    },
    devtool: 'source-map',
    output: {
      path: path.resolve(__dirname, '../js'),
      filename: '[name].js',
    },
    module: {
      rules: [javascript]
    }
  }

  return config;
}

最后是 /_src/main.js 我正在通过 webpack 和 babel 运行:

const testList = document.querySelectorAll('.test-list li');

testList.forEach(item => {
  console.log(item.innerHTML);
})

https://babeljs.io/docs/en/babel-polyfill 的文档说,当通过带有 useBuiltIns: "usage" 的 Webpack 加载它时,您不需要 importrequire polyfill。但即使我删除该选项并在main.js 顶部手动导入整个polyfill(使我的包变得很大),它仍然会在IE11 中出错。

那么……我做错了什么?

【问题讨论】:

标签: javascript webpack babeljs npm-scripts babel-polyfill


【解决方案1】:

更新:从 Babel 7.4.0 开始,Babel 已切换为直接使用 core-js,而不是使用 @babel/polyfill 包装它。 core-js 已经在 NodeList 上填充了 forEach,因此不再需要额外的 polyfill。


babel-polyfill 不会填充缺少的 Web API/原型方法,例如 NodeList.prototype.forEach

另外请注意,您的问题标题具有误导性,因为 NodeList.prototype.forEach 不是 ES6 功能。可迭代集合上的forEach 目前只是候选推荐(截至 2018 年 8 月)。

只需在 Javascript 的顶层包含您自己的 polyfill:

if (window.NodeList && !NodeList.prototype.forEach) {
    NodeList.prototype.forEach = Array.prototype.forEach;
}

这可能会在 core-js 3 稳定后立即改变:https://github.com/zloirock/core-js/issues/329

如果你开始采用 ES6 时代使用的通用模式,你也可以不使用任何 polyfill:

const testList = [...document.querySelectorAll('.test-list li')];

const testList = Array.from(document.querySelectorAll('.test-list li'));

您的另一个选择是改用for...of

const lis = document.querySelectorAll('.test-list li');
for (const li of lis) {
  // li.addEventListener(...) or whatever
}

最后,你也可以采用常见的 ES5 模式:

var testList = document.querySelectorAll('.test-list li');
Array.prototype.forEach.call(testList, function(li) { /*whatever*/ });

【讨论】:

  • Array.from() 不适用于 IE11,因为它不受支持。
  • @RamyRais 这就是为什么我还添加了 ES5 方式。 IE 11 不支持除 constlet 关键字之外的任何 ES6 功能。
猜你喜欢
  • 2020-05-07
  • 2017-11-26
  • 1970-01-01
  • 2019-09-13
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 2021-03-12
  • 1970-01-01
相关资源
最近更新 更多