What is @babel/runtime-corejs3?
The @babel/runtime-corejs3 package is a runtime library for Babel that includes polyfills for ECMAScript features using core-js version 3. It is used alongside Babel to enable the use of new JavaScript features in environments that do not natively support them. This package helps to avoid polluting the global scope and reduces the size of the compiled code by deduplicating helper functions.
What are @babel/runtime-corejs3's main functionalities?
Polyfilling ECMAScript features
This code imports polyfills for stable ECMAScript features and the regenerator runtime needed to use generators and async functions.
import 'core-js/stable';
import 'regenerator-runtime/runtime';
Usage of built-in methods
This code demonstrates how to use the map method from core-js through the runtime without polluting the global Array prototype.
import { map } from '@babel/runtime-corejs3/core-js-stable/instance/map';
let arr = [1, 2, 3];
let mapped = map.call(arr, x => x * x);
Usage of new syntax features
This code shows how to use the helpers provided by @babel/runtime-corejs3 to work with new syntax features like async functions and generators.
import _asyncToGenerator from '@babel/runtime-corejs3/helpers/asyncToGenerator';
import _regeneratorRuntime from '@babel/runtime-corejs3/regenerator';
const asyncFunc = _asyncToGenerator(function* () {
yield Promise.resolve('Hello World');
});
Other packages similar to @babel/runtime-corejs3
core-js
core-js is a modular standard library for JavaScript, including polyfills for ECMAScript up to 2021. It's similar to @babel/runtime-corejs3 but does not require Babel to work and can be included directly in any JavaScript project.
regenerator-runtime
regenerator-runtime is a standalone runtime for Regenerator-compiled generator and async functions. It is similar to the regenerator part of @babel/runtime-corejs3 but does not include other polyfills or helpers.
@babel/polyfill
Deprecated in favor of separate core-js and regenerator-runtime packages, @babel/polyfill was a package that included Babel's polyfills for ECMAScript features. It was similar to @babel/runtime-corejs3 but was designed to be included globally rather than modularly.
@babel/runtime-corejs3
babel's modular runtime helpers with core-js@3 polyfilling
Install
Using npm:
npm install --save-dev @babel/runtime-corejs3
or using yarn:
yarn add @babel/runtime-corejs3 --dev