Skip to content

Commit

Permalink
Allow polyfill providers to specify custom @babel/runtime pkg (#15531)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed May 24, 2023
1 parent 03c51ca commit 72e3184
Show file tree
Hide file tree
Showing 22 changed files with 291 additions and 209 deletions.
2 changes: 1 addition & 1 deletion Makefile.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -46,7 +46,7 @@
"c8": "^7.12.0",
"chalk": "^5.0.0",
"charcodes": "^0.2.0",
"core-js": "^3.26.0",
"core-js": "^3.30.2",
"eslint": "^8.22.0",
"eslint-formatter-codeframe": "^7.32.1",
"eslint-import-resolver-node": "^0.3.6",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-compat-data/package.json
Expand Up @@ -41,7 +41,7 @@
],
"devDependencies": {
"@mdn/browser-compat-data": "^4.0.10",
"core-js-compat": "^3.25.1",
"core-js-compat": "^3.30.2",
"electron-to-chromium": "^1.4.248"
},
"engines": {
Expand Down
10 changes: 10 additions & 0 deletions packages/babel-helper-transform-fixture-test-runner/src/index.ts
Expand Up @@ -457,6 +457,16 @@ function normalizeOutput(
if (normalizePresetEnvDebug) {
result = result.replace(/(\s+)proposal-/gm, "$1transform-");
}

// For some reasons, in older Node.js versions some symlinks are not properly
// resolved. The behavior is still ok, but we need to unify the output with
// newer Node.js versions.
if (parseInt(process.versions.node, 10) <= 8) {
result = result.replace(
/<CWD>\/node_modules\/@babel\/runtime-corejs3/g,
"<CWD>/packages/babel-runtime-corejs3",
);
}
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-node/package.json
Expand Up @@ -25,7 +25,7 @@
"dependencies": {
"@babel/register": "workspace:^",
"commander": "^4.0.1",
"core-js": "^3.26.0",
"core-js": "^3.30.2",
"node-environment-flags": "^1.0.5",
"regenerator-runtime": "^0.13.11",
"v8flags": "^3.1.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-proposal-decorators/package.json
Expand Up @@ -35,7 +35,7 @@
"@babel/traverse": "workspace:^",
"@types/charcodes": "^0.2.0",
"array.prototype.concat": "^1.0.2",
"babel-plugin-polyfill-es-shims": "^0.7.1",
"babel-plugin-polyfill-es-shims": "^0.9.0",
"charcodes": "^0.2.0",
"object.getownpropertydescriptors": "^2.1.1"
},
Expand Down
Expand Up @@ -30,7 +30,7 @@
"devDependencies": {
"@babel/core": "workspace:^",
"@babel/helper-plugin-test-runner": "workspace:^",
"core-js": "^3.26.0"
"core-js": "^3.30.2"
},
"engines": {
"node": ">=6.9.0"
Expand Down
Expand Up @@ -28,8 +28,8 @@
"devDependencies": {
"@babel/core": "workspace:^",
"@babel/helper-plugin-test-runner": "workspace:^",
"babel-plugin-polyfill-corejs3": "^0.6.0",
"core-js-pure": "^3.25.1"
"babel-plugin-polyfill-corejs3": "^0.8.1",
"core-js-pure": "^3.30.2"
},
"engines": {
"node": ">=6.9.0"
Expand Down
Expand Up @@ -30,7 +30,7 @@
"devDependencies": {
"@babel/core": "workspace:^",
"@babel/helper-plugin-test-runner": "workspace:^",
"core-js": "^3.26.0"
"core-js": "^3.30.2"
},
"engines": {
"node": ">=6.9.0"
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-plugin-transform-runtime/package.json
Expand Up @@ -22,9 +22,9 @@
"dependencies": {
"@babel/helper-module-imports": "workspace:^",
"@babel/helper-plugin-utils": "workspace:^",
"babel-plugin-polyfill-corejs2": "^0.3.3",
"babel-plugin-polyfill-corejs3": "^0.6.0",
"babel-plugin-polyfill-regenerator": "^0.4.1",
"babel-plugin-polyfill-corejs2": "^0.4.1",
"babel-plugin-polyfill-corejs3": "^0.8.1",
"babel-plugin-polyfill-regenerator": "^0.5.0",
"semver": "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
},
"peerDependencies": {
Expand Down
179 changes: 16 additions & 163 deletions packages/babel-plugin-transform-runtime/src/index.ts
@@ -1,22 +1,10 @@
import { declare } from "@babel/helper-plugin-utils";
import { addDefault, isModule } from "@babel/helper-module-imports";
import { types as t } from "@babel/core";
import { types as t, type CallerMetadata } from "@babel/core";

import { hasMinVersion } from "./helpers";
import getRuntimePath, { resolveFSPath } from "./get-runtime-path";
import type { PluginAPI, PluginObject, CallerMetadata } from "@babel/core";

import _pluginCorejs2 from "babel-plugin-polyfill-corejs2";
import _pluginCorejs3 from "babel-plugin-polyfill-corejs3";
import _pluginRegenerator from "babel-plugin-polyfill-regenerator";
const pluginCorejs2 = (_pluginCorejs2.default ||
_pluginCorejs2) as typeof _pluginCorejs2.default;
const pluginCorejs3 = (_pluginCorejs3.default ||
_pluginCorejs3) as typeof _pluginCorejs3.default;
const pluginRegenerator = (_pluginRegenerator.default ||
_pluginRegenerator) as typeof _pluginRegenerator.default;

const pluginsCompat = "#__secret_key__@babel/runtime__compatibility";
import { createBasePolyfillsPlugin } from "./polyfills";

function supportsStaticESM(caller: CallerMetadata | undefined) {
// @ts-expect-error TS does not narrow down optional chaining
Expand All @@ -32,79 +20,16 @@ export interface Options {
version?: string;
}

interface CoreJS2PluginOptions {
absoluteImports: string | false;
method: "usage-pure";
[pluginsCompat]: {
runtimeVersion: string;
useBabelRuntime: string | false;
ext: string;
};
}

interface RegeneratorPluginOptions {
absoluteImports: string | false;
method: "usage-pure";
[pluginsCompat]: {
useBabelRuntime: string | false;
};
}

interface CoreJS3PluginOptions {
absoluteImports: string | false;
method: "usage-pure";
proposals: boolean;
version: number;
[pluginsCompat]: {
useBabelRuntime: string | false;
ext: string;
};
}

export default declare((api, options: Options, dirname) => {
api.assertVersion(7);

const {
corejs,
helpers: useRuntimeHelpers = true,
regenerator: useRuntimeRegenerator = true,
useESModules = false,
version: runtimeVersion = "7.0.0-beta.0",
absoluteRuntime = false,
} = options;

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.");
}
Expand Down Expand Up @@ -183,101 +108,29 @@ export default declare((api, options: Options, dirname) => {
const esModules =
useESModules === "auto" ? api.caller(supportsStaticESM) : useESModules;

const injectCoreJS2 = corejsVersion === 2;
const injectCoreJS3 = corejsVersion === 3;

const moduleName = injectCoreJS3
? "@babel/runtime-corejs3"
: injectCoreJS2
? "@babel/runtime-corejs2"
: "@babel/runtime";

const HEADER_HELPERS = ["interopRequireWildcard", "interopRequireDefault"];

const modulePath = getRuntimePath(moduleName, dirname, absoluteRuntime);

function createCorejsPlugin<Options extends {}>(
plugin: (
api: PluginAPI,
options: Options,
filename: string,
) => PluginObject,
options: Options,
regeneratorPlugin: (
api: PluginAPI,
options: RegeneratorPluginOptions,
filename: string,
) => PluginObject,
): (api: PluginAPI, options: {}, filename: string) => PluginObject {
return (api: PluginAPI, _: {}, filename: string) => {
return {
...plugin(api, options, filename),
inherits: regeneratorPlugin,
};
};
}

// TODO: Remove this in Babel 8
function createRegeneratorPlugin(
options: RegeneratorPluginOptions,
): (
api: PluginAPI,
options: RegeneratorPluginOptions,
filename: string,
) => PluginObject {
if (!useRuntimeRegenerator) return undefined;
return (api, _, filename) => {
return pluginRegenerator(api, options, filename);
};
}

return {
name: "transform-runtime",

inherits: injectCoreJS2
? createCorejsPlugin<CoreJS2PluginOptions>(
pluginCorejs2,
{
method: "usage-pure",
absoluteImports: absoluteRuntime ? modulePath : false,
[pluginsCompat]: {
runtimeVersion,
useBabelRuntime: modulePath,
ext: "",
},
},
createRegeneratorPlugin({
method: "usage-pure",
absoluteImports: absoluteRuntime ? modulePath : false,
[pluginsCompat]: { useBabelRuntime: modulePath },
}),
)
: injectCoreJS3
? createCorejsPlugin<CoreJS3PluginOptions>(
pluginCorejs3,
{
method: "usage-pure",
version: 3,
proposals,
absoluteImports: absoluteRuntime ? modulePath : false,
[pluginsCompat]: { useBabelRuntime: modulePath, ext: "" },
},
createRegeneratorPlugin({
method: "usage-pure",
absoluteImports: absoluteRuntime ? modulePath : false,
[pluginsCompat]: { useBabelRuntime: modulePath },
}),
)
: createRegeneratorPlugin({
method: "usage-pure",
absoluteImports: absoluteRuntime ? modulePath : false,
[pluginsCompat]: { useBabelRuntime: modulePath },
}),
inherits: createBasePolyfillsPlugin(
options,
runtimeVersion,
absoluteRuntime,
),

pre(file) {
if (!useRuntimeHelpers) return;

let modulePath: string;

file.set("helperGenerator", (name: string) => {
modulePath ??= getRuntimePath(
file.get("runtimeHelpersModuleName") ?? "@babel/runtime",
dirname,
absoluteRuntime,
);

// If the helper didn't exist yet at the version given, we bail
// out and let Babel either insert it directly, or throw an error
// so that plugins can handle that case properly.
Expand All @@ -287,7 +140,7 @@ export default declare((api, options: Options, dirname) => {
// For regeneratorRuntime, we can fallback to the old behavior of
// relying on the regeneratorRuntime global. If the 'regenerator'
// option is not disabled, babel-plugin-polyfill-regenerator will
// then replace it with a @babel/helpers/regeneratorRuntime import.
// then replace it with a @babel/helpers/regenerator import.
//
// We must wrap it in a function, because built-in Babel helpers
// are functions.
Expand Down

0 comments on commit 72e3184

Please sign in to comment.