Skip to content

Commit

Permalink
[babel 8] Remove getModulesPluginNames (#15989)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Oct 20, 2023
1 parent 2c5fb77 commit 3688240
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 85 deletions.
73 changes: 35 additions & 38 deletions packages/babel-preset-env/src/index.ts
Expand Up @@ -502,45 +502,42 @@ option \`forceAllTransforms: true\` instead.
return { plugins };
});

// TODO(Babel 8): This is only here for backward compatibility. Remove it.
export { getModulesPluginNamesBackwardCompat as getModulesPluginNames };
const getModulesPluginNamesBackwardCompat = ({
modules,
transformations,
shouldTransformESM,
shouldTransformDynamicImport,
shouldTransformExportNamespaceFrom,
}: {
modules: ModuleOption;
transformations: typeof import("./module-transformations").default;
shouldTransformESM: boolean;
shouldTransformDynamicImport: boolean;
shouldTransformExportNamespaceFrom: boolean;
}) => {
const modulesPluginNames = [];
if (modules !== false && transformations[modules]) {
if (shouldTransformESM) {
modulesPluginNames.push(transformations[modules]);
}
if (!process.env.BABEL_8_BREAKING && !USE_ESM) {
// eslint-disable-next-line no-restricted-globals
exports.getModulesPluginNames = ({
modules,
transformations,
shouldTransformESM,
shouldTransformDynamicImport,
shouldTransformExportNamespaceFrom,
}: {
modules: ModuleOption;
transformations: typeof import("./module-transformations").default;
shouldTransformESM: boolean;
shouldTransformDynamicImport: boolean;
shouldTransformExportNamespaceFrom: boolean;
}) => {
const modulesPluginNames = [];
if (modules !== false && transformations[modules]) {
if (shouldTransformESM) {
modulesPluginNames.push(transformations[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 (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 (shouldTransformExportNamespaceFrom) {
modulesPluginNames.push("transform-export-namespace-from");
}

if (!process.env.BABEL_8_BREAKING) {
// Enable module-related syntax plugins for older Babel versions
if (shouldTransformExportNamespaceFrom) {
modulesPluginNames.push("transform-export-namespace-from");
}
if (!shouldTransformDynamicImport) {
modulesPluginNames.push("syntax-dynamic-import");
}
Expand All @@ -549,7 +546,7 @@ const getModulesPluginNamesBackwardCompat = ({
}
modulesPluginNames.push("syntax-top-level-await");
modulesPluginNames.push("syntax-import-meta");
}

return modulesPluginNames;
};
return modulesPluginNames;
};
}
72 changes: 25 additions & 47 deletions packages/babel-preset-env/test/index.skip-bundled.js
Expand Up @@ -2,13 +2,7 @@
import compatData from "@babel/compat-data/plugins";
import * as babel from "@babel/core";

import {
USE_ESM,
itBabel7,
itBabel8,
describeBabel7,
describeBabel7NoESM,
} from "$repo-utils";
import { USE_ESM, itBabel7, itBabel8, describeBabel7NoESM } from "$repo-utils";

import * as babelPresetEnv from "../lib/index.js";

Expand Down Expand Up @@ -60,8 +54,8 @@ describe("babel-preset-env", () => {
});
});
});
describe("getModulesPluginNames", () => {
describeBabel7("modules is set to false", () => {
describeBabel7NoESM("getModulesPluginNames", () => {
describe("modules is set to false", () => {
it("returns only syntax plugins", () => {
expect(
babelPresetEnv.getModulesPluginNames({
Expand All @@ -80,7 +74,7 @@ describe("babel-preset-env", () => {
});
});
describe("modules is not set to false", () => {
describeBabel7("ESMs should not be transformed", () => {
describe("ESMs should not be transformed", () => {
it("returns syntax plugins", () => {
expect(
babelPresetEnv.getModulesPluginNames({
Expand Down Expand Up @@ -108,17 +102,13 @@ describe("babel-preset-env", () => {
shouldTransformDynamicImport: false,
shouldTransformExportNamespaceFrom: false,
});
expect(names).toEqual(
process.env.BABEL_8_BREAKING
? ["transform-modules-commonjs"]
: [
"transform-modules-commonjs",
"syntax-dynamic-import",
"syntax-export-namespace-from",
"syntax-top-level-await",
"syntax-import-meta",
],
);
expect(names).toEqual([
"transform-modules-commonjs",
"syntax-dynamic-import",
"syntax-export-namespace-from",
"syntax-top-level-await",
"syntax-import-meta",
]);
});
});
describe("dynamic imports should be transformed", () => {
Expand All @@ -130,17 +120,13 @@ describe("babel-preset-env", () => {
shouldTransformDynamicImport: true,
shouldTransformExportNamespaceFrom: false,
});
expect(names).toEqual(
process.env.BABEL_8_BREAKING
? ["transform-modules-systemjs", "transform-dynamic-import"]
: [
"transform-modules-systemjs",
"transform-dynamic-import",
"syntax-export-namespace-from",
"syntax-top-level-await",
"syntax-import-meta",
],
);
expect(names).toEqual([
"transform-modules-systemjs",
"transform-dynamic-import",
"syntax-export-namespace-from",
"syntax-top-level-await",
"syntax-import-meta",
]);
});
describe("export namespace from should be transformed", () => {
it("works", () => {
Expand All @@ -151,21 +137,13 @@ describe("babel-preset-env", () => {
shouldTransformDynamicImport: true,
shouldTransformExportNamespaceFrom: true,
});
expect(names).toEqual(
process.env.BABEL_8_BREAKING
? [
"transform-modules-systemjs",
"transform-dynamic-import",
"transform-export-namespace-from",
]
: [
"transform-modules-systemjs",
"transform-dynamic-import",
"transform-export-namespace-from",
"syntax-top-level-await",
"syntax-import-meta",
],
);
expect(names).toEqual([
"transform-modules-systemjs",
"transform-dynamic-import",
"transform-export-namespace-from",
"syntax-top-level-await",
"syntax-import-meta",
]);
});
});
});
Expand Down

0 comments on commit 3688240

Please sign in to comment.