在应用程序中包括Polyfill捆绑包

问题描述 投票:0回答:1

我需要在我的应用程序中支持IE,因此我决定使用polyfill.io来支持ES6功能。https://polyfill.io/v3/

它给了我一个URL,但是我不确定如何实际使用它。我正在使用webpack,但到目前为止我找不到解决方案。

我上次尝试的是尝试用ajax请求加载它

//Version 1
$.ajax({
  type: 'GET',
  url: "https://polyfill.io/v3/polyfill.min.js?features=String.prototype.includes",
  success: Srv.main,
  dataType: 'script',
}).done(() => appStart());

//Version 2
$.getScript("https://polyfill.io/v3/polyfill.min.js?features=String.prototype.includes", function (data, textStatus, jqxhr) {
 appStart();
});

两者似乎都返回了脚本,并且实际上它似乎一开始就可以工作,关于appStart中String.include的操作没有错误。但是我在其他使用方法的模块中遇到了错误。所以它似乎不是全局可用的?

我将不胜感激,也许有人可以通过webpack找到一个更简单的解决方案?

提前感谢!

javascript webpack polyfills
1个回答
1
投票

我将使用core-js将ECMAscript功能多填充到您的应用程序中。

npm i core-js
import 'core-js/stable'; // <- at the top of your entry point

Array.from(new Set([1, 2, 3, 2, 1]));          // => [1, 2, 3]
[1, [2, 3], [4, [5]]].flat(2);                 // => [1, 2, 3, 4, 5]
Promise.resolve(32).then(x => console.log(x)); // => 32

编辑

如果只想包含所需的内容,则可以编辑.babelrc文件以包括“ useBuiltIns”选项

// .babelrc

{
  "presets": [
    ["@babel/preset-env", {
      "targets": [
        "Last 2 versions",
      ]

      // This option configures how @babel/preset-env handles polyfills. The 'usage' value imports
      // only the specific polyfill module when they are used in each file.
      "useBuiltIns": "usage"
    }]
  ],
}
© www.soinside.com 2019 - 2024. All rights reserved.