简体   繁体   中英

Can I import babel-polyfill modules rather than all in?

How can I import some specific modules of babel-polyfill rather than import all ? It seems too huge size for me to import all of it and I just use few of the features of it.

What I want is like following:

import "babel-polyfill/symbol";

Under the hood, the babel-polyfill uses a project called core-js (with some customisations of course). It exposes a CommonJS API , so assuming you are transpiling to CommonJS (default behaviour when using preset-es2015 ), you could simply use that instead:

// This pollutes the global namespace. May conflict with
// any real Symbol implementations in the future
import "core-js/es6/symbol";
// Namespace-safe Symbol import
import symbol from "core-js/es6/symbol";

It's important with this approach that you use a bundler of some sort (Browserify, Webpack, etc), because core-js is made up of a lot of smaller modules and may cause a lot of unnecessary HTTP requests.

You can import core-js directly if you do not wish to import entire babel-polyfill in order to optimize. Core js can

//import 'babel-polyfill';  

//Selective imports in modular pattern
import 'core-js/fn/object/assign';
import 'core-js/fn/promise';
import 'core-js/fn/string/includes';

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