Skip to content

Commit

Permalink
[babel 8] Fix @babel/compat-data package.json (babel#16318)
Browse files Browse the repository at this point in the history
* [babel 8] Fix `@babel/compat-data` package.json

* Do not use Babel 7 compat data in Babel 8

* Fix bundling
  • Loading branch information
nicolo-ribaudo authored and liuxingbaoyu committed Mar 5, 2024
1 parent f36a6c8 commit 2d7c548
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 28 deletions.
15 changes: 15 additions & 0 deletions Gulpfile.mjs
Expand Up @@ -419,6 +419,21 @@ function buildRollup(packages, buildStandalone) {
"packages/babel-compat-data/scripts/data/legacy-plugin-aliases.js",
"packages/*/src/**/*.cjs",
],
ignore:
process.env.STRIP_BABEL_8_FLAG &&
bool(process.env.BABEL_8_BREAKING)
? [
// These require()s are all in babel-preset-env/src/polyfills/babel-7-plugins.cjs
// They are gated by a !process.env.BABEL_8_BREAKING check, but
// @rollup/plugin-commonjs extracts them to import statements outside of the
// check and thus they end up in the final bundle.
"babel-plugin-polyfill-corejs2",
"babel-plugin-polyfill-regenerator",
"./babel-polyfill.cjs",
"./regenerator.cjs",
"@babel/compat-data/corejs2-built-ins",
]
: [],
dynamicRequireTargets: [
// https://github.com/mathiasbynens/regexpu-core/blob/ffd8fff2e31f4597f6fdfee75d5ac1c5c8111ec3/rewrite-pattern.js#L48
resolveChain(
Expand Down
10 changes: 6 additions & 4 deletions packages/babel-compat-data/package.json
Expand Up @@ -15,10 +15,12 @@
"conditions": {
"BABEL_8_BREAKING": [
{
"./plugins": "./plugins.js",
"./native-modules": "./native-modules.js",
"./overlapping-plugins": "./overlapping-plugins.js",
"./plugin-bugfixes": "./plugin-bugfixes.js",
"exports": {
"./plugins": "./plugins.js",
"./native-modules": "./native-modules.js",
"./overlapping-plugins": "./overlapping-plugins.js",
"./plugin-bugfixes": "./plugin-bugfixes.js"
},
"engines": {
"node": "^16.20.0 || ^18.16.0 || >=20.0.0"
}
Expand Down
Expand Up @@ -17,7 +17,6 @@
"babel-plugin"
],
"dependencies": {
"@babel/compat-data": "workspace:^",
"@babel/helper-compilation-targets": "workspace:^",
"@babel/helper-plugin-utils": "workspace:^",
"@babel/plugin-syntax-object-rest-spread": "condition:BABEL_8_BREAKING ? : ^7.8.3",
Expand Down
@@ -0,0 +1,15 @@
export default {
"Object.assign": {
chrome: "49",
opera: "36",
edge: "13",
firefox: "36",
safari: "10",
node: "6",
deno: "1",
ios: "10",
samsung: "5",
opera_mobile: "36",
electron: "0.37",
},
};
Expand Up @@ -4,8 +4,8 @@ import type { PluginPass } from "@babel/core";
import type { NodePath, Scope } from "@babel/traverse";
import { convertFunctionParams } from "@babel/plugin-transform-parameters";
import { isRequired } from "@babel/helper-compilation-targets";
import compatData from "@babel/compat-data/corejs2-built-ins";
import shouldStoreRHSInTemporaryVariable from "./shouldStoreRHSInTemporaryVariable.ts";
import compatData from "./compat-data.ts";

const { isAssignmentPattern, isObjectProperty } = t;
// @babel/types <=7.3.3 counts FOO as referenced in var { x: FOO }.
Expand Down Expand Up @@ -33,7 +33,7 @@ export default declare((api, opts: Options) => {
);

const targets = api.targets();
const supportsObjectAssign = !isRequired("es6.object.assign", targets, {
const supportsObjectAssign = !isRequired("Object.assign", targets, {
compatData,
});

Expand Down
20 changes: 6 additions & 14 deletions packages/babel-preset-env/src/normalize-options.ts
@@ -1,5 +1,4 @@
import semver, { type SemVer } from "semver";
import corejs2Polyfills from "@babel/compat-data/corejs2-built-ins";
// @ts-expect-error Fixme: TS can not infer types from ../data/core-js-compat.js
// but we can't import core-js-compat/data.json because JSON imports do
// not work on Node 14
Expand All @@ -13,11 +12,8 @@ import {
} from "./options.ts";
import { OptionValidator } from "@babel/helper-validator-option";

const corejs2DefaultWebIncludes = [
"web.timers",
"web.immediate",
"web.dom.iterable",
];
// TODO(Babel 8): Remove this
import babel7 from "./polyfills/babel-7-plugins.cjs";

import type {
BuiltInsOption,
Expand Down Expand Up @@ -46,15 +42,11 @@ const getValidIncludesAndExcludes = (
const set = new Set(allPluginsList);
if (type === "exclude") modulePlugins.map(set.add, set);
if (corejs) {
if (process.env.BABEL_8_BREAKING) {
Object.keys(corejs3Polyfills).map(set.add, set);
if (!process.env.BABEL_8_BREAKING && corejs === 2) {
Object.keys(babel7.corejs2Polyfills).map(set.add, set);
set.add("web.timers").add("web.immediate").add("web.dom.iterable");
} else {
if (corejs === 2) {
Object.keys(corejs2Polyfills).map(set.add, set);
corejs2DefaultWebIncludes.map(set.add, set);
} else {
Object.keys(corejs3Polyfills).map(set.add, set);
}
Object.keys(corejs3Polyfills).map(set.add, set);
}
}
return Array.from(set);
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-preset-env/src/polyfills/babel-7-plugins.cjs
Expand Up @@ -10,5 +10,8 @@ if (!process.env.BABEL_8_BREAKING) {
},
legacyBabelPolyfillPlugin: { get: () => require("./babel-polyfill.cjs") },
removeRegeneratorEntryPlugin: { get: () => require("./regenerator.cjs") },
corejs2Polyfills: {
get: () => require("@babel/compat-data/corejs2-built-ins"),
},
});
}
Expand Up @@ -2,3 +2,4 @@ export const pluginCoreJS2: any;
export const pluginRegenerator: any;
export const legacyBabelPolyfillPlugin: any;
export const removeRegeneratorEntryPlugin: any;
export const corejs2Polyfills: any;
6 changes: 0 additions & 6 deletions tsconfig.json
Expand Up @@ -183,12 +183,6 @@
"@babel/compat-data/native-modules": [
"./packages/babel-compat-data/data/native-modules.json"
],
"@babel/compat-data/corejs2-built-ins": [
"./packages/babel-compat-data/data/corejs2-built-ins.json"
],
"@babel/compat-data/corejs3-shipped-proposals": [
"./packages/babel-compat-data/data/corejs3-shipped-proposals.json"
],
"@babel/compat-data/overlapping-plugins": [
"./packages/babel-compat-data/data/overlapping-plugins.json"
],
Expand Down
1 change: 0 additions & 1 deletion yarn.lock
Expand Up @@ -3171,7 +3171,6 @@ __metadata:
version: 0.0.0-use.local
resolution: "@babel/plugin-transform-object-rest-spread@workspace:packages/babel-plugin-transform-object-rest-spread"
dependencies:
"@babel/compat-data": "workspace:^"
"@babel/core": "workspace:^"
"@babel/helper-compilation-targets": "workspace:^"
"@babel/helper-plugin-test-runner": "workspace:^"
Expand Down

0 comments on commit 2d7c548

Please sign in to comment.