babel-polyfill is a comprehensive polyfill orchestration package that provides all ES2015+ features in older JavaScript environments. Rather than implementing polyfills directly, it imports and coordinates multiple polyfill libraries (core-js, regenerator-runtime) and adds a few compatibility enhancements.
npm install babel-polyfillES6 import (recommended):
import "babel-polyfill";CommonJS require:
require("babel-polyfill");Browser script tag:
<script src="node_modules/babel-polyfill/browser.js"></script>// Import at the very beginning of your application entry point
import "babel-polyfill";
// Now all ES2015+ features are available globally
const promise = new Promise((resolve) => resolve("Hello"));
const map = new Map();
const set = new Set();
// Array methods work
[1, 2, 3].includes(2); // true
[1, 2, 3].find(x => x > 1); // 2
// String methods work
"hello".startsWith("he"); // true
"hello".padStart(10, "*"); // "*****hello"
// Object methods work
Object.assign({}, {a: 1}, {b: 2}); // {a: 1, b: 2}
// Async/await and generators work (requires babel-transform)
async function example() {
const result = await Promise.resolve("data");
return result;
}babel-polyfill is a lightweight orchestration layer that coordinates comprehensive polyfills:
global._babelPolyfill flagcore-js/shim for ES2015+ standard library and regenerator-runtime/runtime for async/generator supportThe primary capability - installs all ES2015+ polyfills globally when the module is imported.
// Import triggers global polyfill installation
import "babel-polyfill";
require("babel-polyfill");Behavior:
"only one instance of babel-polyfill is allowed"global._babelPolyfill = true to prevent duplicate importscore-js/shim for ES2015+ standard library featuresregenerator-runtime/runtime for async/generator supportActual functions implemented directly by babel-polyfill:
/**
* Core utility function for defining properties on objects
* Used internally to create backwards-compatible aliases
* @param {Object} O - Target object
* @param {string} key - Property key to define
* @param {any} value - Property value to assign
*/
function define(O, key, value): void;Dynamically creates static versions of Array prototype methods:
// Static methods created from prototype methods using Function.call.bind:
Array.pop; Array.reverse; Array.shift; Array.keys; Array.values; Array.entries;
Array.indexOf; Array.every; Array.some; Array.forEach; Array.map; Array.filter;
Array.find; Array.findIndex; Array.includes; Array.join; Array.slice; Array.concat;
Array.push; Array.splice; Array.unshift; Array.sort; Array.lastIndexOf;
Array.reduce; Array.reduceRight; Array.copyWithin; Array.fill;Implementation mechanism:
// Each static method is created as:
Array[methodName] = Function.call.bind([][methodName]);Backwards-compatible aliases created directly by babel-polyfill:
// Legacy aliases pointing to standard methods
String.prototype.padLeft; // → String.prototype.padStart
String.prototype.padRight; // → String.prototype.padEndImplementation:
define(String.prototype, "padLeft", "".padStart);
define(String.prototype, "padRight", "".padEnd);Features provided through imported dependencies (not directly implemented):
// Promise, Map, Set, WeakMap, WeakSet, Symbol, Proxy, Reflect
// Object.assign, Array.from, String.prototype.startsWith, etc.
// Number.isInteger, Math.sign, and 100+ other ES2015+ features// Runtime support for generators and async functions
regeneratorRuntime.mark(generatorFunction);
regeneratorRuntime.wrap(innerFn, outerFn, self, tryLocsList);
regeneratorRuntime.async(innerFn, outerFn, self, tryLocsList);
regeneratorRuntime.awrap(arg);// RegExp.escape (scheduled for removal in future versions)
RegExp.escape(string);// Throws if imported multiple times
Error: "only one instance of babel-polyfill is allowed"// At the very beginning of your main application file
import "babel-polyfill";
// Rest of your application code
import React from "react";
import App from "./App";
// All ES2015+ features now available// webpack.config.js
module.exports = {
entry: ["babel-polyfill", "./src/index.js"],
// ... rest of config
};// test/setup.js
import "babel-polyfill";
// jest.config.js
module.exports = {
setupFilesAfterEnv: ["<rootDir>/test/setup.js"],
};<!DOCTYPE html>
<html>
<head>
<script src="node_modules/babel-polyfill/browser.js"></script>
</head>
<body>
<script>
// All ES2015+ features available
console.log([1,2,3].includes(2));
</script>
</body>
</html>