diff --git a/packages/babel-preset-env/src/index.ts b/packages/babel-preset-env/src/index.ts index 8cb104ca5be3..75680f5b623c 100644 --- a/packages/babel-preset-env/src/index.ts +++ b/packages/babel-preset-env/src/index.ts @@ -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"); } @@ -549,7 +546,7 @@ const getModulesPluginNamesBackwardCompat = ({ } modulesPluginNames.push("syntax-top-level-await"); modulesPluginNames.push("syntax-import-meta"); - } - return modulesPluginNames; -}; + return modulesPluginNames; + }; +} diff --git a/packages/babel-preset-env/test/index.skip-bundled.js b/packages/babel-preset-env/test/index.skip-bundled.js index c5540efd7fbd..f17da685c286 100644 --- a/packages/babel-preset-env/test/index.skip-bundled.js +++ b/packages/babel-preset-env/test/index.skip-bundled.js @@ -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"; @@ -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({ @@ -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({ @@ -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", () => { @@ -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", () => { @@ -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", + ]); }); }); });