2019 年针对 IE11 的 Polyfill

Polyfills in 2019 for IE11

现在是 2019 年,当我们没有更好的事情要做时,我们希望支持 IE11,我不得不承认我对所有可用的 polyfill 有点困惑。

据我所知,所有这些东西都应该启用更新版本的 Ecmascript,而不是修补其余部分。我有几个自定义 polyfill,例如支持自定义事件。

我认为它没有任何改变,但我正在使用:

现在在我的主脚本的顶部我有:

require('core-js');

但我仍然得到:

Object doesn't support property of method 'Symbol(Symbol.iterator)_a.Kr7pt1C'

这似乎主要是不受支持的 Ecmascript 迭代功能。

关于在问题的宏观层面上做什么的任何建议?

编辑

Symbol.iterator 实际上是由缺少的 "for ... of " polyfill。

编辑:解决方案

我的完整配置在此答案中可见

由于您使用 Babel 进行编译,因此您可以使用 @babel/preset-env 预设并将目标环境设置为 IE11*。

  1. 安装预设:yarn add @babel/preset-env --dev

  2. 在您的 Babel 配置中配置您的目标:

{
  "presets": [
    ["@babel/presets-env", {
      "targets": {
        "browsers": {
          "ie": "11"
        }
      },
    }]
  ]
}

*来自the docs

@babel/preset-env takes any target environments you've specified and checks them against its mappings to compile a list of plugins and passes it to Babel.

official documentation, it says "In order to use Iterators you must include the Babel polyfill." 您可以尝试使用 npm install --save @babel/polyfill 安装它,并在您的应用程序入口点顶部使用 require("@babel/polyfill") .

The polyfill is provided as a convenience but you should use it with @babel/preset-env and the useBuiltIns option so that it doesn't include the whole polyfill which isn't always needed. Otherwise, we would recommend you import the individual polyfills manually.

您也可以尝试导入 core-js/fn/symbol/iterator.js