Next: Configuration, Previous: Plugins and Presets, Up: Babel [Index]
Directly including core-js/stable (to polyfill ECMAScript features) and regenerator-runtime/runtime (needed to use transpiled generator functions):
import "core-js/stable"; import "regenerator-runtime/runtime";
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 (when used alongside the
regenerator plugin). The polyfill adds to the global scope as well as native
prototypes like ‘String’ in order to do this.
We’re using the ‘env’ preset which has a "useBuiltIns" option that when set to "usage" will practically apply the last optimization mentioned above where you only include the polyfills you need. With this new option the configuration changes like this:
{ "presets": [ [ "@babel/env", { "targets": { "edge": "17", "firefox": "60", "chrome": "67", "safari": "11.1", }, "useBuiltIns": "usage", } ] ] }
Babel will now inspect all your code for features that are missing in your target environments and include only the required polyfills.