Skip to content

Commit

Permalink
Do not load unnecessary Babel 7 syntax plugins in Babel 8 (#16406)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Apr 3, 2024
1 parent fa9c005 commit 5a4fb2d
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
"presets": [["env", { "targets": { "browsers": "ie 6" } }]],
"assumptions": {
"setPublicClassFields": true
},
"SKIP_babel7plugins_babel8core": "This test uses preset-env"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
}
}
]
],
"SKIP_babel7plugins_babel8core": "This test uses preset-env"
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"plugins": ["transform-class-properties"],
"presets": [["env", { "targets": { "browsers": "ie 6" } }]],
"SKIP_babel7plugins_babel8core": "This test uses preset-env"
"presets": [["env", { "targets": { "browsers": "ie 6" } }]]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"presets": [["env", { "targets": { "browsers": "ie 6" } }]],
"plugins": ["transform-class-properties"],
"SKIP_babel7plugins_babel8core": "This test uses preset-env"
"plugins": ["transform-class-properties"]
}
14 changes: 11 additions & 3 deletions packages/babel-preset-env/src/available-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ const availablePlugins = {
};

export const minVersions = {};
// TODO(Babel 8): Remove this
export let legacyBabel7SyntaxPlugins: Set<string>;

if (!process.env.BABEL_8_BREAKING) {
/* eslint-disable no-restricted-globals */
Expand All @@ -156,7 +158,7 @@ if (!process.env.BABEL_8_BREAKING) {
// This is a factory to create a function that returns a no-op plugn
const e = () => () => () => ({});

Object.assign(availablePlugins, {
const legacyBabel7SyntaxPluginsLoaders = {
"syntax-async-generators":
USE_ESM || IS_STANDALONE
? e()
Expand Down Expand Up @@ -217,15 +219,21 @@ if (!process.env.BABEL_8_BREAKING) {
USE_ESM || IS_STANDALONE
? e()
: () => require("@babel/plugin-syntax-top-level-await"),
});
};

// This is a CJS plugin that depends on a package from the monorepo, so it
// breaks using ESM. Given that ESM builds are new enough to have this
// syntax enabled by default, we can safely skip enabling it.
if (!USE_ESM) {
// @ts-expect-error unknown key
availablePlugins["unicode-sets-regex"] = IS_STANDALONE
legacyBabel7SyntaxPluginsLoaders["unicode-sets-regex"] = IS_STANDALONE
? e()
: () => require("@babel/plugin-syntax-unicode-sets-regex");
}

Object.assign(availablePlugins, legacyBabel7SyntaxPluginsLoaders);

legacyBabel7SyntaxPlugins = new Set(
Object.keys(legacyBabel7SyntaxPluginsLoaders),
);
}
8 changes: 7 additions & 1 deletion packages/babel-preset-env/src/filter-items.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import semver from "semver";
import { minVersions } from "./available-plugins.ts";
import { minVersions, legacyBabel7SyntaxPlugins } from "./available-plugins.ts";

export function addProposalSyntaxPlugins(
items: Set<string>,
Expand Down Expand Up @@ -31,6 +31,12 @@ export function removeUnsupportedItems(
)
) {
items.delete(item);
} else if (
!process.env.BABEL_8_BREAKING &&
babelVersion[0] === "8" &&
legacyBabel7SyntaxPlugins.has(item)
) {
items.delete(item);
}
});
}
9 changes: 7 additions & 2 deletions packages/babel-preset-env/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export const transformIncludesAndExcludes = (opts: Array<string>): any => {
function getSpecialModulesPluginNames(
modules: Exclude<ModuleOption, "auto">,
shouldTransformDynamicImport: boolean,
babelVersion: string,
) {
const modulesPluginNames = [];
if (modules) {
Expand All @@ -134,7 +135,7 @@ function getSpecialModulesPluginNames(
}
}

if (!process.env.BABEL_8_BREAKING) {
if (!process.env.BABEL_8_BREAKING && babelVersion[0] !== "8") {
// Enable module-related syntax plugins for older Babel versions
if (!shouldTransformDynamicImport) {
modulesPluginNames.push("syntax-dynamic-import");
Expand Down Expand Up @@ -419,7 +420,11 @@ option \`forceAllTransforms: true\` instead.
include.plugins,
exclude.plugins,
transformTargets,
getSpecialModulesPluginNames(modules, shouldTransformDynamicImport),
getSpecialModulesPluginNames(
modules,
shouldTransformDynamicImport,
api.version,
),
process.env.BABEL_8_BREAKING || !loose
? undefined
: ["transform-typeof-symbol"],
Expand Down

0 comments on commit 5a4fb2d

Please sign in to comment.