问题
After reading the article Removing Babel's Stage Presets by babel
, I still not fully understand how to add a proposal from, for example, stage-3
(flatMap) to .babelrc
.
As far as I understand, because flatMap
can be written in ES5, then I need a polyfill and not a plugin. I installed @babel/polyfill
under --save-dev but the browser still tells me that this method doesn't exist. I guess that @babel/polyfill
doesn't cover experimental features.
回答1:
flatMap was removed from @babel/polyfill for babel 7. You need to include it directly from core-js, like
import "core-js/fn/array/flat-map";
Or if you want all of the polyfills that babel 6 used to include:
import "core-js/shim";
See: https://github.com/babel/babel/pull/8440 (or more directly, the relevant section of the v7 upgrade guide)
(Also, don't worry about having to add a new package: you already have core-js in your dependency tree; that's where babel/polyfill gets the rest of its Stage 4+ polyfills)
回答2:
With core.js 3.x use the following import:
import "core-js/features/array/flat-map";
来源:https://stackoverflow.com/questions/52180762/how-to-add-flatmap-using-babel-7