A set of Babel plugins that enable injecting different polyfills with different strategies in your compiled code.
A set of Babel plugins that enable injecting different polyfills with different strategies in your compiled code. Additionally, this repository contains a package that helps with creating providers for other polyfills.
⚠️ These packages are highly experimental and they have not been tested in production applications yet. Also, we are still working on wiring some missing polyfills.
ℹ️ This repository implements what was initially proposed at babel/babel#10008.
💡 If you are looking for some quick setup examples, or just want to see how to migrate your config, please check
docs/migration.md.
The main Babel packages only transform JavaScript syntax: you also need to load a polyfill, to make native functions (
Array.prototype.flat) or built-in objects (
Reflect) work in older browsers.
The easiest way to do so is to directly load the polyfill using a
tag:html
However, this simple approach can potentially include a lot of unnecessary code. The Babel plugins implemented in this repository automatically inject the polyfills in your code, while trying to only load what is really needed. It does this based on your compilation targets and on what you are using in your code.
These plugins (we are calling them "polyfill providers") support different injection methods, to better fit your needs.
For example, if you want to inject imports to
es-shimspolyfills by adding the missing functions to the global objects, you could configure Babel as such:
Configuration | Input code | Output code |
---|---|---|
|
|
|
If you want to see more configuration examples, you can check the migration docs:
docs/migration.md.
If you are interested in reading about all the options supported by these plugins, you can check the usage docs:
docs/usage.md.
| Polyfill | Plugin | Methods | | :------: | :----: | :-----: | |
[email protected]|
babel-plugin-polyfill-corejs2|
entry-global,
usage-globaland
usage-pure| |
[email protected]|
babel-plugin-polyfill-corejs3|
entry-global,
usage-globaland
usage-pure| |
es-shims|
babel-plugin-polyfill-es-shims|
usage-globaland
usage-pure| |
regenerator-runtime|
babel-plugin-polyfill-regenerator|
entry-global,
usage-globaland
usage-pure|
💡 We are maintaining support for
core-jsandes-shims, but we encourage you to implement a provider for your own polyfill, or for your favorite one! One of our goals is to encourage competition between different polyfills, to better balance the different trade offs like spec compliancy and code size.If you want to implement support for a custom polyfill, you can use
@babel/helper-define-polyfill-provider. (docs/polyfill-provider.md.)
Polyfill plugins can expose three different injection methods:
entry-global,
usage-globaland
usage-pure. Note that polyfill plugins don't automatically add the necessary package(s) to your dependencies, so you must explicitly list them in your
package.json.
ℹ️ All the examples assume that you are targeting Chrome 62.
entry-globalmethod replaces a single simple import to the whole polyfill with imports to the specific features not supported by the target environments. It is most useful when you want to be sure that every unsupported function is available, regardless of what you are using in the code you are compiling with Babel. You might want to use this method if:
you want to have a single bundled file containing all the polyfills, without needing to regenerate it when your code changes.
Input code | Output code |
---|---|
|
|
usage-globalmethod injects imports to polyfills attached to the global scope, but only for unsupported features which are used in your code. You might want to use this method if:
your polyfill doesn't support a single entry point, but each of its features must be loaded separately.
Input code | Output code |
---|---|
|
|
usage-puremethod injects imports to polyfills for unsupported features which are used in your code, without attaching the polyfills to the global scope but importing them as normal functions. You might want to use this method if:
you are a library author, and don't want to "pollute" the global scope with the polyfills you are loading.
Input code | Output code |
---|---|
|
|
In the last three years and a half,
@babel/preset-envhas shown its full potential in reducing bundle sizes not only by not transpiling supported syntax features, but also by not including unnecessary
core-jspolyfills.
So far Babel provided three different ways to inject
core-jspolyfills in the source code:
@babel/preset-env's
useBuiltIns: "entry"option, it is possible to inject polyfills for every ECMAScript functionality not natively supported by the target browsers;
@babel/preset-env's
useBuiltIns: "usage", Babel will only inject polyfills for unsupported ECMAScript features but only if they are actually used in the input souce code;
@babel/plugin-transform-runtime, Babel will inject ponyfills (which are "pure" and don't pollute the global scope) for every used ECMAScript feature supported by
core-js. This is usually used by library authors.
Our old approach has two main problems:
@babel/preset-env's
targetsoption with "pure" ponyfills, because
@babel/plugin-transform-runtimeis a completely separate package.
core-jsif they wanted a Babel integration.
core-jsis a good and comprehensive polyfill, but it doesn't fit the needs of all of our users.
With this new packages we are proposing a solution for both of these problem, while still maintaining full backward compatibility.