Skip to content

Commit

Permalink
Fix export-namespace-from handling 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 1836bae
Show file tree
Hide file tree
Showing 19 changed files with 80 additions and 57 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
99 changes: 56 additions & 43 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 @@ -168,7 +165,7 @@ export const getModulesPluginNames = ({
}

return modulesPluginNames;
};
}

export const getPolyfillPlugins = ({
useBuiltIns,
Expand Down Expand Up @@ -268,18 +265,26 @@ function getLocalTargets(
}

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

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

function supportsExportNamespaceFrom(caller: CallerMetadata | undefined) {
// @ts-expect-error supportsExportNamespaceFrom is not defined in CallerMetadata
return !!caller?.supportsExportNamespaceFrom;
const supported = caller?.supportsExportNamespaceFrom;
return supported == null ? null : Boolean(supported);
}

export default declarePreset((api, opts: Options) => {
Expand All @@ -296,7 +301,7 @@ export default declarePreset((api, opts: Options) => {
ignoreBrowserslistConfig,
include: optionsInclude,
loose,
modules,
modules: optionsModules,
shippedProposals,
spec,
targets: optionsTargets,
Expand Down Expand Up @@ -354,21 +359,29 @@ 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 targetsNeedExportNsTransform = isRequired(
"transform-export-namespace-from",
transformTargets,
{ compatData, includes: include.plugins, excludes: exclude.plugins },
);
const shouldTransformExportNamespaceFrom =
modules === false
? targetsNeedExportNsTransform
: optionsModules === "auto"
? api.caller(supportsExportNamespaceFrom) ?? targetsNeedExportNsTransform
: false;
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 +445,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 @@ -22,7 +22,7 @@ Using plugins:
bugfix/transform-safari-id-destructuring-collision-in-function-expression { safari < 16.3 }
transform-modules-commonjs
transform-dynamic-import
transform-export-namespace-from { }
syntax-export-namespace-from
syntax-top-level-await
syntax-import-meta

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Using plugins:
syntax-object-rest-spread
transform-modules-commonjs
transform-dynamic-import
transform-export-namespace-from { }
syntax-export-namespace-from
syntax-top-level-await
syntax-import-meta

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Using plugins:
syntax-object-rest-spread
transform-modules-commonjs
transform-dynamic-import
transform-export-namespace-from { }
syntax-export-namespace-from
syntax-top-level-await
syntax-import-meta

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Using plugins:
bugfix/transform-v8-spread-parameters-in-optional-chaining { chrome < 91 }
transform-modules-commonjs
transform-dynamic-import
transform-export-namespace-from { }
syntax-export-namespace-from
syntax-top-level-await
syntax-import-meta

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 @@ -30,7 +30,7 @@ Using plugins:
syntax-object-rest-spread
transform-modules-commonjs
transform-dynamic-import
transform-export-namespace-from { }
syntax-export-namespace-from
syntax-top-level-await
syntax-import-meta

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Using plugins:
syntax-object-rest-spread
transform-modules-commonjs
transform-dynamic-import
transform-export-namespace-from { }
syntax-export-namespace-from
syntax-top-level-await
syntax-import-meta

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Using plugins:
syntax-object-rest-spread
transform-modules-commonjs
transform-dynamic-import
transform-export-namespace-from { }
syntax-export-namespace-from
syntax-top-level-await
syntax-import-meta

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 @@ -23,7 +23,7 @@ Using plugins:
syntax-object-rest-spread
transform-modules-commonjs
transform-dynamic-import
transform-export-namespace-from { }
syntax-export-namespace-from
syntax-top-level-await
syntax-import-meta
syntax-import-attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Using plugins:
syntax-object-rest-spread
transform-modules-commonjs
transform-dynamic-import
transform-export-namespace-from { }
syntax-export-namespace-from
syntax-top-level-await
syntax-import-meta
syntax-import-attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Using plugins:
syntax-object-rest-spread
transform-modules-commonjs
transform-dynamic-import
transform-export-namespace-from { }
syntax-export-namespace-from
syntax-top-level-await
syntax-import-meta

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

0 comments on commit 1836bae

Please sign in to comment.