Skip to content

Commit

Permalink
[babel 8] Remove core-js@2 & regenerator from transform-runtime (#16063)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Nov 13, 2023
1 parent e368442 commit 210616f
Show file tree
Hide file tree
Showing 27 changed files with 190 additions and 178 deletions.
3 changes: 3 additions & 0 deletions packages/babel-plugin-transform-runtime/src/babel-7/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Object.defineProperty(exports, "createPolyfillPlugins", {
get: () => require("./polyfills.cjs"),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const createPolyfillPlugins: any;
80 changes: 80 additions & 0 deletions packages/babel-plugin-transform-runtime/src/babel-7/polyfills.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// TODO(Babel 8) Remove this file
if (process.env.BABEL_8_BREAKING) {
throw new Error(
"Internal Babel error: This file should only be loaded in Babel 7",
);
}

const pluginCorejs2 = require("babel-plugin-polyfill-corejs2").default;
const pluginRegenerator = require("babel-plugin-polyfill-regenerator").default;

const pluginsCompat = "#__secret_key__@babel/runtime__compatibility";

function createCorejs2Plugin(options) {
return (api, _, filename) => pluginCorejs2(api, options, filename);
}

function createRegeneratorPlugin(options, useRuntimeRegenerator, corejsPlugin) {
if (!useRuntimeRegenerator) return corejsPlugin ?? undefined;
return (api, _, filename) => {
return {
...pluginRegenerator(api, options, filename),
inherits: corejsPlugin ?? undefined,
};
};
}

module.exports = function createBasePolyfillsPlugin(
{ corejs, regenerator = true },
runtimeVersion,
absoluteImports,
corejs3Plugin,
) {
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 regenerator !== "boolean") {
throw new Error(
"The 'regenerator' option must be undefined, or a boolean.",
);
}

const polyfillOpts = {
method: "usage-pure",
absoluteImports,
[pluginsCompat]: { useBabelRuntime: true, runtimeVersion, ext: "" },
};

return createRegeneratorPlugin(
polyfillOpts,
regenerator,
corejsVersion === 2
? createCorejs2Plugin(polyfillOpts)
: corejsVersion === 3
? corejs3Plugin
: null,
);
};
52 changes: 52 additions & 0 deletions packages/babel-plugin-transform-runtime/src/core-js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// TODO: Consider removing babel-plugin-polyfill-corejs3 from here, and ask
// users to explicitly enable it in their Babel configuration files.

import type { PluginAPI } from "@babel/core";
import _pluginCorejs3 from "babel-plugin-polyfill-corejs3";
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const pluginCorejs3 = (_pluginCorejs3.default ||
_pluginCorejs3) as typeof _pluginCorejs3.default;

import type { Options } from "./index.ts";

const pluginsCompat = "#__secret_key__@babel/runtime__compatibility";

export function createCorejs3Plugin(
corejs: Options["corejs"],
absoluteImports: boolean,
) {
let proposals = false;
let rawVersion;

if (typeof corejs === "object" && corejs !== null) {
rawVersion = corejs.version;
proposals = Boolean(corejs.proposals);
} else {
rawVersion = corejs;
}

if (!rawVersion) return null;

const version = rawVersion ? Number(rawVersion) : false;

// TODO: Allow specifying minor versions
if (version !== 3) {
throw new Error(
`The \`core-js\` version must be 3, but got ${JSON.stringify(
rawVersion,
)}.`,
);
}

return (api: PluginAPI, _: {}, filename: string) =>
pluginCorejs3(
api,
{
method: "usage-pure",
proposals,
absoluteImports,
[pluginsCompat]: { useBabelRuntime: true, ext: "" },
},
filename,
);
}
35 changes: 28 additions & 7 deletions packages/babel-plugin-transform-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { types as t, type CallerMetadata } from "@babel/core";

import { hasMinVersion } from "./helpers.ts";
import getRuntimePath, { resolveFSPath } from "./get-runtime-path/index.ts";
import { createBasePolyfillsPlugin } from "./polyfills.ts";
import { createCorejs3Plugin } from "./core-js.ts";

// TODO(Babel 8): Remove this
import babel7 from "./babel-7/index.cjs";

function supportsStaticESM(caller: CallerMetadata | undefined) {
// @ts-expect-error TS does not narrow down optional chaining
Expand All @@ -15,7 +18,6 @@ export interface Options {
absoluteRuntime?: boolean;
corejs?: string | number | { version: string | number; proposals?: boolean };
helpers?: boolean;
regenerator?: boolean;
useESModules?: boolean | "auto";
version?: string;
}
Expand Down Expand Up @@ -109,6 +111,17 @@ export default declare((api, options: Options, dirname) => {
);
}

if (process.env.BABEL_8_BREAKING) {
if (has(options, "regenerator")) {
throw new Error(
"The 'regenerator' option has been removed. The generators transform " +
"no longers relies on a 'regeneratorRuntime' global. " +
"If you still need to replace imports to the 'regeneratorRuntime' " +
"global, you can use babel-plugin-polyfill-regenerator.",
);
}
}

const esModules =
useESModules === "auto" ? api.caller(supportsStaticESM) : useESModules;

Expand All @@ -117,11 +130,19 @@ export default declare((api, options: Options, dirname) => {
return {
name: "transform-runtime",

inherits: createBasePolyfillsPlugin(
options,
runtimeVersion,
absoluteRuntime,
),
inherits: process.env.BABEL_8_BREAKING
? options.corejs
? createCorejs3Plugin(options.corejs, absoluteRuntime)
: undefined
: babel7.createPolyfillPlugins(
options,
runtimeVersion,
absoluteRuntime,
options.corejs === 3 ||
(options.corejs as Options["corejs"] & object)?.version === 3
? createCorejs3Plugin(options.corejs, absoluteRuntime)
: null,
),

pre(file) {
if (!useRuntimeHelpers) return;
Expand Down
141 changes: 0 additions & 141 deletions packages/babel-plugin-transform-runtime/src/polyfills.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
[
"transform-runtime",
{
"regenerator": false,
"corejs": false,
"helpers": true,
"useESModules": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"BABEL_8_BREAKING": false,
"plugins": [["transform-runtime", { "corejs": 2 }], "transform-regenerator"]
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"BABEL_8_BREAKING": false,
"plugins": [["transform-runtime", { "corejs": 2 }], "transform-regenerator"]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"plugins": [
["transform-runtime", { "corejs": 2, "version": "7.100.0" }],
"transform-classes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"plugins": [
"transform-for-of",
["transform-runtime", { "corejs": 2, "version": "7.100.0" }]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"BABEL_8_BREAKING": false,
"plugins": [["transform-runtime", { "corejs": 2 }]]
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"plugins": [["transform-runtime", { "corejs": 2, "version": "7.100.0" }], "transform-regenerator"]
"BABEL_8_BREAKING": false,
"plugins": [
["transform-runtime", { "corejs": 2, "version": "7.100.0" }],
"transform-regenerator"
]
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"BABEL_8_BREAKING": false,
"plugins": [["transform-runtime", { "corejs": 2 }], "transform-regenerator"]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"plugins": [
[
"transform-runtime",
Expand Down
Loading

0 comments on commit 210616f

Please sign in to comment.