babel-plugin-polyfill-corejs2
raw JSON → 0.4.17 verified Sat Apr 25 auth: no javascript maintenance
Babel plugin that automatically injects imports for core-js@2 polyfills based on language features used in the code. Current stable version is 0.4.17, released as part of the Babel polyfills project. Unlike manual core-js imports or @babel/polyfill, this plugin offers fine-grained control via methods like 'usage-global' (per-file polyfill injection into global scope), 'usage-pure' (pure expressions without side effects), and 'entry-global' (replaces core-js import entries). Requires @babel/core peer dependency. Development is focused on core-js@2 compatibility; for core-js@3 use babel-plugin-polyfill-corejs3.
Common errors
error Cannot find module 'core-js' ↓
cause core-js@2 not installed as a dependency.
fix
Install core-js@2: npm install core-js@2
error Error: [BABEL] unknown: You gave us a visitor for the node type "ImportDeclaration" but it's not a valid type ↓
cause Using incompatible version of @babel/core (older than 7.4.0).
fix
Update @babel/core to ^7.4.0.
error ReferenceError: regeneratorRuntime is not defined ↓
cause Plugin does not include regenerator-runtime; needed for async/await.
fix
Add @babel/plugin-transform-regenerator or manually import regenerator-runtime.
error The 'entry-global' method did not inject any polyfills ↓
cause No import statement for core-js found in the file; method expects an explicit import like 'import "core-js"'.
fix
Add 'import "core-js"' at the entry point of your application, or switch to 'usage-global' method.
Warnings
deprecated core-js@2 is deprecated; consider migrating to core-js@3 and babel-plugin-polyfill-corejs3. ↓
fix Replace this plugin with babel-plugin-polyfill-corejs3 and core-js@3.
gotcha The plugin does not automatically install core-js; you must add core-js@2 as a dependency in your package.json. ↓
fix Run 'npm install core-js@2' or equivalent.
breaking From version 0.2.0, the plugin replaces global core-js imports only when 'entry-global' method is used; prior versions defaulted differently. ↓
fix Update to 0.4.x and explicitly set method to 'entry-global' if you previously relied on global injection.
gotcha Using 'usage-pure' method may cause polyfill duplication if multiple files use the same feature. Use 'usage-global' for single global polyfills. ↓
fix Select method carefully: 'usage-pure' for library development, 'usage-global' for applications.
deprecated The 'entry-global' method replaces core-js entries but does not handle proposals; use @babel/plugin-proposal-* for proposals. ↓
fix Add corresponding proposal plugins (e.g., @babel/plugin-proposal-optional-chaining) alongside this plugin.
Install
npm install babel-plugin-polyfill-corejs2 yarn add babel-plugin-polyfill-corejs2 pnpm add babel-plugin-polyfill-corejs2 Imports
- default wrong
const plugin = require('babel-plugin-polyfill-corejs2'); // Works in CJS but not recommended for ESM-only projectscorrectimport plugin from 'babel-plugin-polyfill-corejs2'; // In Babel config file (ESM) - Method options wrong
// Using invalid method: { plugins: [['polyfill-corejs2', { method: 'global' }]] }correct// In Babel config: { plugins: [['polyfill-corejs2', { method: 'usage-global' }]] } - Programmatic usage wrong
const babel = require('@babel/core'); const plugin = require('babel-plugin-polyfill-corejs2'); babel.transform(code, { plugins: [plugin] }); // Missing options or incorrect methodcorrectimport babel from '@babel/core'; import plugin from 'babel-plugin-polyfill-corejs2'; babel.transformSync(code, { plugins: [[plugin, { method: 'usage-pure' }]] });
Quickstart
npm install --save-dev babel-plugin-polyfill-corejs2 core-js@2 @babel/core