Skip to content

Commit

Permalink
[babel 8] Remove core-js 2 and regenerator from preset-env (#15838)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Oct 13, 2023
1 parent 51c234d commit 0ba00c4
Show file tree
Hide file tree
Showing 414 changed files with 618 additions and 7,737 deletions.
20 changes: 10 additions & 10 deletions .yarn/plugins/@yarnpkg/plugin-conditions.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ function importInteropSrc(source, filename) {
return "node";
}
if (
source[0] === "." ||
(source[0] === "." && !source.endsWith(".cjs")) ||
getMonorepoPackages().some(name => source.startsWith(name))
) {
// We don't need to worry about interop for internal files, since we know
Expand Down
1 change: 1 addition & 0 deletions constraints.pro
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ gen_enforced_field(WorkspaceCwd, 'exports', '{ ".": "./lib/index.js", "./package
workspace_ident(WorkspaceCwd, WorkspaceIdent),
WorkspaceIdent \= '@babel/compat-data',
WorkspaceIdent \= '@babel/helper-plugin-test-runner', % TODO: Remove in Babel 8
WorkspaceIdent \= '@babel/core', % TODO: Remove in Babel 8
WorkspaceIdent \= '@babel/parser',
WorkspaceIdent \= '@babel/plugin-transform-react-jsx', % TODO: Remove in Babel 8
WorkspaceIdent \= '@babel/standalone',
Expand Down
1 change: 1 addition & 0 deletions packages/babel-core/.npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
src
test
*.log
cjs-proxy-dev.cjs
4 changes: 4 additions & 0 deletions packages/babel-core/cjs-proxy-dev.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-disable */
module.exports = require("$repo-utils").USE_ESM
? require("./cjs-proxy.cjs")
: require("./lib/index.js");
5 changes: 4 additions & 1 deletion packages/babel-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@
]
},
"exports": {
".": "./lib/index.js",
".": {
"require": "./cjs-proxy-dev.cjs",
"default": "./lib/index.js"
},
"./package.json": "./package.json"
},
"type": "commonjs"
Expand Down
5 changes: 2 additions & 3 deletions packages/babel-preset-env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@
"@babel/plugin-transform-unicode-regex": "workspace:^",
"@babel/plugin-transform-unicode-sets-regex": "workspace:^",
"@babel/preset-modules": "0.1.6-no-external-plugins",
"@babel/types": "workspace:^",
"babel-plugin-polyfill-corejs2": "^0.4.6",
"babel-plugin-polyfill-corejs2": "condition:BABEL_8_BREAKING ? : ^0.4.6 (peer:@babel/core) (esm:default)",
"babel-plugin-polyfill-corejs3": "^0.8.5",
"babel-plugin-polyfill-regenerator": "^0.5.3",
"babel-plugin-polyfill-regenerator": "condition:BABEL_8_BREAKING ? : ^0.5.3 (peer:@babel/core) (esm:default)",
"core-js-compat": "^3.31.0",
"semver": "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.1"
},
Expand Down
204 changes: 133 additions & 71 deletions packages/babel-preset-env/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@ import {
overlappingPlugins,
} from "./plugins-compat-data.ts";

import removeRegeneratorEntryPlugin from "./polyfills/regenerator.ts";
import legacyBabelPolyfillPlugin from "./polyfills/babel-polyfill.ts";

import type { 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;
// TODO(Babel 8): Just use the default import
const pluginCoreJS3 = _pluginCoreJS3.default || _pluginCoreJS3;
const pluginRegenerator = _pluginRegenerator.default || _pluginRegenerator;

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

import getTargets, {
prettifyTargets,
Expand Down Expand Up @@ -170,15 +167,14 @@ export const getModulesPluginNames = ({
return modulesPluginNames;
};

export const getPolyfillPlugins = ({
const getCoreJSOptions = ({
useBuiltIns,
corejs,
polyfillTargets,
include,
exclude,
proposals,
shippedProposals,
regenerator,
debug,
}: {
useBuiltIns: BuiltInsOption;
Expand All @@ -188,64 +184,112 @@ export const getPolyfillPlugins = ({
exclude: Set<string>;
proposals: boolean;
shippedProposals: boolean;
regenerator: boolean;
debug: boolean;
}) => {
const polyfillPlugins = [];
if (useBuiltIns === "usage" || useBuiltIns === "entry") {
const pluginOptions = {
method: `${useBuiltIns}-global`,
version: corejs ? corejs.toString() : undefined,
targets: polyfillTargets,
include,
exclude,
proposals,
shippedProposals,
debug,
"#__secret_key__@babel/preset-env__compatibility": {
noRuntimeName: true,
},
};

if (corejs) {
if (useBuiltIns === "usage") {
if (corejs.major === 2) {
polyfillPlugins.push(
[pluginCoreJS2, pluginOptions],
[legacyBabelPolyfillPlugin, { usage: true }],
);
} else {
polyfillPlugins.push(
[pluginCoreJS3, pluginOptions],
[legacyBabelPolyfillPlugin, { usage: true, deprecated: true }],
);
}
if (regenerator) {
polyfillPlugins.push([
pluginRegenerator,
{ method: "usage-global", debug },
]);
}
} else {
if (corejs.major === 2) {
polyfillPlugins.push(
[legacyBabelPolyfillPlugin, { regenerator }],
[pluginCoreJS2, pluginOptions],
);
}) => ({
method: `${useBuiltIns}-global`,
version: corejs ? corejs.toString() : undefined,
targets: polyfillTargets,
include,
exclude,
proposals,
shippedProposals,
debug,
"#__secret_key__@babel/preset-env__compatibility": {
noRuntimeName: true,
},
});

if (!process.env.BABEL_8_BREAKING) {
// eslint-disable-next-line no-var
var getPolyfillPlugins = ({
useBuiltIns,
corejs,
polyfillTargets,
include,
exclude,
proposals,
shippedProposals,
regenerator,
debug,
}: {
useBuiltIns: BuiltInsOption;
corejs: SemVer | null | false;
polyfillTargets: Targets;
include: Set<string>;
exclude: Set<string>;
proposals: boolean;
shippedProposals: boolean;
regenerator: boolean;
debug: boolean;
}) => {
const polyfillPlugins = [];
if (useBuiltIns === "usage" || useBuiltIns === "entry") {
const pluginOptions = getCoreJSOptions({
useBuiltIns,
corejs,
polyfillTargets,
include,
exclude,
proposals,
shippedProposals,
debug,
});

if (corejs) {
if (process.env.BABEL_8_BREAKING) {
polyfillPlugins.push([pluginCoreJS3, pluginOptions]);
} else {
polyfillPlugins.push(
[pluginCoreJS3, pluginOptions],
[legacyBabelPolyfillPlugin, { deprecated: true }],
);
if (!regenerator) {
polyfillPlugins.push([removeRegeneratorEntryPlugin, pluginOptions]);
if (useBuiltIns === "usage") {
if (corejs.major === 2) {
polyfillPlugins.push(
[babel7.pluginCoreJS2, pluginOptions],
[babel7.legacyBabelPolyfillPlugin, { usage: true }],
);
} else {
polyfillPlugins.push(
[pluginCoreJS3, pluginOptions],
[
babel7.legacyBabelPolyfillPlugin,
{ usage: true, deprecated: true },
],
);
}
if (regenerator) {
polyfillPlugins.push([
babel7.pluginRegenerator,
{ method: "usage-global", debug },
]);
}
} else {
if (corejs.major === 2) {
polyfillPlugins.push(
[babel7.legacyBabelPolyfillPlugin, { regenerator }],
[babel7.pluginCoreJS2, pluginOptions],
);
} else {
polyfillPlugins.push(
[pluginCoreJS3, pluginOptions],
[babel7.legacyBabelPolyfillPlugin, { deprecated: true }],
);
if (!regenerator) {
polyfillPlugins.push([
babel7.removeRegeneratorEntryPlugin,
pluginOptions,
]);
}
}
}
}
}
}
return polyfillPlugins;
};

if (!USE_ESM) {
// eslint-disable-next-line no-restricted-globals
exports.getPolyfillPlugins = getPolyfillPlugins;
}
return polyfillPlugins;
};
}

function getLocalTargets(
optionsTargets: Options["targets"],
Expand Down Expand Up @@ -388,17 +432,35 @@ option \`forceAllTransforms: true\` instead.
removeUnsupportedItems(pluginNames, api.version);
removeUnnecessaryItems(pluginNames, overlappingPlugins);

const polyfillPlugins = getPolyfillPlugins({
useBuiltIns,
corejs,
polyfillTargets: targets,
include: include.builtIns,
exclude: exclude.builtIns,
proposals,
shippedProposals,
regenerator: pluginNames.has("transform-regenerator"),
debug,
});
const polyfillPlugins = process.env.BABEL_8_BREAKING
? useBuiltIns
? [
[
pluginCoreJS3,
getCoreJSOptions({
useBuiltIns,
corejs,
polyfillTargets: targets,
include: include.builtIns,
exclude: exclude.builtIns,
proposals,
shippedProposals,
debug,
}),
],
]
: []
: getPolyfillPlugins({
useBuiltIns,
corejs,
polyfillTargets: targets,
include: include.builtIns,
exclude: exclude.builtIns,
proposals,
shippedProposals,
regenerator: pluginNames.has("transform-regenerator"),
debug,
});

const pluginUseBuiltIns = useBuiltIns !== false;
const plugins = Array.from(pluginNames)
Expand Down

0 comments on commit 0ba00c4

Please sign in to comment.