最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add flatMap using babel 7? - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Sep 5, 2018 at 8:38 Stav AlfiStav Alfi 13.9k27 gold badges108 silver badges196 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 14

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)

With core.js 3.x use the following import:

import "core-js/features/array/flat-map";
发布评论

评论列表(0)

  1. 暂无评论