### `@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.
208 lines
5.6 KiB
JavaScript
208 lines
5.6 KiB
JavaScript
"use strict";
|
|
|
|
const path = require("path");
|
|
const outputFile = require("output-file-sync");
|
|
const helpers = require("@babel/helpers");
|
|
const babel = require("@babel/core");
|
|
const template = require("@babel/template");
|
|
const t = require("@babel/types");
|
|
|
|
const transformRuntime = require("../");
|
|
const transformMemberExpressionLiterals = require("@babel/plugin-transform-member-expression-literals");
|
|
const transformPropertyLiterals = require("@babel/plugin-transform-property-literals");
|
|
|
|
const corejs2Definitions = require("../lib/runtime-corejs2-definitions").default();
|
|
const corejs3Definitions = require("../lib/runtime-corejs3-definitions").default();
|
|
|
|
writeHelpers("@babel/runtime");
|
|
writeHelpers("@babel/runtime-corejs2", { corejs: 2 });
|
|
writeHelpers("@babel/runtime-corejs3", {
|
|
corejs: { version: 3, proposals: true },
|
|
});
|
|
|
|
writeCoreJS({
|
|
corejs: 2,
|
|
proposals: true,
|
|
definitions: corejs2Definitions,
|
|
paths: [
|
|
"is-iterable",
|
|
"get-iterator",
|
|
// This was previously in definitions, but was removed to work around
|
|
// zloirock/core-js#262. We need to keep it in @babel/runtime-corejs2 to
|
|
// avoid a breaking change there.
|
|
"symbol/async-iterator",
|
|
],
|
|
corejsRoot: "core-js/library/fn",
|
|
});
|
|
writeCoreJS({
|
|
corejs: 3,
|
|
proposals: false,
|
|
definitions: corejs3Definitions,
|
|
paths: [],
|
|
corejsRoot: "core-js-pure/stable",
|
|
});
|
|
writeCoreJS({
|
|
corejs: 3,
|
|
proposals: true,
|
|
definitions: corejs3Definitions,
|
|
paths: ["is-iterable", "get-iterator", "get-iterator-method"],
|
|
corejsRoot: "core-js-pure/features",
|
|
});
|
|
|
|
function writeCoreJS({
|
|
corejs,
|
|
proposals,
|
|
definitions: { BuiltIns, StaticProperties, InstanceProperties },
|
|
paths,
|
|
corejsRoot,
|
|
}) {
|
|
const pkgDirname = getRuntimeRoot(`@babel/runtime-corejs${corejs}`);
|
|
|
|
Object.keys(BuiltIns).forEach(name => {
|
|
const { stable, path } = BuiltIns[name];
|
|
if (stable || proposals) paths.push(path);
|
|
});
|
|
|
|
Object.keys(StaticProperties).forEach(builtin => {
|
|
const props = StaticProperties[builtin];
|
|
Object.keys(props).forEach(name => {
|
|
const { stable, path } = props[name];
|
|
if (stable || proposals) paths.push(path);
|
|
});
|
|
});
|
|
|
|
if (InstanceProperties) {
|
|
Object.keys(InstanceProperties).forEach(name => {
|
|
const { stable, path } = InstanceProperties[name];
|
|
if (stable || proposals) paths.push(`instance/${path}`);
|
|
});
|
|
}
|
|
|
|
const runtimeRoot = proposals ? "core-js" : "core-js-stable";
|
|
paths.forEach(function(corejsPath) {
|
|
outputFile(
|
|
path.join(pkgDirname, runtimeRoot, `${corejsPath}.js`),
|
|
`module.exports = require("${corejsRoot}/${corejsPath}");`
|
|
);
|
|
});
|
|
}
|
|
|
|
function writeHelpers(runtimeName, { corejs } = {}) {
|
|
writeHelperFiles(runtimeName, { corejs, esm: false });
|
|
writeHelperFiles(runtimeName, { corejs, esm: true });
|
|
}
|
|
|
|
function writeHelperFiles(runtimeName, { esm, corejs }) {
|
|
const pkgDirname = getRuntimeRoot(runtimeName);
|
|
|
|
for (const helperName of helpers.list) {
|
|
const helperFilename = path.join(
|
|
pkgDirname,
|
|
"helpers",
|
|
esm ? "esm" : "",
|
|
`${helperName}.js`
|
|
);
|
|
|
|
outputFile(
|
|
helperFilename,
|
|
buildHelper(runtimeName, pkgDirname, helperFilename, helperName, {
|
|
esm,
|
|
corejs,
|
|
})
|
|
);
|
|
}
|
|
}
|
|
|
|
function getRuntimeRoot(runtimeName) {
|
|
return path.resolve(
|
|
__dirname,
|
|
"..",
|
|
"..",
|
|
runtimeName.replace(/^@babel\//, "babel-")
|
|
);
|
|
}
|
|
|
|
function buildHelper(
|
|
runtimeName,
|
|
pkgDirname,
|
|
helperFilename,
|
|
helperName,
|
|
{ esm, corejs }
|
|
) {
|
|
const tree = t.program([], [], esm ? "module" : "script");
|
|
const dependencies = {};
|
|
let bindings = null;
|
|
|
|
if (!esm) {
|
|
bindings = [];
|
|
for (const dep of helpers.getDependencies(helperName)) {
|
|
const id = (dependencies[dep] = t.identifier(t.toIdentifier(dep)));
|
|
tree.body.push(template.statement.ast`
|
|
var ${id} = require("${`./${dep}`}");
|
|
`);
|
|
bindings.push(id.name);
|
|
}
|
|
}
|
|
|
|
const helper = helpers.get(
|
|
helperName,
|
|
dep => dependencies[dep],
|
|
esm ? null : template.expression.ast`module.exports`,
|
|
bindings
|
|
);
|
|
tree.body.push(...helper.nodes);
|
|
|
|
return babel.transformFromAst(tree, null, {
|
|
presets: [[require("@babel/preset-env"), { modules: false }]],
|
|
plugins: [
|
|
[transformRuntime, { corejs, useESModules: esm }],
|
|
[transformMemberExpressionLiterals],
|
|
[transformPropertyLiterals],
|
|
buildRuntimeRewritePlugin(
|
|
runtimeName,
|
|
path.relative(path.dirname(helperFilename), pkgDirname),
|
|
helperName
|
|
),
|
|
],
|
|
}).code;
|
|
}
|
|
|
|
function buildRuntimeRewritePlugin(runtimeName, relativePath, helperName) {
|
|
function adjustImportPath(node, relativePath) {
|
|
node.value =
|
|
helpers.list.indexOf(node.value) !== -1
|
|
? `./${node.value}`
|
|
: node.value.replace(runtimeName + "/", relativePath + "/");
|
|
}
|
|
|
|
return {
|
|
pre(file) {
|
|
const original = file.get("helperGenerator");
|
|
file.set("helperGenerator", name => {
|
|
// make sure that helpers won't insert circular references to themselves
|
|
if (name === helperName) return false;
|
|
|
|
return original(name);
|
|
});
|
|
},
|
|
visitor: {
|
|
ImportDeclaration(path) {
|
|
adjustImportPath(path.get("source").node, relativePath);
|
|
},
|
|
CallExpression(path) {
|
|
if (
|
|
!path.get("callee").isIdentifier({ name: "require" }) ||
|
|
path.get("arguments").length !== 1 ||
|
|
!path.get("arguments")[0].isStringLiteral()
|
|
) {
|
|
return;
|
|
}
|
|
|
|
// replace any reference to @babel/runtime and other helpers
|
|
// with a relative path
|
|
adjustImportPath(path.get("arguments")[0].node, relativePath);
|
|
},
|
|
},
|
|
};
|
|
}
|