Skip to content

Commit

Permalink
[babel 8] Remove getModulesPluginNames
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Sep 20, 2023
1 parent 1613ce2 commit 77b4cce
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 79 deletions.
73 changes: 35 additions & 38 deletions packages/babel-preset-env/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,45 +436,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 @@ -483,7 +480,7 @@ const getModulesPluginNamesBackwardCompat = ({
}
modulesPluginNames.push("syntax-top-level-await");
modulesPluginNames.push("syntax-import-meta");
}

return modulesPluginNames;
};
return modulesPluginNames;
};
}
66 changes: 25 additions & 41 deletions packages/babel-preset-env/test/index.skip-bundled.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if (/* commonjs */ _transformations.default) {
pluginCoreJS3 = _pluginCoreJS3_esm;
pluginRegenerator = _pluginRegenerator_esm;
}
import { itBabel7, itBabel8, describeBabel7 } from "$repo-utils";
import { itBabel7, itBabel8, describeBabel7NoESM } from "$repo-utils";

describe("babel-preset-env", () => {
describe("transformIncludesAndExcludes", () => {
Expand All @@ -58,8 +58,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 @@ -78,7 +78,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 @@ -106,17 +106,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 @@ -128,17 +124,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 @@ -149,21 +141,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
1 change: 1 addition & 0 deletions scripts/repo-utils/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ if (typeof jest !== "undefined") {
};
exports.describeESM = USE_ESM ? describe : dummy;
exports.describeBabel7 = process.env.BABEL_8_BREAKING ? dummy : describe;
exports.describeBabel7NoESM = USE_ESM ? dummy : exports.describeBabel7;
exports.describeBabel8 = process.env.BABEL_8_BREAKING ? describe : dummy;
}

Expand Down
1 change: 1 addition & 0 deletions scripts/repo-utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export const itBabel7: jest.It;
export const itBabel7NoESM: jest.It;
export const itDummy: jest.It;
export const describeBabel7: jest.Describe;
export const describeBabel7NoESM: jest.Describe;
export const describeBabel8: jest.Describe;

0 comments on commit 77b4cce

Please sign in to comment.