Skip to content

Commit

Permalink
Move transform selection to getModulesPluginNames
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamRamberg committed Jul 19, 2019
1 parent ae1f9fc commit 50b40bf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
19 changes: 10 additions & 9 deletions packages/babel-preset-env/src/index.js
Expand Up @@ -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";

Expand Down Expand Up @@ -61,26 +61,26 @@ export const transformIncludesAndExcludes = (opts: Array<string>): Object => {
};

export const getModulesPluginNames = ({
moduleTransformation,
modules,
transformations,
shouldTransformESM,
shouldTransformDynamicImport,
}: {
moduleTransformation: $Values<ModuleTransformationsType> | 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 {
Expand Down Expand Up @@ -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:
Expand Down
30 changes: 18 additions & 12 deletions packages/babel-preset-env/test/index.spec.js
Expand Up @@ -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", () => {
Expand All @@ -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,
}),
Expand All @@ -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",
]);
});
});
});
Expand Down

0 comments on commit 50b40bf

Please sign in to comment.