我可以导入babel polyfill模块而不是all in吗

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

本文关键字:all in 导入 babel polyfill 模块 我可以      更新时间:2023-10-29

如何导入babel polyfill的某些特定模块而不是全部导入?它似乎太大了,我无法导入所有的,我只是使用了它的一些功能。

我想要的如下:

import "babel-polyfill/symbol";

在后台,babel-polyfill使用了一个名为core-js的项目(当然还有一些定制)。它公开了一个CommonJS API,所以假设您正在传输到CommonJS(使用preset-es2015时的默认行为),您可以简单地使用它:

// 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";

使用这种方法时,使用某种捆绑器(Browserify、Webpack等)是很重要的,因为core-js由许多较小的模块组成,可能会导致大量不必要的HTTP请求。

如果您不希望导入整个babel polyfill以进行优化,则可以直接导入core-js。Core js可以

//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';