diff --git a/packages/babel-preset-env/src/index.js b/packages/babel-preset-env/src/index.js index 573a756a6a2a..76f11ebcb934 100644 --- a/packages/babel-preset-env/src/index.js +++ b/packages/babel-preset-env/src/index.js @@ -22,7 +22,7 @@ import { filterStageFromList, prettifyTargets } from "./utils"; import { declare } from "@babel/helper-plugin-utils"; import typeof ModuleTransformationsType from "./module-transformations"; -import type { BuiltInsOption, Targets } from "./types"; +import type { BuiltInsOption, Targets, ModuleOption } from "./types"; export { isPluginRequired } from "./filter-items"; @@ -61,26 +61,26 @@ export const transformIncludesAndExcludes = (opts: Array): Object => { }; export const getModulesPluginNames = ({ - moduleTransformation, + modules, + transformations, shouldTransformESM, shouldTransformDynamicImport, }: { - moduleTransformation: $Values | null, + modules: ModuleOption, + transformations: ModuleTransformationsType, shouldTransformESM: boolean, shouldTransformDynamicImport: boolean, }) => { const modulesPluginNames = []; - if (moduleTransformation) { + if (modules !== false && transformations[modules]) { if (shouldTransformESM) { - // NOTE: not giving spec here yet to avoid compatibility issues when - // transform-modules-commonjs gets its spec mode - modulesPluginNames.push(moduleTransformation); + modulesPluginNames.push(transformations[modules]); } if ( shouldTransformDynamicImport && shouldTransformESM && - moduleTransformation !== "transform-modules-umd" + modules !== "umd" ) { modulesPluginNames.push("proposal-dynamic-import"); } else { @@ -217,7 +217,8 @@ export default declare((api, opts) => { const transformTargets = forceAllTransforms || hasUglifyTarget ? {} : targets; const modulesPluginNames = getModulesPluginNames({ - moduleTransformation: moduleTransformations[modules.toString()] || null, + modules, + transformations: moduleTransformations, // TODO: Remove the 'api.caller' check eventually. Just here to prevent // unnecessary breakage in the short term for users on older betas/RCs. shouldTransformESM: diff --git a/packages/babel-preset-env/test/index.spec.js b/packages/babel-preset-env/test/index.spec.js index a0206c199bff..ded00e6c2e14 100644 --- a/packages/babel-preset-env/test/index.spec.js +++ b/packages/babel-preset-env/test/index.spec.js @@ -13,6 +13,7 @@ const replaceCoreJS3EntryPlugin = require("../lib/polyfills/corejs3/entry-plugin .default; const removeRegeneratorEntryPlugin = require("../lib/polyfills/regenerator/entry-plugin") .default; +const transformations = require("../lib/module-transformations").default; describe("babel-preset-env", () => { describe("transformIncludesAndExcludes", () => { @@ -37,23 +38,25 @@ describe("babel-preset-env", () => { }); }); describe("getModulesPluginNames", () => { - describe("no moduleTransformation is specified", () => { + describe("modules is set to false", () => { it("returns only syntax-dynamic-import", () => { expect( babelPresetEnv.getModulesPluginNames({ - moduleTransformation: null, + modules: false, + transformations, shouldTransformESM: false, shouldTransformDynamicImport: false, }), ).toEqual(["syntax-dynamic-import"]); }); }); - describe("a moduleTransformation is specified", () => { + describe("modules is not set to false", () => { describe("ESMs should not be transformed", () => { it("returns syntax-dynamic-import", () => { expect( babelPresetEnv.getModulesPluginNames({ - moduleTransformation: "transform-modules-commonjs", + modules: "commonjs", + transformations, shouldTransformESM: false, shouldTransformDynamicImport: false, }), @@ -62,27 +65,30 @@ describe("babel-preset-env", () => { }); describe("ESMs should be transformed", () => { describe("dynamic imports should not be transformed", () => { - it("returns moduleTransformation and syntax-dynamic-import", () => { - const moduleTransformation = "transform-modules-commonjs"; + it("returns specified modules transform and syntax-dynamic-import", () => { expect( babelPresetEnv.getModulesPluginNames({ - moduleTransformation: moduleTransformation, + modules: "commonjs", + transformations, shouldTransformESM: true, shouldTransformDynamicImport: false, }), - ).toEqual([moduleTransformation, "syntax-dynamic-import"]); + ).toEqual(["transform-modules-commonjs", "syntax-dynamic-import"]); }); }); describe("dynamic imports should be transformed", () => { - it("returns moduleTransformation and proposal-dynamic-import", () => { - const moduleTransformation = "transform-modules-commonjs"; + it("returns specified modules transform and proposal-dynamic-import", () => { expect( babelPresetEnv.getModulesPluginNames({ - moduleTransformation: moduleTransformation, + modules: "systemjs", + transformations, shouldTransformESM: true, shouldTransformDynamicImport: true, }), - ).toEqual([moduleTransformation, "proposal-dynamic-import"]); + ).toEqual([ + "transform-modules-systemjs", + "proposal-dynamic-import", + ]); }); }); });