admin管理员组文章数量:1022956
I have this in my webpack.config.js
to create two different outputs from two sources:
module.exports = {
entry: {
'dist/index.js': ['babel-polyfill', './src/Component.jsx'],
'example/bundle.js': ['babel-polyfill', './src/Page.jsx'],
},
output: {
path: './',
filename: '[name]',
},
...
Compiling it with webpack works just fine, but if I load index.js
in a browser I get this error:
Uncaught Error: only one instance of babel-polyfill is allowed
I need babel-polyfill for both outputs. What can I do?
I have this in my webpack.config.js
to create two different outputs from two sources:
module.exports = {
entry: {
'dist/index.js': ['babel-polyfill', './src/Component.jsx'],
'example/bundle.js': ['babel-polyfill', './src/Page.jsx'],
},
output: {
path: './',
filename: '[name]',
},
...
Compiling it with webpack works just fine, but if I load index.js
in a browser I get this error:
Uncaught Error: only one instance of babel-polyfill is allowed
I need babel-polyfill for both outputs. What can I do?
Share Improve this question asked Sep 6, 2016 at 4:06 RotaretiRotareti 54.1k24 gold badges120 silver badges115 bronze badges 2- Could you clarify why specifically you want it in both entry points? It's pretty large, you really don't want to be loading it twice without a really good reason. – loganfsmyth Commented Sep 6, 2016 at 7:12
- 1 Sure! I want to create a react ponent as a npm package (dist/index.js). In order to develop the ponent in isolation, I created a small page (example/bundle.js), which loads the ponent and displays the result. When I work on the ponent I can see the results in the browser on that little page. The overhead wouldn't be a problem because the code remains small in most cases. – Rotareti Commented Sep 6, 2016 at 11:35
2 Answers
Reset to default 4When developing a library (as opposed to an application), the Babel team does not remend including babel-polyfill
in your library. We remend either:
- Assume the ES6 globals are present, thus you'd instruct your library users to load
babel-polyfill
in their own codebase. - Use
babel-runtime
by enablingbabel-plugin-transform-runtime
, which attempts to analyze your code to figure out what ES6 library functionality you are using, then rewrites the code to load the polyfilled logic frombabel-runtime
instead of from the global scope. One downside of this approach is that it has no way to polyfill new.prototype
methods likeArray.prototype.find
since it can't know iffoo.find
is an array.
So my remendation would be to remove babel-polyfill
from your dist/index.js
bundle entirely.
Use idempotent-babel-polyfill
import 'idempotent-babel-polyfill';
https://github./codejamninja/idempotent-babel-polyfill
I have this in my webpack.config.js
to create two different outputs from two sources:
module.exports = {
entry: {
'dist/index.js': ['babel-polyfill', './src/Component.jsx'],
'example/bundle.js': ['babel-polyfill', './src/Page.jsx'],
},
output: {
path: './',
filename: '[name]',
},
...
Compiling it with webpack works just fine, but if I load index.js
in a browser I get this error:
Uncaught Error: only one instance of babel-polyfill is allowed
I need babel-polyfill for both outputs. What can I do?
I have this in my webpack.config.js
to create two different outputs from two sources:
module.exports = {
entry: {
'dist/index.js': ['babel-polyfill', './src/Component.jsx'],
'example/bundle.js': ['babel-polyfill', './src/Page.jsx'],
},
output: {
path: './',
filename: '[name]',
},
...
Compiling it with webpack works just fine, but if I load index.js
in a browser I get this error:
Uncaught Error: only one instance of babel-polyfill is allowed
I need babel-polyfill for both outputs. What can I do?
Share Improve this question asked Sep 6, 2016 at 4:06 RotaretiRotareti 54.1k24 gold badges120 silver badges115 bronze badges 2- Could you clarify why specifically you want it in both entry points? It's pretty large, you really don't want to be loading it twice without a really good reason. – loganfsmyth Commented Sep 6, 2016 at 7:12
- 1 Sure! I want to create a react ponent as a npm package (dist/index.js). In order to develop the ponent in isolation, I created a small page (example/bundle.js), which loads the ponent and displays the result. When I work on the ponent I can see the results in the browser on that little page. The overhead wouldn't be a problem because the code remains small in most cases. – Rotareti Commented Sep 6, 2016 at 11:35
2 Answers
Reset to default 4When developing a library (as opposed to an application), the Babel team does not remend including babel-polyfill
in your library. We remend either:
- Assume the ES6 globals are present, thus you'd instruct your library users to load
babel-polyfill
in their own codebase. - Use
babel-runtime
by enablingbabel-plugin-transform-runtime
, which attempts to analyze your code to figure out what ES6 library functionality you are using, then rewrites the code to load the polyfilled logic frombabel-runtime
instead of from the global scope. One downside of this approach is that it has no way to polyfill new.prototype
methods likeArray.prototype.find
since it can't know iffoo.find
is an array.
So my remendation would be to remove babel-polyfill
from your dist/index.js
bundle entirely.
Use idempotent-babel-polyfill
import 'idempotent-babel-polyfill';
https://github./codejamninja/idempotent-babel-polyfill
本文标签: javascriptHow can I use babelpolyfill for multiple separated entriesoutputsStack Overflow
版权声明:本文标题:javascript - How can I use babel-polyfill for multiple separated entriesoutputs? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745587625a2157693.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论