简体   繁体   中英

How to add flatMap using babel 7?

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.

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 )

对于core.js 3.x,请使用以下导入:

import "core-js/features/array/flat-map";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM