Skip to content

Commit

Permalink
Refactor handling of modules plugins in preset-env
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Sep 19, 2023
1 parent e0cb7ba commit 04ca3aa
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 49 deletions.
11 changes: 8 additions & 3 deletions packages/babel-core/src/config/helpers/config-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ type EnvFunction = {
(envVars: Array<string>): boolean;
};

type CallerFactory = (
extractor: (callerMetadata: CallerMetadata | undefined) => unknown,
) => SimpleType;
type CallerFactory = {
<T extends SimpleType>(
extractor: (callerMetadata: CallerMetadata | undefined) => T,
): T;
(
extractor: (callerMetadata: CallerMetadata | undefined) => unknown,
): SimpleType;
};
type TargetsFunction = () => Targets;
type AssumptionFunction = (name: AssumptionName) => boolean | undefined;

Expand Down
78 changes: 37 additions & 41 deletions packages/babel-preset-env/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ import type { Targets, InputTargets } from "@babel/helper-compilation-targets";
import availablePlugins from "./available-plugins.ts";
import { declarePreset } from "@babel/helper-plugin-utils";

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

// TODO: Remove in Babel 8
Expand Down Expand Up @@ -120,34 +118,33 @@ export const transformIncludesAndExcludes = (opts: Array<string>): any => {
);
};

export const getModulesPluginNames = ({
export function getModulesPluginNames({
modules,
transformations,
shouldTransformESM,
shouldTransformDynamicImport,
shouldTransformExportNamespaceFrom,
}: {
modules: ModuleOption;
transformations: ModuleTransformationsType;
shouldTransformESM: boolean;
modules: Exclude<ModuleOption, "auto">;
shouldTransformDynamicImport: boolean;
shouldTransformExportNamespaceFrom: boolean;
}) => {
}) {
// For backward compat since this function is exported
if (!process.env.BABEL_8_BREAKING) {
if (arguments[0].modules === "auto") modules = "cjs";
if (arguments[0].shouldTransformESM === false) modules = false;
}
const modulesPluginNames = [];
if (modules !== false && transformations[modules]) {
if (shouldTransformESM) {
modulesPluginNames.push(transformations[modules]);
}
if (modules) {
modulesPluginNames.push(moduleTransformations[modules]);
}

if (shouldTransformDynamicImport) {
if (shouldTransformESM && modules !== "umd") {
modulesPluginNames.push("transform-dynamic-import");
} else {
console.warn(
"Dynamic import can only be transformed when transforming ES" +
" modules to AMD, CommonJS or SystemJS.",
);
}
if (shouldTransformDynamicImport) {
if (modules && modules !== "umd") {
modulesPluginNames.push("transform-dynamic-import");
} else {
console.warn(
"Dynamic import can only be transformed when transforming ES" +
" modules to AMD, CommonJS or SystemJS.",
);
}
}

Expand All @@ -160,15 +157,12 @@ export const getModulesPluginNames = ({
if (!shouldTransformDynamicImport) {
modulesPluginNames.push("syntax-dynamic-import");
}
if (!shouldTransformExportNamespaceFrom) {
modulesPluginNames.push("syntax-export-namespace-from");
}
modulesPluginNames.push("syntax-top-level-await");
modulesPluginNames.push("syntax-import-meta");
}

return modulesPluginNames;
};
}

export const getPolyfillPlugins = ({
useBuiltIns,
Expand Down Expand Up @@ -268,16 +262,19 @@ function getLocalTargets(
}

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

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

function supportsExportNamespaceFrom(caller: CallerMetadata | undefined) {
// TODO(Babel 8): Fallback to null
// @ts-expect-error supportsExportNamespaceFrom is not defined in CallerMetadata
return !!caller?.supportsExportNamespaceFrom;
}
Expand All @@ -296,7 +293,7 @@ export default declarePreset((api, opts: Options) => {
ignoreBrowserslistConfig,
include: optionsInclude,
loose,
modules,
modules: optionsModules,
shippedProposals,
spec,
targets: optionsTargets,
Expand Down Expand Up @@ -354,21 +351,20 @@ option \`forceAllTransforms: true\` instead.
const exclude = transformIncludesAndExcludes(optionsExclude);

const compatData = getPluginList(shippedProposals, bugfixes);
const shouldSkipExportNamespaceFrom =
(modules === "auto" && api.caller(supportsExportNamespaceFrom)) ||
(modules === false &&
!isRequired("transform-export-namespace-from", transformTargets, {
compatData,
includes: include.plugins,
excludes: exclude.plugins,
}));
const modules =
optionsModules === "auto"
? api.caller(supportsStaticESM)
? false
: "commonjs"
: optionsModules;
const shouldTransformDynamicImport =
optionsModules === "auto" ? !api.caller(supportsDynamicImport) : !!modules;
const shouldTransformExportNamespaceFrom =
optionsModules === "auto" && !api.caller(supportsExportNamespaceFrom);
const modulesPluginNames = getModulesPluginNames({
modules,
transformations: moduleTransformations,
shouldTransformESM: modules !== "auto" || !api.caller(supportsStaticESM),
shouldTransformDynamicImport:
modules !== "auto" || !api.caller(supportsDynamicImport),
shouldTransformExportNamespaceFrom: !shouldSkipExportNamespaceFrom,
shouldTransformDynamicImport,
shouldTransformExportNamespaceFrom,
});

const pluginNames = filterItems(
Expand Down Expand Up @@ -432,7 +428,7 @@ option \`forceAllTransforms: true\` instead.
console.log("@babel/preset-env: `DEBUG` option");
console.log("\nUsing targets:");
console.log(JSON.stringify(prettifyTargets(targets), null, 2));
console.log(`\nUsing modules transform: ${modules.toString()}`);
console.log(`\nUsing modules transform: ${optionsModules.toString()}`);
console.log("\nUsing plugins:");
pluginNames.forEach(pluginName => {
logPlugin(pluginName, targets, compatData);
Expand Down
1 change: 0 additions & 1 deletion packages/babel-preset-env/src/module-transformations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
type AvailablePlugins = typeof import("./available-plugins").default;

export default {
auto: "transform-modules-commonjs",
amd: "transform-modules-amd",
commonjs: "transform-modules-commonjs",
cjs: "transform-modules-commonjs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Using plugins:
transform-unicode-regex { ios < 12, safari < 12 }
transform-block-scoping { ios < 11, safari < 11 }
transform-export-namespace-from { android < 72, chrome < 72, edge < 79, firefox < 80, ios < 14.5, opera < 60, safari < 14.1, samsung < 11.0 }
syntax-dynamic-import
syntax-top-level-await
syntax-import-meta
corejs2: `DEBUG` option
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Using plugins:
transform-unicode-regex { ios < 12, safari < 12 }
transform-block-scoping { ios < 11, safari < 11 }
transform-export-namespace-from { android < 72, chrome < 72, edge < 79, firefox < 80, ios < 14.5, opera < 60, safari < 14.1, samsung < 11.0 }
syntax-dynamic-import
syntax-top-level-await
syntax-import-meta
corejs3: `DEBUG` option
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Using plugins:
transform-property-literals { }
transform-reserved-words { }
transform-export-namespace-from { chrome < 72 }
syntax-dynamic-import
syntax-top-level-await
syntax-import-meta
corejs2: `DEBUG` option
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Using plugins:
transform-property-literals { }
transform-reserved-words { }
transform-export-namespace-from { chrome < 72 }
syntax-dynamic-import
syntax-top-level-await
syntax-import-meta
corejs3: `DEBUG` option
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Using plugins:
transform-property-literals { }
transform-reserved-words { }
transform-export-namespace-from { chrome < 72 }
syntax-dynamic-import
syntax-top-level-await
syntax-import-meta
corejs2: `DEBUG` option
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Using plugins:
transform-property-literals { }
transform-reserved-words { }
transform-export-namespace-from { chrome < 72 }
syntax-dynamic-import
syntax-top-level-await
syntax-import-meta
corejs2: `DEBUG` option
Expand Down
4 changes: 0 additions & 4 deletions packages/babel-preset-env/test/index.skip-bundled.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ describe("babel-preset-env", () => {
}),
).toEqual([
"syntax-dynamic-import",
"syntax-export-namespace-from",
"syntax-top-level-await",
"syntax-import-meta",
]);
Expand All @@ -90,7 +89,6 @@ describe("babel-preset-env", () => {
}),
).toEqual([
"syntax-dynamic-import",
"syntax-export-namespace-from",
"syntax-top-level-await",
"syntax-import-meta",
]);
Expand All @@ -112,7 +110,6 @@ describe("babel-preset-env", () => {
: [
"transform-modules-commonjs",
"syntax-dynamic-import",
"syntax-export-namespace-from",
"syntax-top-level-await",
"syntax-import-meta",
],
Expand All @@ -134,7 +131,6 @@ describe("babel-preset-env", () => {
: [
"transform-modules-systemjs",
"transform-dynamic-import",
"syntax-export-namespace-from",
"syntax-top-level-await",
"syntax-import-meta",
],
Expand Down

0 comments on commit 04ca3aa

Please sign in to comment.