Skip to content

Commit f9d1adf

Browse files
Avoid dynamic require call in preset-env (#15907)
Co-authored-by: liuxingbaoyu <[email protected]>
1 parent 235a7ac commit f9d1adf

2 files changed

Lines changed: 69 additions & 30 deletions

File tree

babel.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,14 @@ module.exports = function (api) {
244244
{ name: "IS_STANDALONE", value: env === "standalone" },
245245
"flag-IS_STANDALONE",
246246
],
247+
[
248+
pluginToggleBooleanFlag,
249+
{
250+
name: "USE_ESM_OR_STANDALONE",
251+
value: outputType === "module" || env === "standalone",
252+
},
253+
"flag-USE_ESM_OR_STANDALONE",
254+
],
247255

248256
[
249257
pluginToggleBooleanFlag,

packages/babel-preset-env/src/available-plugins.ts

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint sort-keys: "error" */
22

3-
declare const USE_ESM: boolean;
3+
declare const USE_ESM_OR_STANDALONE: boolean;
44

55
import syntaxImportAssertions from "@babel/plugin-syntax-import-assertions";
66
import syntaxImportAttributes from "@babel/plugin-syntax-import-attributes";
@@ -134,6 +134,8 @@ const availablePlugins = {
134134
export const minVersions = {};
135135

136136
if (!process.env.BABEL_8_BREAKING) {
137+
/* eslint-disable no-restricted-globals */
138+
137139
Object.assign(minVersions, {
138140
"bugfix/transform-safari-id-destructuring-collision-in-function-expression":
139141
"7.16.0",
@@ -142,39 +144,68 @@ if (!process.env.BABEL_8_BREAKING) {
142144
"transform-private-property-in-object": "7.10.0",
143145
});
144146

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

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

160-
babel7SyntaxPlugin("async-generators");
161-
babel7SyntaxPlugin("class-properties");
162-
babel7SyntaxPlugin("class-static-block");
163-
babel7SyntaxPlugin("dynamic-import");
164-
babel7SyntaxPlugin("export-namespace-from");
165-
babel7SyntaxPlugin("import-meta");
166-
babel7SyntaxPlugin("json-strings");
167-
babel7SyntaxPlugin("logical-assignment-operators");
168-
babel7SyntaxPlugin("nullish-coalescing-operator");
169-
babel7SyntaxPlugin("numeric-separator");
170-
babel7SyntaxPlugin("object-rest-spread");
171-
babel7SyntaxPlugin("optional-catch-binding");
172-
babel7SyntaxPlugin("optional-chaining");
173-
babel7SyntaxPlugin("private-property-in-object");
174-
babel7SyntaxPlugin("top-level-await");
154+
Object.assign(availablePlugins, {
155+
"syntax-async-generators": USE_ESM_OR_STANDALONE
156+
? e()
157+
: () => require("@babel/plugin-syntax-async-generators"),
158+
"syntax-class-properties": USE_ESM_OR_STANDALONE
159+
? e()
160+
: () => require("@babel/plugin-syntax-class-properties"),
161+
"syntax-class-static-block": USE_ESM_OR_STANDALONE
162+
? e()
163+
: () => require("@babel/plugin-syntax-class-static-block"),
164+
"syntax-dynamic-import": USE_ESM_OR_STANDALONE
165+
? e()
166+
: () => require("@babel/plugin-syntax-dynamic-import"),
167+
"syntax-export-namespace-from": USE_ESM_OR_STANDALONE
168+
? e()
169+
: () => require("@babel/plugin-syntax-export-namespace-from"),
170+
"syntax-import-meta": USE_ESM_OR_STANDALONE
171+
? e()
172+
: () => require("@babel/plugin-syntax-import-meta"),
173+
"syntax-json-strings": USE_ESM_OR_STANDALONE
174+
? e()
175+
: () => require("@babel/plugin-syntax-json-strings"),
176+
"syntax-logical-assignment-operators": USE_ESM_OR_STANDALONE
177+
? e()
178+
: () => require("@babel/plugin-syntax-logical-assignment-operators"),
179+
"syntax-nullish-coalescing-operator": USE_ESM_OR_STANDALONE
180+
? e()
181+
: () => require("@babel/plugin-syntax-nullish-coalescing-operator"),
182+
"syntax-numeric-separator": USE_ESM_OR_STANDALONE
183+
? e()
184+
: () => require("@babel/plugin-syntax-numeric-separator"),
185+
"syntax-object-rest-spread": USE_ESM_OR_STANDALONE
186+
? e()
187+
: () => require("@babel/plugin-syntax-object-rest-spread"),
188+
"syntax-optional-catch-binding": USE_ESM_OR_STANDALONE
189+
? e()
190+
: () => require("@babel/plugin-syntax-optional-catch-binding"),
191+
"syntax-optional-chaining": USE_ESM_OR_STANDALONE
192+
? e()
193+
: () => require("@babel/plugin-syntax-optional-chaining"),
194+
"syntax-private-property-in-object": USE_ESM_OR_STANDALONE
195+
? e()
196+
: () => require("@babel/plugin-syntax-private-property-in-object"),
197+
"syntax-top-level-await": USE_ESM_OR_STANDALONE
198+
? e()
199+
: () => require("@babel/plugin-syntax-top-level-await"),
200+
});
175201

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

0 commit comments

Comments
 (0)