• Stars
    star
    330
  • Rank 127,657 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A set of Babel plugins that enable injecting different polyfills with different strategies in your compiled code.

Babel Polyfills

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.

โ„น๏ธ 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.

Usage

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 <script ...> tag:

<script src="https://unpkg.com/[email protected]/minified.js"></script>

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-shims polyfills by adding the missing functions to the global objects, you could configure Babel as such:

Configuration Input code Output code
{
  "targets": { "firefox": 65 },
  "plugins": [
    ["polyfill-es-shims", {
      "method": "usage-global"
    }]
  ]
}
Promise.allSettled([
  p1,
  p2
]).finally(() => {
  console.log("Done!");
});
import "promise.prototype.finally/auto";
import "promise.allsettled/auto";

Promise.allSettled([
  p1,
  p2
]).finally(() => {
  console.log("Done!");
});

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.

Supported polyfills

Polyfill Plugin Methods
core-js@2 babel-plugin-polyfill-corejs2 entry-global, usage-global and usage-pure
core-js@3 babel-plugin-polyfill-corejs3 entry-global, usage-global and usage-pure
es-shims babel-plugin-polyfill-es-shims usage-global and usage-pure
regenerator-runtime babel-plugin-polyfill-regenerator entry-global, usage-global and usage-pure

๐Ÿ’ก We are maintaining support for core-js and es-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.)

Injection methods

Polyfill plugins can expose three different injection methods: entry-global, usage-global and 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.

  • The entry-global method 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:

    1. you are not compiling your dependencies, but you want to be sure that they have all the necessary polyfills;
    2. Babel's detection logic isn't smart enough to understand which functions you are using;
    3. 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
    import "core-js";
    import "core-js/modules/es7.array.flat-map.js";
    import "core-js/modules/es6.array.sort.js";
    import "core-js/modules/es7.promise.finally.js";
    import "core-js/modules/es7.symbol.async-iterator.js";
    import "core-js/modules/es7.string.trim-left.js";
    import "core-js/modules/es7.string.trim-right.js";
    import "core-js/modules/web.timers.js";
    import "core-js/modules/web.immediate.js";
    import "core-js/modules/web.dom.iterable.js";
  • The usage-global method 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:

    1. you need to keep your code size as small as possible, and only include what is effectively used;
    2. your polyfill doesn't support a single entry point, but each of its features must be loaded separately.
    Input code Output code
    foo.flatMap(x => [x, x+1]);
    bar.trimLeft();
    arr.includes(2);
    import "core-js/modules/es.array.flat-map.js";
    import "core-js/modules/es.array.unscopables.flat-map.js";
    import "core-js/modules/es.string.trim-start.js";
    
    foo.flatMap(x => [x, x + 1]);
    bar.trimLeft();
    arr.includes(2);
  • The usage-pure method 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:

    1. you are a library author, and don't want to "pollute" the global scope with the polyfills you are loading.
    Input code Output code
    foo.flatMap(x => [x, x+1]);
    bar.trimLeft();
    arr.includes(2);
    import _flatMapInstanceProperty from "core-js-pure/stable/instance/flat-map.js";
    import _trimLeftInstanceProperty from "core-js-pure/stable/instance/trim-left.js";
    
    _flatMapInstanceProperty(foo).call(foo, x => [x, x + 1]);
    _trimLeftInstanceProperty(bar).call(bar);
    arr.includes(2);

History and Motivation

In the last three years and a half, @babel/preset-env has shown its full potential in reducing bundle sizes not only by not transpiling supported syntax features, but also by not including unnecessary core-js polyfills.

So far Babel provided three different ways to inject core-js polyfills in the source code:

  • By using @babel/preset-env's useBuiltIns: "entry" option, it is possible to inject self-installing polyfills for every ECMAScript functionality not natively supported by the target browsers;
  • By using @babel/preset-env's useBuiltIns: "usage", Babel will only inject self-installing polyfills for unsupported ECMAScript features but only if they are actually used in the input souce code;
  • By using @babel/plugin-transform-runtime, Babel will inject "pure" polyfills (which, unlike self-installing ones, 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:

  • It wasn't possible to use @babel/preset-env's targets option with "pure" polyfills, because @babel/plugin-transform-runtime is a completely separate package.
  • We forced our users to use core-js if they wanted a Babel integration. core-js is 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.

Want to contribute?

See our CONTRIBUTING.md to get started with setting up the repo.

More Repositories

1

babel

๐Ÿ  Babel is a compiler for writing next generation JavaScript.
TypeScript
43,141
star
2

babel-loader

๐Ÿ“ฆ Babel loader for webpack
JavaScript
4,824
star
3

minify

โœ‚๏ธ An ES6+ aware minifier based on the Babel toolchain (beta)
JavaScript
4,391
star
4

babel-preset-env

PSA: this repo has been moved into babel/babel -->
JavaScript
3,500
star
5

babel-sublime

Syntax definitions for ES6 JavaScript with React JSX extensions.
JavaScript
3,256
star
6

babel-eslint

๐Ÿ—ผ A wrapper for Babel's parser used for ESLint (renamed to @babel/eslint-parser)
JavaScript
2,963
star
7

example-node-server

Example Node Server w/ Babel
JavaScript
2,844
star
8

babylon

PSA: moved into babel/babel as @babel/parser -->
JavaScript
1,714
star
9

babelify

Browserify transform for Babel
JavaScript
1,680
star
10

gulp-babel

Gulp plugin for Babel
JavaScript
1,320
star
11

babel-upgrade

โฌ†๏ธ A tool for upgrading Babel versions (to v7): `npx babel-upgrade`
JavaScript
1,309
star
12

awesome-babel

๐Ÿ˜ŽA list of awesome Babel plugins, presets, etc.
864
star
13

babel-standalone

๐ŸŽฎ Now located in the Babel repo! Standalone build of Babel for use in non-Node.js environments, including browsers.
JavaScript
819
star
14

website

๐ŸŒ The Babel documentation website
TypeScript
749
star
15

preset-modules

A Babel preset that targets modern browsers by fixing engine bugs (will be merged into preset-env eventually)
JavaScript
739
star
16

kneden

Transpile ES2017 async/await to vanilla ES6 Promise chains: a Babel plugin
JavaScript
514
star
17

grunt-babel

Grunt plugin for Babel
JavaScript
437
star
18

proposals

โœ๏ธ Tracking the status of Babel's implementation of TC39 proposals (may be out of date)
432
star
19

eslint-plugin-babel

An ESlint rule plugin companion to babel-eslint
JavaScript
389
star
20

generator-babel-boilerplate

A Yeoman generator to author libraries in ES2015 (and beyond!) for Node and the browser.
JavaScript
381
star
21

babel-time-travel

Time travel through babel transformations one by one (implemented in the Babel REPL now)
JavaScript
344
star
22

babel-sublime-snippets

Next generation JavaScript and React snippets for Sublime
263
star
23

karma-babel-preprocessor

Karma plugin for Babel
JavaScript
167
star
24

ruby-babel-transpiler

Ruby Babel is a bridge to the JS Babel transpiler.
Ruby
159
star
25

babel-jest

Jest plugin for Babel (moved) ->
138
star
26

notes

โ™ฌ Notes from the @Babel Team (discuss in PRs)
122
star
27

generator-babel-plugin

Babel Plugin generator for Yeoman
JavaScript
118
star
28

babel-bridge

A placeholder package that bridges babel-core to @babel/core.
JavaScript
94
star
29

babel-bot

๐Ÿค– A helpful bot to automate common tasks on Babel Issues/PRs
JavaScript
75
star
30

babel-brunch

Brunch plugin for Babel
JavaScript
68
star
31

broccoli-babel-transpiler

Broccoli plugin for Babel
JavaScript
57
star
32

jade-babel

Jade plugin for Babel
JavaScript
40
star
33

sandboxes

Babel repl-like codesandbox: check out link =>
JavaScript
39
star
34

jekyll-babel

A Babel converter for Jekyll.
Ruby
34
star
35

acorn-to-esprima

(unmaintained) Converts acorn tokens to esprima tokens
JavaScript
33
star
36

babel-connect

Connect plugin for Babel
JavaScript
27
star
37

podcast.babeljs.io

The Babel Podcast site!
JavaScript
25
star
38

phabricator-to-github

A tool to migrate phabricator issues to github
JavaScript
24
star
39

actions

Babel specific github actions ๐Ÿค–
JavaScript
24
star
40

rfcs

RFCs for changes to Babel
21
star
41

metalsmith-babel

A Metalsmith plugin to compile JavaScript with Babel
JavaScript
20
star
42

babel-configuration-examples

WIP
JavaScript
18
star
43

logo

Babel logo
15
star
44

duo-babel

Duo plugin for Babel
JavaScript
15
star
45

parser_performance

JavaScript
13
star
46

eslint-config-babel

ESLint config for all Babel repos
JavaScript
10
star
47

gobble-babel

Gobble plugin for Babel
JavaScript
9
star
48

babel-standalone-bower

Bower build of babel-standalone. See https://github.com/Daniel15/babel-standalone
JavaScript
9
star
49

eslint-plugin-babel-plugin

A set of eslint rules to enforce best practices in the development of Babel plugins.
JavaScript
8
star
50

babel-plugin-proposal-private-property-in-object

@babel/plugin-proposal-private-property-in-object, with an added warning for when depending on this package without explicitly listing it in dependencies or devDependencies
JavaScript
8
star
51

babel-archive

๐Ÿ—‘ Packages that are deprecated or don't need to be updated
JavaScript
7
star
52

.github

Community health files for the Babel organization
7
star
53

flavortown

what else?
6
star
54

babel-test262-runner

Run test262 tests on Node 0.10 using Babel 7 and `core-js@3`.
JavaScript
6
star
55

babel-plugin-transform-async-functions

https://github.com/MatAtBread/fast-async/issues/46
4
star
56

phabricator-redirects

โ†ฉ๏ธ Redirects for old phabricator urls
HTML
2
star
57

slack-invite-link

Source of slack.babeljs.io
1
star