Skip to content

Commit

Permalink
[babel 8] Remove corejs2 and regenerator from preset-env
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Aug 3, 2023
1 parent 565d7be commit 69b7499
Show file tree
Hide file tree
Showing 389 changed files with 381 additions and 7,300 deletions.
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.5",
"babel-plugin-polyfill-corejs2": "condition:BABEL_8_BREAKING ? : ^0.4.5",
"babel-plugin-polyfill-corejs3": "^0.8.3",
"babel-plugin-polyfill-regenerator": "^0.5.2",
"babel-plugin-polyfill-regenerator": "condition:BABEL_8_BREAKING ? : ^0.5.2",
"core-js-compat": "^3.31.0",
"semver": "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.1"
},
Expand Down
83 changes: 47 additions & 36 deletions packages/babel-preset-env/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ import {
overlappingPlugins,
} from "./plugins-compat-data";

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

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 {
pluginCoreJS2,
pluginRegenerator,
legacyBabelPolyfillPlugin,
removeRegeneratorEntryPlugin,
} from "./polyfills/babel-7-plugins.cjs";

import getTargets, {
prettifyTargets,
Expand Down Expand Up @@ -170,6 +172,8 @@ export const getModulesPluginNames = ({
return modulesPluginNames;
};

// TODO(Babel 8): Given that there a single polyfill plugin, we do not need this
// method anymore. Cleanup, and remove it from the public API.
export const getPolyfillPlugins = ({
useBuiltIns,
corejs,
Expand Down Expand Up @@ -208,37 +212,44 @@ export const getPolyfillPlugins = ({
};

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 },
]);
}
if (process.env.BABEL_8_BREAKING) {
polyfillPlugins.push([pluginCoreJS3, pluginOptions]);
} else {
if (corejs.major === 2) {
polyfillPlugins.push(
[legacyBabelPolyfillPlugin, { regenerator }],
[pluginCoreJS2, pluginOptions],
);
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 {
polyfillPlugins.push(
[pluginCoreJS3, pluginOptions],
[legacyBabelPolyfillPlugin, { deprecated: true }],
);
if (!regenerator) {
polyfillPlugins.push([removeRegeneratorEntryPlugin, pluginOptions]);
if (corejs.major === 2) {
polyfillPlugins.push(
[legacyBabelPolyfillPlugin, { regenerator }],
[pluginCoreJS2, pluginOptions],
);
} else {
polyfillPlugins.push(
[pluginCoreJS3, pluginOptions],
[legacyBabelPolyfillPlugin, { deprecated: true }],
);
if (!regenerator) {
polyfillPlugins.push([
removeRegeneratorEntryPlugin,
pluginOptions,
]);
}
}
}
}
Expand Down
38 changes: 22 additions & 16 deletions packages/babel-preset-env/src/normalize-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,28 @@ export function normalizeCoreJSOption(
let rawVersion;

if (useBuiltIns && corejs === undefined) {
rawVersion = 2;
console.warn(
"\nWARNING (@babel/preset-env): We noticed you're using the `useBuiltIns` option without declaring a " +
"core-js version. Currently, we assume version 2.x when no version " +
"is passed. Since this default version will likely change in future " +
"versions of Babel, we recommend explicitly setting the core-js version " +
"you are using via the `corejs` option.\n" +
"\nYou should also be sure that the version you pass to the `corejs` " +
"option matches the version specified in your `package.json`'s " +
"`dependencies` section. If it doesn't, you need to run one of the " +
"following commands:\n\n" +
" npm install --save core-js@2 npm install --save core-js@3\n" +
" yarn add core-js@2 yarn add core-js@3\n\n" +
"More info about useBuiltIns: https://babeljs.io/docs/en/babel-preset-env#usebuiltins\n" +
"More info about core-js: https://babeljs.io/docs/en/babel-preset-env#corejs",
);
if (process.env.BABEL_8_BREAKING) {
throw new Error(
"When using the `useBuiltIns` option you must specify" +
' the code-js version you are using, such as `"corejs": "3.32.0"`.',
);
} else {
console.warn(
"\nWARNING (@babel/preset-env): We noticed you're using the `useBuiltIns` option without declaring a " +
`core-js version. Currently, we assume version 2.x when no version ` +
"is passed. Since this default version will likely change in future " +
"versions of Babel, we recommend explicitly setting the core-js version " +
"you are using via the `corejs` option.\n" +
"\nYou should also be sure that the version you pass to the `corejs` " +
"option matches the version specified in your `package.json`'s " +
"`dependencies` section. If it doesn't, you need to run one of the " +
"following commands:\n\n" +
" npm install --save core-js@2 npm install --save core-js@3\n" +
" yarn add core-js@2 yarn add core-js@3\n\n" +
"More info about useBuiltIns: https://babeljs.io/docs/en/babel-preset-env#usebuiltins\n" +
"More info about core-js: https://babeljs.io/docs/en/babel-preset-env#corejs",
);
}
} else if (typeof corejs === "object" && corejs !== null) {
rawVersion = corejs.version;
proposals = Boolean(corejs.proposals);
Expand Down
9 changes: 9 additions & 0 deletions packages/babel-preset-env/src/polyfills/babel-7-plugins.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// TODO(Babel 8): Remove this file

if (!process.env.BABEL_8_BREAKING) {
exports.pluginCoreJS2 = require("babel-plugin-polyfill-corejs2").default;
exports.pluginRegenerator =
require("babel-plugin-polyfill-regenerator").default;
exports.legacyBabelPolyfillPlugin = require("./babel-polyfill.cjs");
exports.removeRegeneratorEntryPlugin = require("./regenerator.cjs");
}
4 changes: 4 additions & 0 deletions packages/babel-preset-env/src/polyfills/babel-7-plugins.d.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const pluginCoreJS2: any;
export const pluginRegenerator: any;
export const legacyBabelPolyfillPlugin: any;
export const removeRegeneratorEntryPlugin: any;
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { getImportSource, getRequireSource, isPolyfillSource } from "./utils";
// 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",
);
}

import type { NodePath } from "@babel/traverse";
import type * as t from "@babel/types";
const {
getImportSource,
getRequireSource,
isPolyfillSource,
} = require("./utils.cjs");

const BABEL_POLYFILL_DEPRECATION = `
\`@babel/polyfill\` is deprecated. Please, use required parts of \`core-js\`
Expand All @@ -11,14 +19,11 @@ const NO_DIRECT_POLYFILL_IMPORT = `
When setting \`useBuiltIns: 'usage'\`, polyfills are automatically imported when needed.
Please remove the direct import of \`SPECIFIER\` or use \`useBuiltIns: 'entry'\` instead.`;

export default function (
{ template }: any,
{ regenerator, deprecated, usage }: any,
) {
module.exports = function ({ template }, { regenerator, deprecated, usage }) {
return {
name: "preset-env/replace-babel-polyfill",
visitor: {
ImportDeclaration(path: NodePath<t.ImportDeclaration>) {
ImportDeclaration(path) {
const src = getImportSource(path);
if (usage && isPolyfillSource(src)) {
console.warn(NO_DIRECT_POLYFILL_IMPORT.replace("SPECIFIER", src));
Expand All @@ -38,7 +43,7 @@ export default function (
}
}
},
Program(path: NodePath<t.Program>) {
Program(path) {
path.get("body").forEach(bodyPath => {
const src = getRequireSource(bodyPath);
if (usage && isPolyfillSource(src)) {
Expand All @@ -62,4 +67,4 @@ export default function (
},
},
};
}
};
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { getImportSource, getRequireSource } from "./utils";
import type { Visitor } from "@babel/traverse";
import type { PluginObject, PluginPass } from "@babel/core";
import type { Options } from "../types";
// 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",
);
}

function isRegeneratorSource(source: string) {
const { getImportSource, getRequireSource } = require("./utils.cjs");

function isRegeneratorSource(source) {
return (
source === "regenerator-runtime/runtime" ||
source === "regenerator-runtime/runtime.js"
);
}

type State = {
regeneratorImportExcluded: boolean;
};

export default function (): PluginObject<State & PluginPass<Options>> {
const visitor: Visitor<State & PluginPass> = {
module.exports = function () {
const visitor = {
ImportDeclaration(path) {
if (isRegeneratorSource(getImportSource(path))) {
this.regeneratorImportExcluded = true;
Expand Down Expand Up @@ -51,4 +51,4 @@ export default function (): PluginObject<State & PluginPass<Options>> {
}
},
};
}
};
28 changes: 28 additions & 0 deletions packages/babel-preset-env/src/polyfills/utils.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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",
);
}

exports.getImportSource = function ({ node }) {
if (node.specifiers.length === 0) return node.source.value;
};

exports.getRequireSource = function ({ node }) {
if (node.type !== "ExpressionStatement") return;
const { expression } = node;
if (
expression.type === "CallExpression" &&
expression.callee.type === "Identifier" &&
expression.callee.name === "require" &&
expression.arguments.length === 1 &&
expression.arguments[0].type === "StringLiteral"
) {
return expression.arguments[0].value;
}
};

exports.isPolyfillSource = function (source) {
return source === "@babel/polyfill" || source === "core-js";
};
30 changes: 0 additions & 30 deletions packages/babel-preset-env/src/polyfills/utils.ts

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 69b7499

Please sign in to comment.