【发布时间】:2020-08-14 06:56:36
【问题描述】:
我尝试捆绑两个 JavaScript 模块,以便生成的代码在 IE11 中工作。为此,我设置了一个 yarn/npm 项目,它使用 rollup.js 进行捆绑,使用 Babel 进行转译。在我添加(非开发)依赖项core-js 之前,一切正常。
这里有详细信息:
1 设置之前 添加core-js
JS 文件
- src/main.js
- src/utils.js
配置文件
package.json
{
"name": "rollup_for_ie",
"devDependencies": {
"@babel/core": "^7.11.1",
"@babel/preset-env": "^7.11.0",
"@rollup/plugin-babel": "^5.2.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"rollup": "^2.24.0"
},
}
rollup.config.js
import resolve from '@rollup/plugin-node-resolve';
import babel from '@rollup/plugin-babel';
export default {
input: 'src/main.js',
output: {
file: 'dist/main.js',
format: 'iife'
},
plugins: [
resolve({
browser: true
}),
babel({
exclude: "node_modules/**", // only transpile our source code
babelHelpers: 'bundled'
})
]
};
babel.config.js
module.exports = {
presets: [
[
"@babel/preset-env",
{
targets: {
browsers: "> 0.5%, ie >= 11"
},
modules: false,
spec: true,
useBuiltIns: "usage",
forceAllTransforms: true,
corejs: 3
}
]
],
};
当我运行rollup -c 时,我收到有关未解决依赖项的警告,但会生成一个捆绑文件dist/main.js,其中包含src 目录中的(使用过的)内容。生成的文件甚至可以在 Chrome 等现代浏览器中使用。到目前为止一切顺利。
问题 添加core-js
但是捆绑的文件还没有 IE11 准备好:在 IE 中我收到类似 Object 不支持属性或方法“getOwnPropertySymbols”的错误。所以需要添加来自 core-js 的 polyfill。
为此,我将 core-js 安装为 prod 依赖项。现在rollup -c 没有给我警告 - 但结果 dist/main.js 开始像
(function (exports) {
'use strict';
var $ = require('../internals/export');
.
.
.
Chrome 和 IE 都不能作为脚本执行!看起来 core-js 库的存在以某种方式使汇总捆绑器关闭。
如何修复我的设置,以便生成的 dist/main.js 在 Chrome 和 IE11 中用作非模块脚本?
【问题讨论】:
-
你能准确地说出添加 core-js 后你做了什么吗?您的意思是在 babel.config 文件中添加
corejs选项吗? -
@tmhao2005 我的意思是我将 core-js 作为产品依赖添加到 npm 模块中。我用
yarn add core-js这样做了。 babel 配置没有改变。 -
你能分享导致错误的最小代码吗?包 json 和主入口点包含错误
-
@tmhao2005 你的意思是浏览器的错误? 添加 core-js 后,我得到“需要未定义”。
rollup -c命令在这两种情况下都没有错误地完成。在第一种情况下,我收到“(!)未解决的依赖项”之类的警告。 -
我知道了。我将尝试通过包含问题的代码进行复制,然后尝试构建并查看捆绑的文件。有意义吗?
标签: javascript internet-explorer module babeljs rollupjs