Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid dynamic require call in preset-env #15907

Merged
merged 3 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ module.exports = function (api) {
{ name: "IS_STANDALONE", value: env === "standalone" },
"flag-IS_STANDALONE",
],
[
pluginToggleBooleanFlag,
{
name: "USE_ESM_OR_STANDALONE",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can then use this in other packages, so that we don't have to manually check for both cases.

value: outputType === "module" || env === "standalone",
},
"flag-USE_ESM_OR_STANDALONE",
],

[
pluginToggleBooleanFlag,
Expand Down
91 changes: 61 additions & 30 deletions packages/babel-preset-env/src/available-plugins.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint sort-keys: "error" */

declare const USE_ESM: boolean;
declare const USE_ESM_OR_STANDALONE: boolean;

import syntaxImportAssertions from "@babel/plugin-syntax-import-assertions";
import syntaxImportAttributes from "@babel/plugin-syntax-import-attributes";
Expand Down Expand Up @@ -134,6 +134,8 @@ const availablePlugins = {
export const minVersions = {};

if (!process.env.BABEL_8_BREAKING) {
/* eslint-disable no-restricted-globals */

Object.assign(minVersions, {
"bugfix/transform-safari-id-destructuring-collision-in-function-expression":
"7.16.0",
Expand All @@ -142,39 +144,68 @@ if (!process.env.BABEL_8_BREAKING) {
"transform-private-property-in-object": "7.10.0",
});

const emptyPlugin = () => ({});
// We cannot use the require call in ESM and when bundling.
// Babel standalone uses a modern parser, so just include a noop plugin.
// Use `bind` so that it's not detected as a duplicate plugin when using it.

const babel7SyntaxPlugin = (name: string) => {
// We cannot use the require call in ESM and when bundling.
// Babel standalone uses a modern parser, so just include a noop plugin.
// Use `bind` so that it's not detected as a duplicate plugin when using it.
// @ts-expect-error key not recognized in the object
availablePlugins[`syntax-${name}`] = USE_ESM
? () => emptyPlugin.bind(null)
: IS_STANDALONE
? () => emptyPlugin.bind(null)
: // eslint-disable-next-line no-restricted-globals
() => require(`@babel/plugin-syntax-${name}`);
};
// This is a factory to create a function that returns a no-op plugn
const e = () => () => () => ({});

babel7SyntaxPlugin("async-generators");
babel7SyntaxPlugin("class-properties");
babel7SyntaxPlugin("class-static-block");
babel7SyntaxPlugin("dynamic-import");
babel7SyntaxPlugin("export-namespace-from");
babel7SyntaxPlugin("import-meta");
babel7SyntaxPlugin("json-strings");
babel7SyntaxPlugin("logical-assignment-operators");
babel7SyntaxPlugin("nullish-coalescing-operator");
babel7SyntaxPlugin("numeric-separator");
babel7SyntaxPlugin("object-rest-spread");
babel7SyntaxPlugin("optional-catch-binding");
babel7SyntaxPlugin("optional-chaining");
babel7SyntaxPlugin("private-property-in-object");
babel7SyntaxPlugin("top-level-await");
Object.assign(availablePlugins, {
"syntax-async-generators": USE_ESM_OR_STANDALONE
? e()
: () => require("@babel/plugin-syntax-async-generators"),
"syntax-class-properties": USE_ESM_OR_STANDALONE
? e()
: () => require("@babel/plugin-syntax-class-properties"),
"syntax-class-static-block": USE_ESM_OR_STANDALONE
? e()
: () => require("@babel/plugin-syntax-class-static-block"),
"syntax-dynamic-import": USE_ESM_OR_STANDALONE
? e()
: () => require("@babel/plugin-syntax-dynamic-import"),
"syntax-export-namespace-from": USE_ESM_OR_STANDALONE
? e()
: () => require("@babel/plugin-syntax-export-namespace-from"),
"syntax-import-meta": USE_ESM_OR_STANDALONE
? e()
: () => require("@babel/plugin-syntax-import-meta"),
"syntax-json-strings": USE_ESM_OR_STANDALONE
? e()
: () => require("@babel/plugin-syntax-json-strings"),
"syntax-logical-assignment-operators": USE_ESM_OR_STANDALONE
? e()
: () => require("@babel/plugin-syntax-logical-assignment-operators"),
"syntax-nullish-coalescing-operator": USE_ESM_OR_STANDALONE
? e()
: () => require("@babel/plugin-syntax-nullish-coalescing-operator"),
"syntax-numeric-separator": USE_ESM_OR_STANDALONE
? e()
: () => require("@babel/plugin-syntax-numeric-separator"),
"syntax-object-rest-spread": USE_ESM_OR_STANDALONE
? e()
: () => require("@babel/plugin-syntax-object-rest-spread"),
"syntax-optional-catch-binding": USE_ESM_OR_STANDALONE
? e()
: () => require("@babel/plugin-syntax-optional-catch-binding"),
"syntax-optional-chaining": USE_ESM_OR_STANDALONE
? e()
: () => require("@babel/plugin-syntax-optional-chaining"),
"syntax-private-property-in-object": USE_ESM_OR_STANDALONE
? e()
: () => require("@babel/plugin-syntax-private-property-in-object"),
"syntax-top-level-await": USE_ESM_OR_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) babel7SyntaxPlugin("unicode-sets-regex");
if (!USE_ESM) {
// @ts-expect-error unknown key
availablePlugins["unicode-sets-regex"] = USE_ESM_OR_STANDALONE
? e()
: () => require("@babel/plugin-syntax-unicode-sets-regex");
}
}