@babel/polyfill
Babel includes a polyfill) that includes a custom and core-js.
This will emulate a full ES2015+ environment (no < Stage 4 proposals) and is intended to be used in an application rather than a library/tool.(this polyfill is automatically loaded when using babel-node
).
This means you can use new built-ins like Promise
or WeakMap
, static methods like Array.from
or Object.assign
, instance methods like Array.prototype.includes
, and generator functions (provided you use the plugin). The polyfill adds to the global scope as well as native prototypes like String
in order to do this.
Size
The polyfill is provided as a convenience but you should use it with and the useBuiltIns
option so that it doesn't include the whole polyfill which isn't always needed. Otherwise, we would recommend you import the individual polyfills manually.
Usage in Node / Browserify / Webpack
To include the polyfill you need to require it at the top of the entry point to your application.
If you are using ES6's import
syntax in your application's entry point, youshould instead import the polyfill at the top of the entry point to ensure thepolyfills are loaded first:
With webpack, there are multiple ways to include the polyfills:
When used alongside
@babel/preset-env
,If
useBuiltIns: 'entry'
is specified in.babelrc
then include@babel/polyfill
at the top of the entry point to your application viarequire
orimport
as discussed above.If key is not specified or it is explicitly set with
useBuiltIns: false
in your .babelrc, add@babel/polyfill
directly to the entry array in yourwebpack.config.js
.
- If is not used then add
@babel/polyfill
to webpack entry array as discussed above. It can still be added at the top of the entry point to application viaimport
orrequire
, but this is not recommended.
Available from the dist/polyfill.js
file within a @babel/polyfill
npm release.This needs to be included before all your compiled Babel code. You can eitherprepend it to your compiled code or include it in a <script>
before it.
NOTE: Do not require
this via browserify etc, use @babel/polyfill
.