我使用babel如下编译了我的简单代码。
var aa = new Promise();
to
"use strict";
require("core-js/modules/es.object.to-string");
require("core-js/modules/es.promise");
var aa = new Promise();
但是,像IE9这样的旧浏览器无法执行该代码。因为浏览器无法解析路径core-js / modules / es.object.to-string。
我必须使用webpack来使用polyfill吗?
我不想使用webpack。
下面是我在package.json中的babel设置。
"babel": {
"presets": [
[
"@babel/preset-env",
{
"corejs": 3,
"useBuiltIns": "usage",
"targets": ">0.2%, not dead, not ie <= 11, not op_mini all"
}
],
"@babel/preset-react"
]
}
您可以在索引html文件中使用<script>
标记来包含您的polyfills脚本,并适当地排序您的polyfills和应用程序脚本。 require
不是浏览器可识别的命令。它是来自node.js的服务器端实现。
您可能考虑改用import
?
import 'core-js';
或此处显示的特定模块https://github.com/zloirock/core-js
import 'core-js/features/promise';
->将修改全局名称空间
import Promise from 'core-js-pure/features/promise';
->将不会修改全局名称空间