问题
DISCLAIMER: I'm not terribly familiar with webpack or babel outside of simple setup, so if the question isn't clear then I apologize and will do my best to offer further clarity.
So, the situation currently is that a coworker updated a bunch of packages recently, babel among them, and babel is no longer transpiling the code properly for .forEach and spread operators in ie11 (specifically when iterating over a node list). The resulting behavior is a bit frustrating; simply put, nothing happens when the page is loaded in those browsers, no console errors, just nothing.
While troubleshooting this, I've been able to get it fixed by adding core-js as a dependency in package.json and adding the following imports to the main.js file:
import 'core-js/stable/array/for-each';
import 'core-js/stable/array/from';
import 'core-js/stable/dom-collections';
import 'core-js/stable/object/get-own-property-symbols';
The question is, is there a way to get this same result purely through the webpack config? Again, I'm not all that familiar with how to play around in webpack outside of some basic common setup tasks, so I hope I'm phrasing this in a way that makes sense. If not, I'll do my best to correct based on feedback.
回答1:
You can add this by webpack. Take a look at the documentation https://github.com/zloirock/core-js#babelpreset-env You need babel .babelrc
{
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "usage",
"corejs": 3
}
]
]
}
Then you do not need to add dependency in main.js all dependencies are added automatically by core-js
I prepared the code https://github.com/tomik23/webpack-babel-corejs
来源:https://stackoverflow.com/questions/56799222/is-there-a-way-to-setup-webpack-config-to-load-specific-core-js-entries