1.0.118 • Published 7 days ago

babel-plugin-polyfill-custom v1.0.118

Weekly downloads
-
License
MIT
Repository
github
Last release
7 days ago

Babel Plugin Polyfill Custom

github sponsors npm MIT License standard-readme compliant

Yet another babel plugin that lets you freely customize polyfills.

Table of Contents

Background

There are several ways to insert polyfill via babel, including @babel/preset-env and @babel/transform-runtime. Recently, babel-plugin-polyfill-es-shims and others have been created to provide more options other than core-js.

On the other hand, core-js and es-shims are specialized for ECMAScript polyfills, and there is a lack of DOM API polyfills.

babel-plugin-polyfill-custom allows you to set any polyfills you want.

For example, if you want to insert a polyfill for fetch, you can choose whatwg-fetch or unfetch. When considering a polyfill for Promise, you can also choose an alternative other than core-js, such as Zousan, Yaku, or Aigle.

In addition, babel-plugin-polyfill-custom can determine which polyfill is needed based on the browsers listed in the browserslist. This is highly beneficial when using the differencial bundle serving.

Install

npm install --dev @babel/core babel-plugin-polyfill-custom

:warning: This plugin cannot transpile ECMAScript syntax. If you want to use new ECMAScript syntax, please use @babel/preset-env together.

Using with a bundler

babel-plugin-polyfill-custom inserts polyfill using base64 data URIs. When used with a bundler, the bundler should be able to recognize base64 data URIs.

webpack

webpack 5.38.0 or later supports data scheme.

const config = {
  module: {
    rules: [
      {
        mimetype: 'text/javascript',
        scheme: 'data',
        type: 'javascript/auto',
      },
      {
        test: /\.js$/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              ...,
            },
          },
        ],
      },
    ],
  },
};

Rollup

@rollup/plugin-data-uri needs to be loaded.

const { babel } = require('@rollup/plugin-babel');
const dataUri = require('@rollup/plugin-data-uri');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');

const config = {
  plugins: [
    babel({
      ...,
    }),
    dataUri(),
    nodeResolve(),
    commonjs(),
  ],
};

Parcel

Parcel does not have a feature to recognize base64 data URIs. You can create your own Parcel Resolver to recognize base64 data URIs.

See ./examples/parcel for more details.

{
  "extends": "@parcel/config-default",
  "resolvers": [
    // You should create resolver as parcel-resolver-data-uri.
    "parcel-resolver-data-uri",
    "@parcel/resolver-default"
  ]
}

Options

You can find more examples in ./examples.

const polyfills = {
  'polyfill:abort-controller': {
    features: ['api.AbortController'],
    packages: [
      {
        packageName: 'abort-controller/polyfill',
      },
    ],
  },
  'polyfill:fetch': {
    features: ['api.fetch', 'api.fetch.init_signal_parameter'],
    packages: [
      {
        definitions: {
          ['fetch']: 'fetch',
        },
        packageName: 'whatwg-fetch',
      },
    ],
  },
};

const config = {
  plugins: [
    [
      'polyfill-custom',
      {
        method: 'entry-global',
        polyfills,
      },
    ],
  ],
};

method

entry-global is only supported.

See https://github.com/babel/babel-polyfills#injection-methods.

polyfills

An Object for the definition of polyfill. A key of object is the identifier of the polyfill and is used as the package name when importing.

// Source
import 'polyfill:fetch';
import 'polyfill:abort-controller';

const abortController = new AbortController();

fetch('https://example.com', {
  signal: abortController.signal,
}).then(function (res) {
  if (!res.ok) {
    throw new Error('Failed to fetch: ' + String(res.status));
  }
  console.log(res);
});
// Transpiled
import 'data:text/javascript;base64,aW1wb3J0e2ZldGNoIGFzIF9fZmV0Y2h9ZnJvbSJ3aGF0d2ctZmV0Y2giO3ZhciBleHBvcnRzPWZ1bmN0aW9uKCl7cmV0dXJuIHR5cGVvZiBnbG9iYWxUaGlzIT09InVuZGVmaW5lZCI/Z2xvYmFsVGhpczp0eXBlb2Ygc2VsZiE9PSJ1bmRlZmluZWQiP3NlbGY6dHlwZW9mIHdpbmRvdyE9PSJ1bmRlZmluZWQiP3dpbmRvdzp0eXBlb2YgZ2xvYmFsIT09InVuZGVmaW5lZCI/Z2xvYmFsOkZ1bmN0aW9uKCJyZXR1cm4gdGhpcyIpKCl9KCk7ZXhwb3J0cy5mZXRjaD1fX2ZldGNoOw==';
import 'abort-controller/polyfill';

const abortController = new AbortController();

fetch('https://example.com', {
  signal: abortController.signal,
}).then(function (res) {
  if (!res.ok) {
    throw new Error('Failed to fetch: ' + String(res.status));
  }
  console.log(res);
});

polyfills[ID].features

A list of features that polyfill has. Fill in the @mdn/browser-compat-data identifier.

For example, in the case of polyfill supporting the fetch API, the identifier is api.fetch.

polyfills[ID].packages

The definition of the package to be inserted as polyfill.

packageName is the name of the package. definitions specifies which export variables are to be inserted as what global object.

// To assign `import { Aigle } from 'aigle'` to `window.Promise`,
// specify `Promise` on the left side and `Aigle` on the right side.
{
  definitions: {
    ['Promise']: 'Aigle',
  },
  packageName: 'aigle',
}
// To assign `import matches from '@ungap/element-matches'` to `Element.prototype.matches`,
// specify `Element.prototype.matches` on the left side and `default` on the right side.
{
  definitions: {
    ['Element.prototype.matches']: 'default',
  },
  packageName: '@ungap/element-matches',
}

Contributing

PRs accepted.

License

MIT (c) 3846masa

1.0.118

7 days ago

1.0.117

7 days ago

1.0.116

13 days ago

1.0.115

17 days ago

1.0.114

21 days ago

1.0.113

27 days ago

1.0.112

1 month ago

1.0.111

1 month ago

1.0.110

2 months ago

1.0.109

2 months ago

1.0.108

2 months ago

1.0.107

2 months ago

1.0.106

2 months ago

1.0.105

2 months ago

1.0.104

2 months ago

1.0.103

2 months ago

1.0.102

3 months ago

1.0.101

3 months ago

1.0.100

3 months ago

1.0.99

3 months ago

1.0.98

3 months ago

1.0.97

3 months ago

1.0.96

3 months ago

1.0.95

4 months ago

1.0.94

4 months ago

1.0.73

10 months ago

1.0.72

10 months ago

1.0.71

10 months ago

1.0.70

10 months ago

1.0.77

9 months ago

1.0.76

9 months ago

1.0.75

9 months ago

1.0.74

10 months ago

1.0.79

9 months ago

1.0.78

9 months ago

1.0.80

9 months ago

1.0.84

8 months ago

1.0.83

8 months ago

1.0.82

8 months ago

1.0.81

9 months ago

1.0.88

7 months ago

1.0.87

7 months ago

1.0.86

8 months ago

1.0.85

8 months ago

1.0.89

7 months ago

1.0.91

6 months ago

1.0.90

6 months ago

1.0.93

6 months ago

1.0.92

6 months ago

1.0.69

10 months ago

1.0.68

10 months ago

1.0.66

11 months ago

1.0.65

11 months ago

1.0.64

11 months ago

1.0.63

11 months ago

1.0.67

11 months ago

1.0.62

11 months ago

1.0.61

11 months ago

1.0.60

11 months ago

1.0.59

12 months ago

1.0.58

12 months ago

1.0.57

12 months ago

1.0.51

1 year ago

1.0.50

1 year ago

1.0.55

12 months ago

1.0.54

1 year ago

1.0.53

1 year ago

1.0.52

1 year ago

1.0.56

12 months ago

1.0.48

1 year ago

1.0.47

1 year ago

1.0.46

1 year ago

1.0.49

1 year ago

1.0.39

1 year ago

1.0.38

1 year ago

1.0.40

1 year ago

1.0.44

1 year ago

1.0.43

1 year ago

1.0.42

1 year ago

1.0.41

1 year ago

1.0.45

1 year ago

1.0.37

1 year ago

1.0.36

1 year ago

1.0.35

1 year ago

1.0.34

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.29

1 year ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.33

1 year ago

1.0.32

1 year ago

1.0.31

1 year ago

1.0.30

1 year ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.20

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago