Skip to content

Commit c46f000

Browse files
Refactor handling of modules plugins in preset-env (#15988)
1 parent 637ee78 commit c46f000

File tree

25 files changed

+125
-104
lines changed

25 files changed

+125
-104
lines changed

packages/babel-core/src/config/helpers/config-api.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ type EnvFunction = {
2020
(envVars: Array<string>): boolean;
2121
};
2222

23-
type CallerFactory = (
24-
extractor: (callerMetadata: CallerMetadata | undefined) => unknown,
25-
) => SimpleType;
23+
type CallerFactory = {
24+
<T extends SimpleType>(
25+
extractor: (callerMetadata: CallerMetadata | undefined) => T,
26+
): T;
27+
(
28+
extractor: (callerMetadata: CallerMetadata | undefined) => unknown,
29+
): SimpleType;
30+
};
2631
type TargetsFunction = () => Targets;
2732
type AssumptionFunction = (name: AssumptionName) => boolean | undefined;
2833

packages/babel-preset-env/src/index.ts

+95-57
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ import type { Targets, InputTargets } from "@babel/helper-compilation-targets";
3737
import availablePlugins from "./available-plugins.ts";
3838
import { declarePreset } from "@babel/helper-plugin-utils";
3939

40-
type ModuleTransformationsType =
41-
typeof import("./module-transformations").default;
4240
import type { BuiltInsOption, ModuleOption, Options } from "./types.ts";
4341

4442
// TODO: Remove in Babel 8
@@ -117,55 +115,37 @@ export const transformIncludesAndExcludes = (opts: Array<string>): any => {
117115
);
118116
};
119117

120-
export const getModulesPluginNames = ({
121-
modules,
122-
transformations,
123-
shouldTransformESM,
124-
shouldTransformDynamicImport,
125-
shouldTransformExportNamespaceFrom,
126-
}: {
127-
modules: ModuleOption;
128-
transformations: ModuleTransformationsType;
129-
shouldTransformESM: boolean;
130-
shouldTransformDynamicImport: boolean;
131-
shouldTransformExportNamespaceFrom: boolean;
132-
}) => {
118+
function getSpecialModulesPluginNames(
119+
modules: Exclude<ModuleOption, "auto">,
120+
shouldTransformDynamicImport: boolean,
121+
) {
133122
const modulesPluginNames = [];
134-
if (modules !== false && transformations[modules]) {
135-
if (shouldTransformESM) {
136-
modulesPluginNames.push(transformations[modules]);
137-
}
138-
139-
if (shouldTransformDynamicImport) {
140-
if (shouldTransformESM && modules !== "umd") {
141-
modulesPluginNames.push("transform-dynamic-import");
142-
} else {
143-
console.warn(
144-
"Dynamic import can only be transformed when transforming ES" +
145-
" modules to AMD, CommonJS or SystemJS.",
146-
);
147-
}
148-
}
123+
if (modules) {
124+
modulesPluginNames.push(moduleTransformations[modules]);
149125
}
150126

151-
if (shouldTransformExportNamespaceFrom) {
152-
modulesPluginNames.push("transform-export-namespace-from");
127+
if (shouldTransformDynamicImport) {
128+
if (modules && modules !== "umd") {
129+
modulesPluginNames.push("transform-dynamic-import");
130+
} else {
131+
console.warn(
132+
"Dynamic import can only be transformed when transforming ES" +
133+
" modules to AMD, CommonJS or SystemJS.",
134+
);
135+
}
153136
}
154137

155138
if (!process.env.BABEL_8_BREAKING) {
156139
// Enable module-related syntax plugins for older Babel versions
157140
if (!shouldTransformDynamicImport) {
158141
modulesPluginNames.push("syntax-dynamic-import");
159142
}
160-
if (!shouldTransformExportNamespaceFrom) {
161-
modulesPluginNames.push("syntax-export-namespace-from");
162-
}
163143
modulesPluginNames.push("syntax-top-level-await");
164144
modulesPluginNames.push("syntax-import-meta");
165145
}
166146

167147
return modulesPluginNames;
168-
};
148+
}
169149

170150
const getCoreJSOptions = ({
171151
useBuiltIns,
@@ -312,16 +292,19 @@ function getLocalTargets(
312292
}
313293

314294
function supportsStaticESM(caller: CallerMetadata | undefined) {
295+
// TODO(Babel 8): Fallback to true
315296
// @ts-expect-error supportsStaticESM is not defined in CallerMetadata
316297
return !!caller?.supportsStaticESM;
317298
}
318299

319300
function supportsDynamicImport(caller: CallerMetadata | undefined) {
301+
// TODO(Babel 8): Fallback to true
320302
// @ts-expect-error supportsDynamicImport is not defined in CallerMetadata
321303
return !!caller?.supportsDynamicImport;
322304
}
323305

324306
function supportsExportNamespaceFrom(caller: CallerMetadata | undefined) {
307+
// TODO(Babel 8): Fallback to null
325308
// @ts-expect-error supportsExportNamespaceFrom is not defined in CallerMetadata
326309
return !!caller?.supportsExportNamespaceFrom;
327310
}
@@ -344,7 +327,7 @@ export default declarePreset((api, opts: Options) => {
344327
ignoreBrowserslistConfig,
345328
include: optionsInclude,
346329
loose,
347-
modules,
330+
modules: optionsModules,
348331
shippedProposals,
349332
spec,
350333
targets: optionsTargets,
@@ -402,31 +385,34 @@ option \`forceAllTransforms: true\` instead.
402385
const exclude = transformIncludesAndExcludes(optionsExclude);
403386

404387
const compatData = getPluginList(shippedProposals, bugfixes);
405-
const shouldSkipExportNamespaceFrom =
406-
(modules === "auto" && api.caller?.(supportsExportNamespaceFrom)) ||
407-
(modules === false &&
408-
!isRequired("transform-export-namespace-from", transformTargets, {
409-
compatData,
410-
includes: include.plugins,
411-
excludes: exclude.plugins,
412-
}));
413-
const modulesPluginNames = getModulesPluginNames({
414-
modules,
415-
transformations: moduleTransformations,
416-
// TODO: Remove the 'api.caller' check eventually. Just here to prevent
417-
// unnecessary breakage in the short term for users on older betas/RCs.
418-
shouldTransformESM: modules !== "auto" || !api.caller?.(supportsStaticESM),
419-
shouldTransformDynamicImport:
420-
modules !== "auto" || !api.caller?.(supportsDynamicImport),
421-
shouldTransformExportNamespaceFrom: !shouldSkipExportNamespaceFrom,
422-
});
388+
const modules =
389+
optionsModules === "auto"
390+
? api.caller(supportsStaticESM)
391+
? false
392+
: "commonjs"
393+
: optionsModules;
394+
const shouldTransformDynamicImport =
395+
optionsModules === "auto" ? !api.caller(supportsDynamicImport) : !!modules;
396+
397+
// If the caller does not support export-namespace-from, we forcefully add
398+
// the plugin to `includes`.
399+
// TODO(Babel 8): stop doing this, similarly to how we don't do this for any
400+
// other plugin. We can consider adding bundlers as targets in the future,
401+
// but we should not have a one-off special case for this plugin.
402+
if (
403+
optionsModules === "auto" &&
404+
!api.caller(supportsExportNamespaceFrom) &&
405+
!exclude.plugins.has("transform-export-namespace-from")
406+
) {
407+
include.plugins.add("transform-export-namespace-from");
408+
}
423409

424410
const pluginNames = filterItems(
425411
compatData,
426412
include.plugins,
427413
exclude.plugins,
428414
transformTargets,
429-
modulesPluginNames,
415+
getSpecialModulesPluginNames(modules, shouldTransformDynamicImport),
430416
getOptionSpecificExcludesFor({ loose }),
431417
pluginSyntaxMap,
432418
);
@@ -500,7 +486,7 @@ option \`forceAllTransforms: true\` instead.
500486
console.log("@babel/preset-env: `DEBUG` option");
501487
console.log("\nUsing targets:");
502488
console.log(JSON.stringify(prettifyTargets(targets), null, 2));
503-
console.log(`\nUsing modules transform: ${modules.toString()}`);
489+
console.log(`\nUsing modules transform: ${optionsModules.toString()}`);
504490
console.log("\nUsing plugins:");
505491
pluginNames.forEach(pluginName => {
506492
logPlugin(pluginName, targets, compatData);
@@ -515,3 +501,55 @@ option \`forceAllTransforms: true\` instead.
515501

516502
return { plugins };
517503
});
504+
505+
// TODO(Babel 8): This is only here for backward compatibility. Remove it.
506+
export { getModulesPluginNamesBackwardCompat as getModulesPluginNames };
507+
const getModulesPluginNamesBackwardCompat = ({
508+
modules,
509+
transformations,
510+
shouldTransformESM,
511+
shouldTransformDynamicImport,
512+
shouldTransformExportNamespaceFrom,
513+
}: {
514+
modules: ModuleOption;
515+
transformations: typeof import("./module-transformations").default;
516+
shouldTransformESM: boolean;
517+
shouldTransformDynamicImport: boolean;
518+
shouldTransformExportNamespaceFrom: boolean;
519+
}) => {
520+
const modulesPluginNames = [];
521+
if (modules !== false && transformations[modules]) {
522+
if (shouldTransformESM) {
523+
modulesPluginNames.push(transformations[modules]);
524+
}
525+
526+
if (shouldTransformDynamicImport) {
527+
if (shouldTransformESM && modules !== "umd") {
528+
modulesPluginNames.push("transform-dynamic-import");
529+
} else {
530+
console.warn(
531+
"Dynamic import can only be transformed when transforming ES" +
532+
" modules to AMD, CommonJS or SystemJS.",
533+
);
534+
}
535+
}
536+
}
537+
538+
if (shouldTransformExportNamespaceFrom) {
539+
modulesPluginNames.push("transform-export-namespace-from");
540+
}
541+
542+
if (!process.env.BABEL_8_BREAKING) {
543+
// Enable module-related syntax plugins for older Babel versions
544+
if (!shouldTransformDynamicImport) {
545+
modulesPluginNames.push("syntax-dynamic-import");
546+
}
547+
if (!shouldTransformExportNamespaceFrom) {
548+
modulesPluginNames.push("syntax-export-namespace-from");
549+
}
550+
modulesPluginNames.push("syntax-top-level-await");
551+
modulesPluginNames.push("syntax-import-meta");
552+
}
553+
554+
return modulesPluginNames;
555+
};

packages/babel-preset-env/src/module-transformations.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
type AvailablePlugins = typeof import("./available-plugins").default;
22

33
export default {
4-
auto: "transform-modules-commonjs",
54
amd: "transform-modules-amd",
65
commonjs: "transform-modules-commonjs",
76
cjs: "transform-modules-commonjs",

packages/babel-preset-env/src/shipped-proposals.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const pluginSyntaxObject = process.env.BABEL_8_BREAKING
1919
"transform-async-generator-functions": "syntax-async-generators",
2020
"transform-class-properties": "syntax-class-properties",
2121
"transform-class-static-block": "syntax-class-static-block",
22+
"transform-export-namespace-from": "syntax-export-namespace-from",
2223
"transform-json-strings": "syntax-json-strings",
2324
"transform-nullish-coalescing-operator":
2425
"syntax-nullish-coalescing-operator",

packages/babel-preset-env/test/fixtures/bugfixes/safari-id-destructuring-collision-in-function-expression-safari-15-no-bugfixes/stdout.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ Using plugins:
1919
syntax-optional-catch-binding
2020
syntax-async-generators
2121
syntax-object-rest-spread
22+
transform-export-namespace-from { }
2223
bugfix/transform-safari-id-destructuring-collision-in-function-expression { safari < 16.3 }
2324
transform-modules-commonjs
2425
transform-dynamic-import
25-
transform-export-namespace-from { }
2626
syntax-top-level-await
2727
syntax-import-meta
2828

packages/babel-preset-env/test/fixtures/bugfixes/safari-id-destructuring-collision-in-function-expression-safari-15/stdout.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ Using plugins:
2020
transform-parameters { safari < 16.3 }
2121
syntax-async-generators
2222
syntax-object-rest-spread
23+
transform-export-namespace-from { }
2324
transform-modules-commonjs
2425
transform-dynamic-import
25-
transform-export-namespace-from { }
2626
syntax-top-level-await
2727
syntax-import-meta
2828

packages/babel-preset-env/test/fixtures/bugfixes/v8-spread-parameters-in-optional-chaining-chrome-89-no-bugfixes/stdout.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ Using plugins:
1919
syntax-optional-catch-binding
2020
syntax-async-generators
2121
syntax-object-rest-spread
22+
transform-export-namespace-from { }
2223
transform-modules-commonjs
2324
transform-dynamic-import
24-
transform-export-namespace-from { }
2525
syntax-top-level-await
2626
syntax-import-meta
2727

packages/babel-preset-env/test/fixtures/bugfixes/v8-spread-parameters-in-optional-chaining-chrome-89/stdout.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ Using plugins:
1919
syntax-optional-catch-binding
2020
syntax-async-generators
2121
syntax-object-rest-spread
22+
transform-export-namespace-from { }
2223
bugfix/transform-v8-spread-parameters-in-optional-chaining { chrome < 91 }
2324
transform-modules-commonjs
2425
transform-dynamic-import
25-
transform-export-namespace-from { }
2626
syntax-top-level-await
2727
syntax-import-meta
2828

packages/babel-preset-env/test/fixtures/corejs2-babel-7/usage-browserslist-config-ignore/stdout.txt

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Using plugins:
4040
transform-unicode-regex { ios < 12, safari < 12 }
4141
transform-block-scoping { ios < 11, safari < 11 }
4242
transform-export-namespace-from { android < 72, chrome < 72, edge < 79, firefox < 80, ios < 14.5, opera < 60, safari < 14.1, samsung < 11.0 }
43+
syntax-dynamic-import
4344
syntax-top-level-await
4445
syntax-import-meta
4546
corejs2: `DEBUG` option

packages/babel-preset-env/test/fixtures/corejs3-babel-7/usage-browserslist-config-ignore/stdout.txt

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Using plugins:
4040
transform-unicode-regex { ios < 12, safari < 12 }
4141
transform-block-scoping { ios < 11, safari < 11 }
4242
transform-export-namespace-from { android < 72, chrome < 72, edge < 79, firefox < 80, ios < 14.5, opera < 60, safari < 14.1, samsung < 11.0 }
43+
syntax-dynamic-import
4344
syntax-top-level-await
4445
syntax-import-meta
4546
corejs3: `DEBUG` option

packages/babel-preset-env/test/fixtures/debug-babel-7/browserslists-defaults-not-ie/stdout.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ Using plugins:
2828
transform-parameters { ios < 16.3, safari < 16.3 }
2929
syntax-async-generators
3030
syntax-object-rest-spread
31+
transform-export-namespace-from { }
3132
transform-modules-commonjs
3233
transform-dynamic-import
33-
transform-export-namespace-from { }
3434
syntax-top-level-await
3535
syntax-import-meta
3636

packages/babel-preset-env/test/fixtures/debug-babel-7/browserslists-defaults/stdout.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ Using plugins:
2828
transform-parameters { ios < 16.3, safari < 16.3 }
2929
syntax-async-generators
3030
syntax-object-rest-spread
31+
transform-export-namespace-from { }
3132
transform-modules-commonjs
3233
transform-dynamic-import
33-
transform-export-namespace-from { }
3434
syntax-top-level-await
3535
syntax-import-meta
3636

packages/babel-preset-env/test/fixtures/debug-babel-7/browserslists-last-2-versions-not-ie/stdout.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ Using plugins:
2727
syntax-optional-catch-binding
2828
syntax-async-generators
2929
syntax-object-rest-spread
30+
transform-export-namespace-from { }
3031
transform-modules-commonjs
3132
transform-dynamic-import
32-
transform-export-namespace-from { }
3333
syntax-top-level-await
3434
syntax-import-meta
3535

packages/babel-preset-env/test/fixtures/debug-babel-7/entry-corejs2-force-all-transforms/stdout.txt

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Using plugins:
5151
transform-property-literals { }
5252
transform-reserved-words { }
5353
transform-export-namespace-from { chrome < 72 }
54+
syntax-dynamic-import
5455
syntax-top-level-await
5556
syntax-import-meta
5657
corejs2: `DEBUG` option

packages/babel-preset-env/test/fixtures/debug-babel-7/entry-corejs3-force-all-transforms/stdout.txt

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Using plugins:
5151
transform-property-literals { }
5252
transform-reserved-words { }
5353
transform-export-namespace-from { chrome < 72 }
54+
syntax-dynamic-import
5455
syntax-top-level-await
5556
syntax-import-meta
5657
corejs3: `DEBUG` option

packages/babel-preset-env/test/fixtures/debug-babel-7/entry-no-corejs-uglify/stdout.txt

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Using plugins:
5151
transform-property-literals { }
5252
transform-reserved-words { }
5353
transform-export-namespace-from { chrome < 72 }
54+
syntax-dynamic-import
5455
syntax-top-level-await
5556
syntax-import-meta
5657
corejs2: `DEBUG` option

packages/babel-preset-env/test/fixtures/debug-babel-7/shippedProposals-chrome-80/stdout.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ Using plugins:
2121
syntax-optional-catch-binding
2222
syntax-async-generators
2323
syntax-object-rest-spread
24+
transform-export-namespace-from { }
2425
transform-modules-commonjs
2526
transform-dynamic-import
26-
transform-export-namespace-from { }
2727
syntax-top-level-await
2828
syntax-import-meta
2929
syntax-import-attributes

0 commit comments

Comments
 (0)