Update to core-js@3 (#7646)
### `@babel/runtime` - Added `@babel/runtime-corejs3` package and `corejs: 3` options to `@babel/plugin-transform-runtime`. - Added support of instance methods, fixes #8928. - Added flag `proposals` (in `corejs: { version: 3, proposals: true }` format) for support all proposals polyfills from `core-js`. - Used separate directories in runtime for `core-js` entry points with proposals and without. - Used `get-iterator-method` helper for getting iterators, fixes #2500. - As a cheap bonus, added support of IE8- (except some cases of `regenerator`). ### `@babel/polyfill` - Should be deprecated in favor of separate usage required features from `core-js` and `regenerator-runtime` with an informative message. ### `@babel/preset-env` - Uses for built-ins data from [`core-js-compat`](https://github.com/zloirock/core-js/tree/master/packages/core-js-compat) instead of `compat-table` since information from `compat-table` [is not enough](https://github.com/zloirock/core-js/tree/master/packages/core-js-compat). - `useBuilIns` now requires direct setting of `corejs` version option, without it will be used `2` by default and shown deprecation warning. - Added support of minor `core-js` versions for simplify updating in the future. - For preventing some order-related problems, polyfills in the both `core-js@3` plugins added on `post` stage in the order of `core-js-compat` data. - Divided plugins and polyfills parts of `preset-env`, instead of 2 internal plugins for adding polyfills, we have 6: usage and entry versions of plugins for `core-js@2`, ### Current state: `core-js@3`, `regenerator-runtime`. - Added support `samsung` target (for Samsung Internet) since `core-js-compat` and `compat-table` now contains mapping for this, fixes #6602. #### `useBuilIns: entry` with `corejs: 3` - No longer transforms `@babel/polyfill`. - Transforms **all possible** `core-js` entry points to import of related modules (based on data from [`core-js-compat`](https://unpkg.com/core-js-compat@3.0.0-beta.15/entries.json)). - Since of this, we no longer need `shippedProposals` / `proposals` flags with `useBuilIns: entry`. - Removes `regenerator-runtime/runtime` import where it's not required. #### `useBuilIns: usage` with `corejs: 3` - In addition to `shippedProposals`, added flag `proposals` (in `corejs: { version: 3, proposals: true }` format) for polyfill all proposals from `core-js`. - Fixed list of dependencies in built-in definitions. - Improved the way of determination method / built-in name and source of this method. - Adds import of required polyfills on `MemberExpression`, `ObjectPattern`, `in` operator. - Adds import of required polyfills on access to global object properties. - Adds import of all required common iterators on all syntax features which use iterators protocol (`for-of`, destructuring, spread, `yield` delegation, etc.). - Adds import of promises on syntax features which use promises (async functions/generators, dynamic import, etc.), fixes #9250, #7402, etc. ### `core-js@2` stuff I didn't want to tough `core-js@2`-related stuff, however - Fixed some serious errors in definitions which breaks `Object.getOwnPropertySymbols`, `Symbol.toStringTag` logic, `Promise#finally`, `Array#forEach`, etc. - `Array#flatMap` and trim methods moved to stable features as a part of ES2019 and loaded by deprecated `@babel/polyfill` and `@babel/preset-env` with `corejs: 2` option.
This commit is contained in:
committed by
Nicolò Ribaudo
parent
7df0d16131
commit
3303b079c5
@@ -1,168 +0,0 @@
|
||||
import semver from "semver";
|
||||
|
||||
function hasMinVersion(minVersion, runtimeVersion) {
|
||||
// If the range is unavailable, we're running the script during Babel's
|
||||
// build process, and we want to assume that all versions are satisfied so
|
||||
// that the built output will include all definitions.
|
||||
if (!runtimeVersion) return true;
|
||||
|
||||
// semver.intersects() has some surprising behavior with comparing ranges
|
||||
// with preprelease versions. We add '^' to ensure that we are always
|
||||
// comparing ranges with ranges, which sidesteps this logic.
|
||||
// For example:
|
||||
//
|
||||
// semver.intersects(`<7.0.1`, "7.0.0-beta.0") // false - surprising
|
||||
// semver.intersects(`<7.0.1`, "^7.0.0-beta.0") // true - expected
|
||||
//
|
||||
// This is because the first falls back to
|
||||
//
|
||||
// semver.satisfies("7.0.0-beta.0", `<7.0.1`) // false - surprising
|
||||
//
|
||||
// and this fails because a prerelease version can only satisfy a range
|
||||
// if it is a prerelease within the same major/minor/patch range.
|
||||
//
|
||||
// Note: If this is found to have issues, please also revist the logic in
|
||||
// babel-core's availableHelper() API.
|
||||
if (semver.valid(runtimeVersion)) runtimeVersion = `^${runtimeVersion}`;
|
||||
|
||||
return (
|
||||
!semver.intersects(`<${minVersion}`, runtimeVersion) &&
|
||||
!semver.intersects(`>=8.0.0`, runtimeVersion)
|
||||
);
|
||||
}
|
||||
|
||||
export default runtimeVersion => {
|
||||
// Conditionally include 'Math' because it was not included in the 7.0.0
|
||||
// release of '@babel/runtime'. See issue https://github.com/babel/babel/pull/8616.
|
||||
const includeMathModule = hasMinVersion("7.0.1", runtimeVersion);
|
||||
|
||||
return {
|
||||
builtins: {
|
||||
Symbol: "symbol",
|
||||
Promise: "promise",
|
||||
Map: "map",
|
||||
WeakMap: "weak-map",
|
||||
Set: "set",
|
||||
WeakSet: "weak-set",
|
||||
setImmediate: "set-immediate",
|
||||
clearImmediate: "clear-immediate",
|
||||
parseFloat: "parse-float",
|
||||
parseInt: "parse-int",
|
||||
},
|
||||
|
||||
methods: {
|
||||
Array: {
|
||||
from: "array/from",
|
||||
isArray: "array/is-array",
|
||||
of: "array/of",
|
||||
},
|
||||
|
||||
JSON: {
|
||||
stringify: "json/stringify",
|
||||
},
|
||||
|
||||
Object: {
|
||||
assign: "object/assign",
|
||||
create: "object/create",
|
||||
defineProperties: "object/define-properties",
|
||||
defineProperty: "object/define-property",
|
||||
entries: "object/entries",
|
||||
freeze: "object/freeze",
|
||||
getOwnPropertyDescriptor: "object/get-own-property-descriptor",
|
||||
getOwnPropertyDescriptors: "object/get-own-property-descriptors",
|
||||
getOwnPropertyNames: "object/get-own-property-names",
|
||||
getOwnPropertySymbols: "object/get-own-property-symbols",
|
||||
getPrototypeOf: "object/get-prototype-of",
|
||||
isExtensible: "object/is-extensible",
|
||||
isFrozen: "object/is-frozen",
|
||||
isSealed: "object/is-sealed",
|
||||
is: "object/is",
|
||||
keys: "object/keys",
|
||||
preventExtensions: "object/prevent-extensions",
|
||||
seal: "object/seal",
|
||||
setPrototypeOf: "object/set-prototype-of",
|
||||
values: "object/values",
|
||||
},
|
||||
|
||||
...(includeMathModule
|
||||
? {
|
||||
Math: {
|
||||
acosh: "math/acosh",
|
||||
asinh: "math/asinh",
|
||||
atanh: "math/atanh",
|
||||
cbrt: "math/cbrt",
|
||||
clz32: "math/clz32",
|
||||
cosh: "math/cosh",
|
||||
expm1: "math/expm1",
|
||||
fround: "math/fround",
|
||||
hypot: "math/hypot",
|
||||
imul: "math/imul",
|
||||
log10: "math/log10",
|
||||
log1p: "math/log1p",
|
||||
log2: "math/log2",
|
||||
sign: "math/sign",
|
||||
sinh: "math/sinh",
|
||||
tanh: "math/tanh",
|
||||
trunc: "math/trunc",
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
|
||||
Symbol: {
|
||||
// FIXME: Disabled to work around zloirock/core-js#262.
|
||||
// asyncIterator: "symbol/async-iterator",
|
||||
for: "symbol/for",
|
||||
hasInstance: "symbol/has-instance",
|
||||
isConcatSpreadable: "symbol/is-concat-spreadable",
|
||||
iterator: "symbol/iterator",
|
||||
keyFor: "symbol/key-for",
|
||||
match: "symbol/match",
|
||||
replace: "symbol/replace",
|
||||
search: "symbol/search",
|
||||
species: "symbol/species",
|
||||
split: "symbol/split",
|
||||
toPrimitive: "symbol/to-primitive",
|
||||
toStringTag: "symbol/to-string-tag",
|
||||
unscopables: "symbol/unscopables",
|
||||
},
|
||||
|
||||
String: {
|
||||
at: "string/at",
|
||||
fromCodePoint: "string/from-code-point",
|
||||
raw: "string/raw",
|
||||
},
|
||||
|
||||
Number: {
|
||||
EPSILON: "number/epsilon",
|
||||
isFinite: "number/is-finite",
|
||||
isInteger: "number/is-integer",
|
||||
isNaN: "number/is-nan",
|
||||
isSafeInteger: "number/is-safe-integer",
|
||||
MAX_SAFE_INTEGER: "number/max-safe-integer",
|
||||
MIN_SAFE_INTEGER: "number/min-safe-integer",
|
||||
parseFloat: "number/parse-float",
|
||||
parseInt: "number/parse-int",
|
||||
},
|
||||
|
||||
Reflect: {
|
||||
apply: "reflect/apply",
|
||||
construct: "reflect/construct",
|
||||
defineProperty: "reflect/define-property",
|
||||
deleteProperty: "reflect/delete-property",
|
||||
getOwnPropertyDescriptor: "reflect/get-own-property-descriptor",
|
||||
getPrototypeOf: "reflect/get-prototype-of",
|
||||
get: "reflect/get",
|
||||
has: "reflect/has",
|
||||
isExtensible: "reflect/is-extensible",
|
||||
ownKeys: "reflect/own-keys",
|
||||
preventExtensions: "reflect/prevent-extensions",
|
||||
setPrototypeOf: "reflect/set-prototype-of",
|
||||
set: "reflect/set",
|
||||
},
|
||||
|
||||
Date: {
|
||||
now: "date/now",
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
32
packages/babel-plugin-transform-runtime/src/helpers.js
Normal file
32
packages/babel-plugin-transform-runtime/src/helpers.js
Normal file
@@ -0,0 +1,32 @@
|
||||
import semver from "semver";
|
||||
|
||||
export function hasMinVersion(minVersion, runtimeVersion) {
|
||||
// If the range is unavailable, we're running the script during Babel's
|
||||
// build process, and we want to assume that all versions are satisfied so
|
||||
// that the built output will include all definitions.
|
||||
if (!runtimeVersion) return true;
|
||||
|
||||
// semver.intersects() has some surprising behavior with comparing ranges
|
||||
// with preprelease versions. We add '^' to ensure that we are always
|
||||
// comparing ranges with ranges, which sidesteps this logic.
|
||||
// For example:
|
||||
//
|
||||
// semver.intersects(`<7.0.1`, "7.0.0-beta.0") // false - surprising
|
||||
// semver.intersects(`<7.0.1`, "^7.0.0-beta.0") // true - expected
|
||||
//
|
||||
// This is because the first falls back to
|
||||
//
|
||||
// semver.satisfies("7.0.0-beta.0", `<7.0.1`) // false - surprising
|
||||
//
|
||||
// and this fails because a prerelease version can only satisfy a range
|
||||
// if it is a prerelease within the same major/minor/patch range.
|
||||
//
|
||||
// Note: If this is found to have issues, please also revist the logic in
|
||||
// babel-core's availableHelper() API.
|
||||
if (semver.valid(runtimeVersion)) runtimeVersion = `^${runtimeVersion}`;
|
||||
|
||||
return (
|
||||
!semver.intersects(`<${minVersion}`, runtimeVersion) &&
|
||||
!semver.intersects(`>=8.0.0`, runtimeVersion)
|
||||
);
|
||||
}
|
||||
@@ -4,7 +4,8 @@ import { declare } from "@babel/helper-plugin-utils";
|
||||
import { addDefault, isModule } from "@babel/helper-module-imports";
|
||||
import { types as t } from "@babel/core";
|
||||
|
||||
import getDefinitions from "./definitions";
|
||||
import getCoreJS2Definitions from "./runtime-corejs2-definitions";
|
||||
import getCoreJS3Definitions from "./runtime-corejs3-definitions";
|
||||
|
||||
function resolveAbsoluteRuntime(moduleName: string, dirname: string) {
|
||||
try {
|
||||
@@ -33,7 +34,7 @@ export default declare((api, options, dirname) => {
|
||||
api.assertVersion(7);
|
||||
|
||||
const {
|
||||
corejs: corejsVersion = false,
|
||||
corejs,
|
||||
helpers: useRuntimeHelpers = true,
|
||||
regenerator: useRuntimeRegenerator = true,
|
||||
useESModules = false,
|
||||
@@ -41,21 +42,48 @@ export default declare((api, options, dirname) => {
|
||||
absoluteRuntime = false,
|
||||
} = options;
|
||||
|
||||
const definitions = getDefinitions(runtimeVersion);
|
||||
let proposals = false;
|
||||
let rawVersion;
|
||||
|
||||
if (typeof corejs === "object" && corejs !== null) {
|
||||
rawVersion = corejs.version;
|
||||
proposals = Boolean(corejs.proposals);
|
||||
} else {
|
||||
rawVersion = corejs;
|
||||
}
|
||||
|
||||
const corejsVersion = rawVersion ? Number(rawVersion) : false;
|
||||
|
||||
if (![false, 2, 3].includes(corejsVersion)) {
|
||||
throw new Error(
|
||||
`The \`core-js\` version must be false, 2 or 3, but got ${JSON.stringify(
|
||||
rawVersion,
|
||||
)}.`,
|
||||
);
|
||||
}
|
||||
|
||||
if (proposals && (!corejsVersion || corejsVersion < 3)) {
|
||||
throw new Error(
|
||||
"The 'proposals' option is only supported when using 'corejs: 3'",
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof useRuntimeRegenerator !== "boolean") {
|
||||
throw new Error(
|
||||
"The 'regenerator' option must be undefined, or a boolean.",
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof useRuntimeHelpers !== "boolean") {
|
||||
throw new Error("The 'helpers' option must be undefined, or a boolean.");
|
||||
}
|
||||
|
||||
if (typeof useESModules !== "boolean" && useESModules !== "auto") {
|
||||
throw new Error(
|
||||
"The 'useESModules' option must be undefined, or a boolean, or 'auto'.",
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
typeof absoluteRuntime !== "boolean" &&
|
||||
typeof absoluteRuntime !== "string"
|
||||
@@ -64,16 +92,7 @@ export default declare((api, options, dirname) => {
|
||||
"The 'absoluteRuntime' option must be undefined, a boolean, or a string.",
|
||||
);
|
||||
}
|
||||
if (
|
||||
corejsVersion !== false &&
|
||||
(typeof corejsVersion !== "number" || corejsVersion !== 2) &&
|
||||
(typeof corejsVersion !== "string" || corejsVersion !== "2")
|
||||
) {
|
||||
throw new Error(
|
||||
`The 'corejs' option must be undefined, false, 2 or '2', ` +
|
||||
`but got ${JSON.stringify(corejsVersion)}.`,
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof runtimeVersion !== "string") {
|
||||
throw new Error(`The 'version' option must be a version string.`);
|
||||
}
|
||||
@@ -81,6 +100,18 @@ export default declare((api, options, dirname) => {
|
||||
function has(obj, key) {
|
||||
return Object.prototype.hasOwnProperty.call(obj, key);
|
||||
}
|
||||
|
||||
function hasMapping(methods, name) {
|
||||
return has(methods, name) && (proposals || methods[name].stable);
|
||||
}
|
||||
|
||||
function hasStaticMapping(object, method) {
|
||||
return (
|
||||
has(StaticProperties, object) &&
|
||||
hasMapping(StaticProperties[object], method)
|
||||
);
|
||||
}
|
||||
|
||||
if (has(options, "useBuiltIns")) {
|
||||
if (options.useBuiltIns) {
|
||||
throw new Error(
|
||||
@@ -90,10 +121,11 @@ export default declare((api, options, dirname) => {
|
||||
} else {
|
||||
throw new Error(
|
||||
"The 'useBuiltIns' option has been removed. Use the 'corejs'" +
|
||||
"option with value '2' to polyfill with CoreJS 2.x via @babel/runtime.",
|
||||
"option to polyfill with `core-js` via @babel/runtime.",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (has(options, "polyfill")) {
|
||||
if (options.polyfill === false) {
|
||||
throw new Error(
|
||||
@@ -103,10 +135,11 @@ export default declare((api, options, dirname) => {
|
||||
} else {
|
||||
throw new Error(
|
||||
"The 'polyfill' option has been removed. Use the 'corejs'" +
|
||||
"option with value '2' to polyfill with CoreJS 2.x via @babel/runtime.",
|
||||
"option to polyfill with `core-js` via @babel/runtime.",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (has(options, "moduleName")) {
|
||||
throw new Error(
|
||||
"The 'moduleName' option has been removed. @babel/transform-runtime " +
|
||||
@@ -119,11 +152,22 @@ export default declare((api, options, dirname) => {
|
||||
const esModules =
|
||||
useESModules === "auto" ? api.caller(supportsStaticESM) : useESModules;
|
||||
|
||||
const injectCoreJS2 = `${corejsVersion}` === "2";
|
||||
const moduleName = injectCoreJS2
|
||||
const injectCoreJS2 = corejsVersion === 2;
|
||||
const injectCoreJS3 = corejsVersion === 3;
|
||||
const injectCoreJS = corejsVersion !== false;
|
||||
|
||||
const moduleName = injectCoreJS3
|
||||
? "@babel/runtime-corejs3"
|
||||
: injectCoreJS2
|
||||
? "@babel/runtime-corejs2"
|
||||
: "@babel/runtime";
|
||||
|
||||
const corejsRoot = injectCoreJS3 && !proposals ? "core-js-stable" : "core-js";
|
||||
|
||||
const { BuiltIns, StaticProperties, InstanceProperties } = (injectCoreJS2
|
||||
? getCoreJS2Definitions
|
||||
: getCoreJS3Definitions)(runtimeVersion);
|
||||
|
||||
const HEADER_HELPERS = ["interopRequireWildcard", "interopRequireDefault"];
|
||||
|
||||
let modulePath = moduleName;
|
||||
@@ -199,7 +243,10 @@ export default declare((api, options, dirname) => {
|
||||
visitor: {
|
||||
ReferencedIdentifier(path) {
|
||||
const { node, parent, scope } = path;
|
||||
if (node.name === "regeneratorRuntime" && useRuntimeRegenerator) {
|
||||
const { name } = node;
|
||||
|
||||
// transform `regeneratorRuntime`
|
||||
if (name === "regeneratorRuntime" && useRuntimeRegenerator) {
|
||||
path.replaceWith(
|
||||
this.addDefaultImport(
|
||||
`${modulePath}/regenerator`,
|
||||
@@ -209,50 +256,81 @@ export default declare((api, options, dirname) => {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!injectCoreJS2) return;
|
||||
if (!injectCoreJS) return;
|
||||
|
||||
if (t.isMemberExpression(parent)) return;
|
||||
if (!has(definitions.builtins, node.name)) return;
|
||||
if (scope.getBindingIdentifier(node.name)) return;
|
||||
if (!hasMapping(BuiltIns, name)) return;
|
||||
if (scope.getBindingIdentifier(name)) return;
|
||||
|
||||
// Symbol() -> _core.Symbol(); new Promise -> new _core.Promise
|
||||
// transform global built-ins like `Symbol()`, `new Promise`
|
||||
path.replaceWith(
|
||||
this.addDefaultImport(
|
||||
`${modulePath}/core-js/${definitions.builtins[node.name]}`,
|
||||
node.name,
|
||||
`${modulePath}/${corejsRoot}/${BuiltIns[name].path}`,
|
||||
name,
|
||||
),
|
||||
);
|
||||
},
|
||||
|
||||
// arr[Symbol.iterator]() -> _core.$for.getIterator(arr)
|
||||
CallExpression(path) {
|
||||
if (!injectCoreJS2) return;
|
||||
if (!injectCoreJS) return;
|
||||
|
||||
// we can't compile this
|
||||
if (path.node.arguments.length) return;
|
||||
const { node } = path;
|
||||
const { callee } = node;
|
||||
|
||||
const callee = path.node.callee;
|
||||
if (!t.isMemberExpression(callee)) return;
|
||||
|
||||
const { object, property } = callee;
|
||||
const propertyName = property.name;
|
||||
|
||||
// transform calling instance methods like `something.includes()`
|
||||
if (injectCoreJS3 && !hasStaticMapping(object.name, propertyName)) {
|
||||
if (hasMapping(InstanceProperties, propertyName)) {
|
||||
let context1, context2;
|
||||
if (t.isIdentifier(object)) {
|
||||
context1 = object;
|
||||
context2 = t.cloneNode(object);
|
||||
} else {
|
||||
context1 = path.scope.generateDeclaredUidIdentifier("context");
|
||||
context2 = t.assignmentExpression("=", context1, object);
|
||||
}
|
||||
node.callee = t.memberExpression(
|
||||
t.callExpression(
|
||||
this.addDefaultImport(
|
||||
`${moduleName}/${corejsRoot}/instance/${
|
||||
InstanceProperties[propertyName].path
|
||||
}`,
|
||||
`${propertyName}InstanceProperty`,
|
||||
),
|
||||
[context2],
|
||||
),
|
||||
t.identifier("call"),
|
||||
);
|
||||
node.arguments.unshift(context1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// we can't compile this
|
||||
if (node.arguments.length) return;
|
||||
if (!callee.computed) return;
|
||||
if (!path.get("callee.property").matchesPattern("Symbol.iterator")) {
|
||||
return;
|
||||
}
|
||||
|
||||
// transform `something[Symbol.iterator]()` to calling `getIterator(something)` helper
|
||||
path.replaceWith(
|
||||
t.callExpression(
|
||||
this.addDefaultImport(
|
||||
`${modulePath}/core-js/get-iterator`,
|
||||
"getIterator",
|
||||
),
|
||||
[callee.object],
|
||||
[object],
|
||||
),
|
||||
);
|
||||
},
|
||||
|
||||
// Symbol.iterator in arr -> core.$for.isIterable(arr)
|
||||
// transform `Symbol.iterator in something` to calling `isIterable(something)` helper
|
||||
BinaryExpression(path) {
|
||||
if (!injectCoreJS2) return;
|
||||
|
||||
if (!injectCoreJS) return;
|
||||
if (path.node.operator !== "in") return;
|
||||
if (!path.get("left").matchesPattern("Symbol.iterator")) return;
|
||||
|
||||
@@ -267,61 +345,84 @@ export default declare((api, options, dirname) => {
|
||||
);
|
||||
},
|
||||
|
||||
// Array.from -> _core.Array.from
|
||||
// transform static built-ins methods like `Array.from`
|
||||
MemberExpression: {
|
||||
enter(path) {
|
||||
if (!injectCoreJS2) return;
|
||||
if (!injectCoreJS) return;
|
||||
if (!path.isReferenced()) return;
|
||||
|
||||
const { node } = path;
|
||||
const obj = node.object;
|
||||
const prop = node.property;
|
||||
const { object, property } = node;
|
||||
|
||||
if (!t.isReferenced(obj, node)) return;
|
||||
if (node.computed) return;
|
||||
if (!has(definitions.methods, obj.name)) return;
|
||||
if (!t.isReferenced(object, node)) return;
|
||||
|
||||
const methods = definitions.methods[obj.name];
|
||||
if (!has(methods, prop.name)) return;
|
||||
|
||||
// doesn't reference the global
|
||||
if (path.scope.getBindingIdentifier(obj.name)) return;
|
||||
|
||||
// special case Object.defineProperty to not use core-js when using string keys
|
||||
if (
|
||||
obj.name === "Object" &&
|
||||
prop.name === "defineProperty" &&
|
||||
path.parentPath.isCallExpression()
|
||||
) {
|
||||
const call = path.parentPath.node;
|
||||
if (call.arguments.length === 3 && t.isLiteral(call.arguments[1])) {
|
||||
return;
|
||||
if (node.computed) {
|
||||
if (injectCoreJS2) return;
|
||||
// transform `something[Symbol.iterator]` to calling `getIteratorMethod(something)` helper
|
||||
if (path.get("property").matchesPattern("Symbol.iterator")) {
|
||||
path.replaceWith(
|
||||
t.callExpression(
|
||||
this.addDefaultImport(
|
||||
`${moduleName}/core-js/get-iterator-method`,
|
||||
"getIteratorMethod",
|
||||
),
|
||||
[object],
|
||||
),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const objectName = object.name;
|
||||
const propertyName = property.name;
|
||||
// doesn't reference the global
|
||||
if (
|
||||
path.scope.getBindingIdentifier(objectName) ||
|
||||
!hasStaticMapping(objectName, propertyName)
|
||||
) {
|
||||
// transform getting of instance methods like `method = something.includes`
|
||||
if (injectCoreJS3 && hasMapping(InstanceProperties, propertyName)) {
|
||||
path.replaceWith(
|
||||
t.callExpression(
|
||||
this.addDefaultImport(
|
||||
`${moduleName}/${corejsRoot}/instance/${
|
||||
InstanceProperties[propertyName].path
|
||||
}`,
|
||||
`${propertyName}InstanceProperty`,
|
||||
),
|
||||
[object],
|
||||
),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
path.replaceWith(
|
||||
this.addDefaultImport(
|
||||
`${modulePath}/core-js/${methods[prop.name]}`,
|
||||
`${obj.name}$${prop.name}`,
|
||||
`${modulePath}/${corejsRoot}/${
|
||||
StaticProperties[objectName][propertyName].path
|
||||
}`,
|
||||
`${objectName}$${propertyName}`,
|
||||
),
|
||||
);
|
||||
},
|
||||
|
||||
exit(path) {
|
||||
if (!injectCoreJS2) return;
|
||||
if (!injectCoreJS) return;
|
||||
if (!path.isReferenced()) return;
|
||||
|
||||
const { node } = path;
|
||||
const obj = node.object;
|
||||
const { object } = node;
|
||||
const { name } = object;
|
||||
|
||||
if (!has(definitions.builtins, obj.name)) return;
|
||||
if (path.scope.getBindingIdentifier(obj.name)) return;
|
||||
if (!hasMapping(BuiltIns, name)) return;
|
||||
if (path.scope.getBindingIdentifier(name)) return;
|
||||
|
||||
path.replaceWith(
|
||||
t.memberExpression(
|
||||
this.addDefaultImport(
|
||||
`${modulePath}/core-js/${definitions.builtins[obj.name]}`,
|
||||
obj.name,
|
||||
`${modulePath}/${corejsRoot}/${BuiltIns[name].path}`,
|
||||
name,
|
||||
),
|
||||
node.property,
|
||||
node.computed,
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
import { hasMinVersion } from "./helpers";
|
||||
|
||||
export default runtimeVersion => {
|
||||
// Conditionally include 'Math' because it was not included in the 7.0.0
|
||||
// release of '@babel/runtime'. See issue https://github.com/babel/babel/pull/8616.
|
||||
const includeMathModule = hasMinVersion("7.0.1", runtimeVersion);
|
||||
|
||||
return {
|
||||
BuiltIns: {
|
||||
Symbol: { stable: true, path: "symbol" },
|
||||
Promise: { stable: true, path: "promise" },
|
||||
Map: { stable: true, path: "map" },
|
||||
WeakMap: { stable: true, path: "weak-map" },
|
||||
Set: { stable: true, path: "set" },
|
||||
WeakSet: { stable: true, path: "weak-set" },
|
||||
setImmediate: { stable: true, path: "set-immediate" },
|
||||
clearImmediate: { stable: true, path: "clear-immediate" },
|
||||
parseFloat: { stable: true, path: "parse-float" },
|
||||
parseInt: { stable: true, path: "parse-int" },
|
||||
},
|
||||
|
||||
StaticProperties: {
|
||||
Array: {
|
||||
from: { stable: true, path: "array/from" },
|
||||
isArray: { stable: true, path: "array/is-array" },
|
||||
of: { stable: true, path: "array/of" },
|
||||
},
|
||||
|
||||
JSON: {
|
||||
stringify: { stable: true, path: "json/stringify" },
|
||||
},
|
||||
|
||||
Object: {
|
||||
assign: { stable: true, path: "object/assign" },
|
||||
create: { stable: true, path: "object/create" },
|
||||
defineProperties: { stable: true, path: "object/define-properties" },
|
||||
defineProperty: { stable: true, path: "object/define-property" },
|
||||
entries: { stable: true, path: "object/entries" },
|
||||
freeze: { stable: true, path: "object/freeze" },
|
||||
getOwnPropertyDescriptor: {
|
||||
stable: true,
|
||||
path: "object/get-own-property-descriptor",
|
||||
},
|
||||
getOwnPropertyDescriptors: {
|
||||
stable: true,
|
||||
path: "object/get-own-property-descriptors",
|
||||
},
|
||||
getOwnPropertyNames: {
|
||||
stable: true,
|
||||
path: "object/get-own-property-names",
|
||||
},
|
||||
getOwnPropertySymbols: {
|
||||
stable: true,
|
||||
path: "object/get-own-property-symbols",
|
||||
},
|
||||
getPrototypeOf: { stable: true, path: "object/get-prototype-of" },
|
||||
isExtensible: { stable: true, path: "object/is-extensible" },
|
||||
isFrozen: { stable: true, path: "object/is-frozen" },
|
||||
isSealed: { stable: true, path: "object/is-sealed" },
|
||||
is: { stable: true, path: "object/is" },
|
||||
keys: { stable: true, path: "object/keys" },
|
||||
preventExtensions: { stable: true, path: "object/prevent-extensions" },
|
||||
seal: { stable: true, path: "object/seal" },
|
||||
setPrototypeOf: { stable: true, path: "object/set-prototype-of" },
|
||||
values: { stable: true, path: "object/values" },
|
||||
},
|
||||
|
||||
...(includeMathModule
|
||||
? {
|
||||
Math: {
|
||||
acosh: { stable: true, path: "math/acosh" },
|
||||
asinh: { stable: true, path: "math/asinh" },
|
||||
atanh: { stable: true, path: "math/atanh" },
|
||||
cbrt: { stable: true, path: "math/cbrt" },
|
||||
clz32: { stable: true, path: "math/clz32" },
|
||||
cosh: { stable: true, path: "math/cosh" },
|
||||
expm1: { stable: true, path: "math/expm1" },
|
||||
fround: { stable: true, path: "math/fround" },
|
||||
hypot: { stable: true, path: "math/hypot" },
|
||||
imul: { stable: true, path: "math/imul" },
|
||||
log10: { stable: true, path: "math/log10" },
|
||||
log1p: { stable: true, path: "math/log1p" },
|
||||
log2: { stable: true, path: "math/log2" },
|
||||
sign: { stable: true, path: "math/sign" },
|
||||
sinh: { stable: true, path: "math/sinh" },
|
||||
tanh: { stable: true, path: "math/tanh" },
|
||||
trunc: { stable: true, path: "math/trunc" },
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
|
||||
Symbol: {
|
||||
// FIXME: Disabled to work around zloirock/core-js#262.
|
||||
// asyncIterator: { stable: true, path: "symbol/async-iterator" },
|
||||
for: { stable: true, path: "symbol/for" },
|
||||
hasInstance: { stable: true, path: "symbol/has-instance" },
|
||||
isConcatSpreadable: {
|
||||
stable: true,
|
||||
path: "symbol/is-concat-spreadable",
|
||||
},
|
||||
iterator: { stable: true, path: "symbol/iterator" },
|
||||
keyFor: { stable: true, path: "symbol/key-for" },
|
||||
match: { stable: true, path: "symbol/match" },
|
||||
replace: { stable: true, path: "symbol/replace" },
|
||||
search: { stable: true, path: "symbol/search" },
|
||||
species: { stable: true, path: "symbol/species" },
|
||||
split: { stable: true, path: "symbol/split" },
|
||||
toPrimitive: { stable: true, path: "symbol/to-primitive" },
|
||||
toStringTag: { stable: true, path: "symbol/to-string-tag" },
|
||||
unscopables: { stable: true, path: "symbol/unscopables" },
|
||||
},
|
||||
|
||||
String: {
|
||||
at: { stable: true, path: "string/at" },
|
||||
fromCodePoint: { stable: true, path: "string/from-code-point" },
|
||||
raw: { stable: true, path: "string/raw" },
|
||||
},
|
||||
|
||||
Number: {
|
||||
EPSILON: { stable: true, path: "number/epsilon" },
|
||||
isFinite: { stable: true, path: "number/is-finite" },
|
||||
isInteger: { stable: true, path: "number/is-integer" },
|
||||
isNaN: { stable: true, path: "number/is-nan" },
|
||||
isSafeInteger: { stable: true, path: "number/is-safe-integer" },
|
||||
MAX_SAFE_INTEGER: { stable: true, path: "number/max-safe-integer" },
|
||||
MIN_SAFE_INTEGER: { stable: true, path: "number/min-safe-integer" },
|
||||
parseFloat: { stable: true, path: "number/parse-float" },
|
||||
parseInt: { stable: true, path: "number/parse-int" },
|
||||
},
|
||||
|
||||
Reflect: {
|
||||
apply: { stable: true, path: "reflect/apply" },
|
||||
construct: { stable: true, path: "reflect/construct" },
|
||||
defineProperty: { stable: true, path: "reflect/define-property" },
|
||||
deleteProperty: { stable: true, path: "reflect/delete-property" },
|
||||
getOwnPropertyDescriptor: {
|
||||
stable: true,
|
||||
path: "reflect/get-own-property-descriptor",
|
||||
},
|
||||
getPrototypeOf: { stable: true, path: "reflect/get-prototype-of" },
|
||||
get: { stable: true, path: "reflect/get" },
|
||||
has: { stable: true, path: "reflect/has" },
|
||||
isExtensible: { stable: true, path: "reflect/is-extensible" },
|
||||
ownKeys: { stable: true, path: "reflect/own-keys" },
|
||||
preventExtensions: { stable: true, path: "reflect/prevent-extensions" },
|
||||
setPrototypeOf: { stable: true, path: "reflect/set-prototype-of" },
|
||||
set: { stable: true, path: "reflect/set" },
|
||||
},
|
||||
|
||||
Date: {
|
||||
now: { stable: true, path: "date/now" },
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,227 @@
|
||||
export default () => {
|
||||
return {
|
||||
BuiltIns: {
|
||||
AggregateError: { stable: false, path: "aggregate-error" },
|
||||
Map: { stable: true, path: "map" },
|
||||
Observable: { stable: false, path: "observable" },
|
||||
Promise: { stable: true, path: "promise" },
|
||||
Set: { stable: true, path: "set" },
|
||||
Symbol: { stable: true, path: "symbol" },
|
||||
URL: { stable: true, path: "url" },
|
||||
URLSearchParams: { stable: true, path: "url-search-params" },
|
||||
WeakMap: { stable: true, path: "weak-map" },
|
||||
WeakSet: { stable: true, path: "weak-set" },
|
||||
clearImmediate: { stable: true, path: "clear-immediate" },
|
||||
compositeKey: { stable: false, path: "composite-key" },
|
||||
compositeSymbol: { stable: false, path: "composite-symbol" },
|
||||
globalThis: { stable: false, path: "global-this" },
|
||||
parseFloat: { stable: true, path: "parse-float" },
|
||||
parseInt: { stable: true, path: "parse-int" },
|
||||
queueMicrotask: { stable: true, path: "queue-microtask" },
|
||||
setImmediate: { stable: true, path: "set-immediate" },
|
||||
setInterval: { stable: true, path: "set-interval" },
|
||||
setTimeout: { stable: true, path: "set-timeout" },
|
||||
},
|
||||
|
||||
StaticProperties: {
|
||||
Array: {
|
||||
from: { stable: true, path: "array/from" },
|
||||
isArray: { stable: true, path: "array/is-array" },
|
||||
of: { stable: true, path: "array/of" },
|
||||
},
|
||||
|
||||
Date: {
|
||||
now: { stable: true, path: "date/now" },
|
||||
},
|
||||
|
||||
JSON: {
|
||||
stringify: { stable: true, path: "json/stringify" },
|
||||
},
|
||||
|
||||
Math: {
|
||||
DEG_PER_RAD: { stable: false, path: "math/deg-per-rad" },
|
||||
RAD_PER_DEG: { stable: false, path: "math/rad-per-deg" },
|
||||
acosh: { stable: true, path: "math/acosh" },
|
||||
asinh: { stable: true, path: "math/asinh" },
|
||||
atanh: { stable: true, path: "math/atanh" },
|
||||
cbrt: { stable: true, path: "math/cbrt" },
|
||||
clamp: { stable: false, path: "math/clamp" },
|
||||
clz32: { stable: true, path: "math/clz32" },
|
||||
cosh: { stable: true, path: "math/cosh" },
|
||||
degrees: { stable: false, path: "math/degrees" },
|
||||
expm1: { stable: true, path: "math/expm1" },
|
||||
fround: { stable: true, path: "math/fround" },
|
||||
fscale: { stable: false, path: "math/fscale" },
|
||||
hypot: { stable: true, path: "math/hypot" },
|
||||
iaddh: { stable: false, path: "math/iaddh" },
|
||||
imul: { stable: true, path: "math/imul" },
|
||||
imulh: { stable: false, path: "math/imulh" },
|
||||
isubh: { stable: false, path: "math/isubh" },
|
||||
log10: { stable: true, path: "math/log10" },
|
||||
log1p: { stable: true, path: "math/log1p" },
|
||||
log2: { stable: true, path: "math/log2" },
|
||||
radians: { stable: false, path: "math/radians" },
|
||||
scale: { stable: false, path: "math/scale" },
|
||||
seededPRNG: { stable: false, path: "math/seeded-prng" },
|
||||
sign: { stable: true, path: "math/sign" },
|
||||
signbit: { stable: false, path: "math/signbit" },
|
||||
sinh: { stable: true, path: "math/sinh" },
|
||||
tanh: { stable: true, path: "math/tanh" },
|
||||
trunc: { stable: true, path: "math/trunc" },
|
||||
umulh: { stable: false, path: "math/umulh" },
|
||||
},
|
||||
|
||||
Number: {
|
||||
EPSILON: { stable: true, path: "number/epsilon" },
|
||||
MAX_SAFE_INTEGER: { stable: true, path: "number/max-safe-integer" },
|
||||
MIN_SAFE_INTEGER: { stable: true, path: "number/min-safe-integer" },
|
||||
fromString: { stable: false, path: "number/from-string" },
|
||||
isFinite: { stable: true, path: "number/is-finite" },
|
||||
isInteger: { stable: true, path: "number/is-integer" },
|
||||
isNaN: { stable: true, path: "number/is-nan" },
|
||||
isSafeInteger: { stable: true, path: "number/is-safe-integer" },
|
||||
parseFloat: { stable: true, path: "number/parse-float" },
|
||||
parseInt: { stable: true, path: "number/parse-int" },
|
||||
},
|
||||
|
||||
Object: {
|
||||
assign: { stable: true, path: "object/assign" },
|
||||
create: { stable: true, path: "object/create" },
|
||||
defineProperties: { stable: true, path: "object/define-properties" },
|
||||
defineProperty: { stable: true, path: "object/define-property" },
|
||||
entries: { stable: true, path: "object/entries" },
|
||||
freeze: { stable: true, path: "object/freeze" },
|
||||
fromEntries: { stable: true, path: "object/from-entries" },
|
||||
getOwnPropertyDescriptor: {
|
||||
stable: true,
|
||||
path: "object/get-own-property-descriptor",
|
||||
},
|
||||
getOwnPropertyDescriptors: {
|
||||
stable: true,
|
||||
path: "object/get-own-property-descriptors",
|
||||
},
|
||||
getOwnPropertyNames: {
|
||||
stable: true,
|
||||
path: "object/get-own-property-names",
|
||||
},
|
||||
getOwnPropertySymbols: {
|
||||
stable: true,
|
||||
path: "object/get-own-property-symbols",
|
||||
},
|
||||
getPrototypeOf: { stable: true, path: "object/get-prototype-of" },
|
||||
isExtensible: { stable: true, path: "object/is-extensible" },
|
||||
isFrozen: { stable: true, path: "object/is-frozen" },
|
||||
isSealed: { stable: true, path: "object/is-sealed" },
|
||||
is: { stable: true, path: "object/is" },
|
||||
keys: { stable: true, path: "object/keys" },
|
||||
preventExtensions: { stable: true, path: "object/prevent-extensions" },
|
||||
seal: { stable: true, path: "object/seal" },
|
||||
setPrototypeOf: { stable: true, path: "object/set-prototype-of" },
|
||||
values: { stable: true, path: "object/values" },
|
||||
},
|
||||
|
||||
Reflect: {
|
||||
apply: { stable: true, path: "reflect/apply" },
|
||||
construct: { stable: true, path: "reflect/construct" },
|
||||
defineMetadata: { stable: false, path: "reflect/define-metadata" },
|
||||
defineProperty: { stable: true, path: "reflect/define-property" },
|
||||
deleteMetadata: { stable: false, path: "reflect/delete-metadata" },
|
||||
deleteProperty: { stable: true, path: "reflect/delete-property" },
|
||||
getMetadata: { stable: false, path: "reflect/get-metadata" },
|
||||
getMetadataKeys: { stable: false, path: "reflect/get-metadata-keys" },
|
||||
getOwnMetadata: { stable: false, path: "reflect/get-own-metadata" },
|
||||
getOwnMetadataKeys: {
|
||||
stable: false,
|
||||
path: "reflect/get-own-metadata-keys",
|
||||
},
|
||||
getOwnPropertyDescriptor: {
|
||||
stable: true,
|
||||
path: "reflect/get-own-property-descriptor",
|
||||
},
|
||||
getPrototypeOf: { stable: true, path: "reflect/get-prototype-of" },
|
||||
get: { stable: true, path: "reflect/get" },
|
||||
has: { stable: true, path: "reflect/has" },
|
||||
hasMetadata: { stable: false, path: "reflect/has-metadata" },
|
||||
hasOwnMetadata: { stable: false, path: "reflect/has-own-metadata" },
|
||||
isExtensible: { stable: true, path: "reflect/is-extensible" },
|
||||
metadata: { stable: false, path: "reflect/metadata" },
|
||||
ownKeys: { stable: true, path: "reflect/own-keys" },
|
||||
preventExtensions: { stable: true, path: "reflect/prevent-extensions" },
|
||||
set: { stable: true, path: "reflect/set" },
|
||||
setPrototypeOf: { stable: true, path: "reflect/set-prototype-of" },
|
||||
},
|
||||
|
||||
String: {
|
||||
fromCodePoint: { stable: true, path: "string/from-code-point" },
|
||||
raw: { stable: true, path: "string/raw" },
|
||||
},
|
||||
|
||||
Symbol: {
|
||||
asyncIterator: { stable: true, path: "symbol/async-iterator" },
|
||||
dispose: { stable: false, path: "symbol/dispose" },
|
||||
for: { stable: true, path: "symbol/for" },
|
||||
hasInstance: { stable: true, path: "symbol/has-instance" },
|
||||
isConcatSpreadable: {
|
||||
stable: true,
|
||||
path: "symbol/is-concat-spreadable",
|
||||
},
|
||||
iterator: { stable: true, path: "symbol/iterator" },
|
||||
keyFor: { stable: true, path: "symbol/key-for" },
|
||||
match: { stable: true, path: "symbol/match" },
|
||||
observable: { stable: false, path: "symbol/observable" },
|
||||
patternMatch: { stable: false, path: "symbol/pattern-match" },
|
||||
replace: { stable: true, path: "symbol/replace" },
|
||||
search: { stable: true, path: "symbol/search" },
|
||||
species: { stable: true, path: "symbol/species" },
|
||||
split: { stable: true, path: "symbol/split" },
|
||||
toPrimitive: { stable: true, path: "symbol/to-primitive" },
|
||||
toStringTag: { stable: true, path: "symbol/to-string-tag" },
|
||||
unscopables: { stable: true, path: "symbol/unscopables" },
|
||||
},
|
||||
},
|
||||
|
||||
InstanceProperties: {
|
||||
at: { stable: false, path: "at" },
|
||||
bind: { stable: true, path: "bind" },
|
||||
codePointAt: { stable: true, path: "code-point-at" },
|
||||
codePoints: { stable: false, path: "code-points" },
|
||||
concat: { stable: true, path: "concat" },
|
||||
copyWithin: { stable: true, path: "copy-within" },
|
||||
endsWith: { stable: true, path: "ends-with" },
|
||||
entries: { stable: true, path: "entries" },
|
||||
every: { stable: true, path: "every" },
|
||||
fill: { stable: true, path: "fill" },
|
||||
filter: { stable: true, path: "filter" },
|
||||
find: { stable: true, path: "find" },
|
||||
findIndex: { stable: true, path: "find-index" },
|
||||
flags: { stable: true, path: "flags" },
|
||||
flatMap: { stable: true, path: "flat-map" },
|
||||
flat: { stable: true, path: "flat" },
|
||||
forEach: { stable: true, path: "for-each" },
|
||||
includes: { stable: true, path: "includes" },
|
||||
indexOf: { stable: true, path: "index-of" },
|
||||
keys: { stable: true, path: "keys" },
|
||||
lastIndexOf: { stable: true, path: "last-index-of" },
|
||||
map: { stable: true, path: "map" },
|
||||
matchAll: { stable: false, path: "match-all" },
|
||||
padEnd: { stable: true, path: "pad-end" },
|
||||
padStart: { stable: true, path: "pad-start" },
|
||||
reduce: { stable: true, path: "reduce" },
|
||||
reduceRight: { stable: true, path: "reduce-right" },
|
||||
repeat: { stable: true, path: "repeat" },
|
||||
replaceAll: { stable: false, path: "replace-all" },
|
||||
reverse: { stable: true, path: "reverse" },
|
||||
slice: { stable: true, path: "slice" },
|
||||
some: { stable: true, path: "some" },
|
||||
sort: { stable: true, path: "sort" },
|
||||
splice: { stable: true, path: "splice" },
|
||||
startsWith: { stable: true, path: "starts-with" },
|
||||
trim: { stable: true, path: "trim" },
|
||||
trimEnd: { stable: true, path: "trim-end" },
|
||||
trimLeft: { stable: true, path: "trim-left" },
|
||||
trimRight: { stable: true, path: "trim-right" },
|
||||
trimStart: { stable: true, path: "trim-start" },
|
||||
values: { stable: true, path: "values" },
|
||||
},
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user