gpt4 book ai didi

javascript - 我如何将 babel-polyfill 用于多个单独的条目/输出?

转载 作者:行者123 更新时间:2023-11-29 10:08:03 25 4
gpt4 key购买 nike

我的 webpack.config.js 中有这个,用于从两个来源创建两个不同的输出:

module.exports = {
entry: {
'dist/index.js': ['babel-polyfill', './src/Component.jsx'],
'example/bundle.js': ['babel-polyfill', './src/Page.jsx'],
},
output: {
path: './',
filename: '[name]',
},
...

webpack 编译它工作得很好,但是如果我在浏览器中加载 index.js 我会得到这个错误:

Uncaught Error: only one instance of babel-polyfill is allowed

两个输出都需要 babel-polyfill。我能做什么?

最佳答案

在开发库(而不是应用程序)时,Babel 团队不建议在您的库中包含 babel-polyfill。我们建议:

  1. 假设存在 ES6 全局变量,因此您将指示您的库用户在他们自己的代码库中加载 babel-polyfill
  2. 通过启用 babel-plugin-transform-runtime 使用 babel-runtime,它会尝试分析您的代码以确定您正在使用的 ES6 库功能,然后重写从 babel-runtime 而不是从全局范围加载 polyfilled 逻辑的代码。这种方法的一个缺点是它无法填充新的 .prototype 方法,比如 Array.prototype.find 因为它不知道 foo.find 是一个数组。

所以我的建议是从你的 dist/index.js 包中完全删除 babel-polyfill

关于javascript - 我如何将 babel-polyfill 用于多个单独的条目/输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39340491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com