Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internally rename proposal-* to transform-* in preset-env #14976

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 19 additions & 19 deletions packages/babel-preset-env/src/available-plugins.ts
Expand Up @@ -83,23 +83,6 @@ export default {
"bugfix/transform-tagged-template-caching": () => bugfixTaggedTemplateCaching,
"bugfix/transform-v8-spread-parameters-in-optional-chaining": () =>
bugfixV8SpreadParametersInOptionalChaining,
"proposal-async-generator-functions": () => proposalAsyncGeneratorFunctions,
"proposal-class-properties": () => proposalClassProperties,
"proposal-class-static-block": () => proposalClassStaticBlock,
"proposal-dynamic-import": () => proposalDynamicImport,
"proposal-export-namespace-from": () => proposalExportNamespaceFrom,
"proposal-json-strings": () => proposalJsonStrings,
"proposal-logical-assignment-operators": () =>
proposalLogicalAssignmentOperators,
"proposal-nullish-coalescing-operator": () =>
proposalNullishCoalescingOperator,
"proposal-numeric-separator": () => proposalNumericSeparator,
"proposal-object-rest-spread": () => proposalObjectRestSpread,
"proposal-optional-catch-binding": () => proposalOptionalCatchBinding,
"proposal-optional-chaining": () => proposalOptionalChaining,
"proposal-private-methods": () => proposalPrivateMethods,
"proposal-private-property-in-object": () => proposalPrivatePropertyInObject,
"proposal-unicode-property-regex": () => proposalUnicodePropertyRegex,
"syntax-async-generators": () => syntaxAsyncGenerators,
"syntax-class-properties": () => syntaxClassProperties,
"syntax-class-static-block": () => syntaxClassStaticBlock,
Expand All @@ -116,18 +99,26 @@ export default {
"syntax-private-property-in-object": () => syntaxPrivatePropertyInObject,
"syntax-top-level-await": () => syntaxTopLevelAwait,
"transform-arrow-functions": () => transformArrowFunctions,
"transform-async-generator-functions": () => proposalAsyncGeneratorFunctions,
"transform-async-to-generator": () => transformAsyncToGenerator,
"transform-block-scoped-functions": () => transformBlockScopedFunctions,
"transform-block-scoping": () => transformBlockScoping,
"transform-class-properties": () => proposalClassProperties,
"transform-class-static-block": () => proposalClassStaticBlock,
"transform-classes": () => transformClasses,
"transform-computed-properties": () => transformComputedProperties,
"transform-destructuring": () => transformDestructuring,
"transform-dotall-regex": () => transformDotallRegex,
"transform-duplicate-keys": () => transformDuplicateKeys,
"transform-dynamic-import": () => proposalDynamicImport,
"transform-exponentiation-operator": () => transformExponentialOperator,
"transform-export-namespace-from": () => proposalExportNamespaceFrom,
"transform-for-of": () => transformForOf,
"transform-function-name": () => transformFunctionName,
"transform-json-strings": () => proposalJsonStrings,
"transform-literals": () => transformLiterals,
"transform-logical-assignment-operators": () =>
proposalLogicalAssignmentOperators,
"transform-member-expression-literals": () =>
transformMemberExpressionLiterals,
"transform-modules-amd": () => transformModulesAmd,
Expand All @@ -137,8 +128,16 @@ export default {
"transform-named-capturing-groups-regex": () =>
transformNamedCapturingGroupsRegex,
"transform-new-target": () => transformNewTarget,
"transform-nullish-coalescing-operator": () =>
proposalNullishCoalescingOperator,
"transform-numeric-separator": () => proposalNumericSeparator,
"transform-object-rest-spread": () => proposalObjectRestSpread,
"transform-object-super": () => transformObjectSuper,
"transform-optional-catch-binding": () => proposalOptionalCatchBinding,
"transform-optional-chaining": () => proposalOptionalChaining,
"transform-parameters": () => transformParameters,
"transform-private-methods": () => proposalPrivateMethods,
"transform-private-property-in-object": () => proposalPrivatePropertyInObject,
"transform-property-literals": () => transformPropertyLiterals,
"transform-regenerator": () => transformRegenerator,
"transform-reserved-words": () => transformReservedWords,
Expand All @@ -148,12 +147,13 @@ export default {
"transform-template-literals": () => transformTemplateLiterals,
"transform-typeof-symbol": () => transformTypeofSymbol,
"transform-unicode-escapes": () => transformUnicodeEscapes,
"transform-unicode-property-regex": () => proposalUnicodePropertyRegex,
"transform-unicode-regex": () => transformUnicodeRegex,
};

export const minVersions = {
"bugfix/transform-safari-id-destructuring-collision-in-function-expression":
"7.16.0",
"proposal-class-static-block": "7.12.0",
"proposal-private-property-in-object": "7.10.0",
"transform-class-static-block": "7.12.0",
"transform-private-property-in-object": "7.10.0",
};
21 changes: 18 additions & 3 deletions packages/babel-preset-env/src/debug.ts
@@ -1,6 +1,9 @@
import { getInclusionReasons } from "@babel/helper-compilation-targets";

import type { Targets, Target } from "@babel/helper-compilation-targets";
import {
getInclusionReasons,
type Targets,
type Target,
} from "@babel/helper-compilation-targets";
import compatData from "@babel/compat-data/plugins";

// Outputs a message that shows which target(s) caused an item to be included:
// transform-foo { "edge":"13", "firefox":"49", "ie":"10" }
Expand All @@ -13,6 +16,18 @@ export const logPlugin = (

const support = list[item];

// TODO(Babel 8): Remove this. It's needed to keep outputting proposal-
// in the debug log.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes in this file are to continue printing proposal- instead of transform- in the debug log. I think that it would be ok to print transform-, since the debug output is just logging and thus I wouldn't consider it a breaking change. What do you think?

if (item.startsWith("transform-")) {
const proposalName = `proposal-${item.slice(10)}`;
if (
proposalName === "proposal-dynamic-import" ||
Object.prototype.hasOwnProperty.call(compatData, proposalName)
) {
item = proposalName;
}
}

if (!support) {
console.log(` ${item}`);
return;
Expand Down
14 changes: 7 additions & 7 deletions packages/babel-preset-env/src/index.ts
Expand Up @@ -16,8 +16,8 @@ import {
import {
plugins as pluginsList,
pluginsBugfixes as pluginsBugfixesList,
overlappingPlugins,
} from "./plugins-compat-data";
import overlappingPlugins from "@babel/compat-data/overlapping-plugins";

import removeRegeneratorEntryPlugin from "./polyfills/regenerator";
import legacyBabelPolyfillPlugin from "./polyfills/babel-polyfill";
Expand Down Expand Up @@ -146,7 +146,7 @@ export const getModulesPluginNames = ({
shouldTransformESM &&
modules !== "umd"
) {
modulesPluginNames.push("proposal-dynamic-import");
modulesPluginNames.push("transform-dynamic-import");
} else {
if (shouldTransformDynamicImport) {
console.warn(
Expand All @@ -161,7 +161,7 @@ export const getModulesPluginNames = ({
}

if (shouldTransformExportNamespaceFrom) {
modulesPluginNames.push("proposal-export-namespace-from");
modulesPluginNames.push("transform-export-namespace-from");
} else {
modulesPluginNames.push("syntax-export-namespace-from");
}
Expand Down Expand Up @@ -362,7 +362,7 @@ option \`forceAllTransforms: true\` instead.
const shouldSkipExportNamespaceFrom =
(modules === "auto" && api.caller?.(supportsExportNamespaceFrom)) ||
(modules === false &&
!isRequired("proposal-export-namespace-from", transformTargets, {
!isRequired("transform-export-namespace-from", transformTargets, {
compatData,
includes: include.plugins,
excludes: exclude.plugins,
Expand Down Expand Up @@ -411,9 +411,9 @@ option \`forceAllTransforms: true\` instead.
const plugins = Array.from(pluginNames)
.map(pluginName => {
if (
pluginName === "proposal-class-properties" ||
pluginName === "proposal-private-methods" ||
pluginName === "proposal-private-property-in-object"
pluginName === "transform-class-properties" ||
pluginName === "transform-private-methods" ||
pluginName === "transform-private-property-in-object"
) {
return [
getPlugin(pluginName),
Expand Down
89 changes: 47 additions & 42 deletions packages/babel-preset-env/src/normalize-options.ts
@@ -1,7 +1,7 @@
import semver, { type SemVer } from "semver";
import corejs2Polyfills from "@babel/compat-data/corejs2-built-ins";
// @ts-expect-error Fixme: TS can not infer types from ../data/core-js-compat.js
// but we can't import core-js-compat/data.json because JSON imports does
// but we can't import core-js-compat/data.json because JSON imports do
// not work on Node 14
import corejs3Polyfills from "../data/core-js-compat";
import { plugins as pluginsList } from "./plugins-compat-data";
Expand All @@ -20,7 +20,6 @@ import type {
CorejsOption,
ModuleOption,
Options,
PluginListItem,
PluginListOption,
} from "./types";

Expand All @@ -34,67 +33,73 @@ const allPluginsList = Object.keys(pluginsList);
// should only be possible to exclude and not include module plugins, otherwise it's possible that preset-env
// will add a module plugin twice.
const modulePlugins = [
"proposal-dynamic-import",
"transform-dynamic-import",
...Object.keys(moduleTransformations).map(m => moduleTransformations[m]),
];

const getValidIncludesAndExcludes = (
type: "include" | "exclude",
corejs: number | false,
) =>
new Set([
...allPluginsList,
...(type === "exclude" ? modulePlugins : []),
...(corejs
? corejs == 2
? [...Object.keys(corejs2Polyfills), ...corejs2DefaultWebIncludes]
: Object.keys(corejs3Polyfills)
: []),
]);

const pluginToRegExp = (plugin: PluginListItem) => {
if (plugin instanceof RegExp) return plugin;
try {
return new RegExp(`^${normalizePluginName(plugin)}$`);
} catch (e) {
return null;
}
};

const selectPlugins = (
regexp: RegExp | null,
type: "include" | "exclude",
corejs: number | false,
) =>
Array.from(getValidIncludesAndExcludes(type, corejs)).filter(
item => regexp instanceof RegExp && regexp.test(item),
Array.from(
new Set([
...allPluginsList,
...(type === "exclude" ? modulePlugins : []),
...(corejs
? corejs == 2
? [...Object.keys(corejs2Polyfills), ...corejs2DefaultWebIncludes]
: Object.keys(corejs3Polyfills)
: []),
]),
);

const flatten = <T>(array: Array<Array<T>>): Array<T> => [].concat(...array);
function flatMap<T, U>(array: Array<T>, fn: (item: T) => Array<U>): Array<U> {
return Array.prototype.concat.apply([], array.map(fn));
}

const expandIncludesAndExcludes = (
plugins: PluginListOption = [],
filterList: PluginListOption = [],
type: "include" | "exclude",
corejs: number | false,
) => {
if (plugins.length === 0) return [];

const selectedPlugins = plugins.map(plugin =>
selectPlugins(pluginToRegExp(plugin), type, corejs),
);
const invalidRegExpList = plugins.filter(
(p, i) => selectedPlugins[i].length === 0,
);
if (filterList.length === 0) return [];

const filterableItems = getValidIncludesAndExcludes(type, corejs);

const invalidFilters: PluginListOption = [];
const selectedPlugins = flatMap(filterList, filter => {
let re: RegExp;
if (typeof filter === "string") {
try {
re = new RegExp(`^${normalizePluginName(filter)}$`);
} catch (e) {
invalidFilters.push(filter);
return [];
}
} else {
re = filter;
}
const items = filterableItems.filter(
item =>
re.test(item) ||
// For backwards compatibility, we also support matching against the
// proposal- name.
// TODO(Babel 8): Remove this.
re.test(item.replace(/^transform-/, "proposal-")),
);
if (items.length === 0) invalidFilters.push(filter);
return items;
});

v.invariant(
invalidRegExpList.length === 0,
`The plugins/built-ins '${invalidRegExpList.join(
invalidFilters.length === 0,
`The plugins/built-ins '${invalidFilters.join(
", ",
)}' passed to the '${type}' option are not
valid. Please check data/[plugin-features|built-in-features].js in babel-preset-env`,
);

return flatten<string>(selectedPlugins);
return selectedPlugins;
};

export const normalizePluginName = (plugin: string) =>
Expand Down
48 changes: 34 additions & 14 deletions packages/babel-preset-env/src/plugins-compat-data.ts
@@ -1,22 +1,42 @@
import plugins from "@babel/compat-data/plugins";
import bugfixPlugins from "@babel/compat-data/plugin-bugfixes";
import originalPlugins from "@babel/compat-data/plugins";
import originalPluginsBugfixes from "@babel/compat-data/plugin-bugfixes";
import originalOverlappingPlugins from "@babel/compat-data/overlapping-plugins";
import availablePlugins from "./available-plugins";

const pluginsFiltered = {};
const bugfixPluginsFiltered = {};
const keys: <O extends object>(o: O) => (keyof O)[] = Object.keys;

for (const plugin of Object.keys(plugins)) {
if (Object.hasOwnProperty.call(availablePlugins, plugin)) {
// @ts-expect-error fixme: refine pluginsFiltered types
pluginsFiltered[plugin] = plugins[plugin];
export const plugins = filterAvailable(proposalToTransform(originalPlugins));
export const pluginsBugfixes = filterAvailable(
proposalToTransform(originalPluginsBugfixes),
);
export const overlappingPlugins = proposalToTransform(
originalOverlappingPlugins,
);

function filterAvailable<Data extends { [name: string]: unknown }>(
data: Data,
): { [Name in keyof Data & keyof typeof availablePlugins]: Data[Name] } {
const result = {} as any;
for (const plugin of keys(data)) {
if (Object.hasOwnProperty.call(availablePlugins, plugin)) {
result[plugin] = data[plugin];
}
}
return result;
}

for (const plugin of Object.keys(bugfixPlugins)) {
if (Object.hasOwnProperty.call(availablePlugins, plugin)) {
// @ts-expect-error fixme: refine bugfixPluginsFiltered types
bugfixPluginsFiltered[plugin] = bugfixPlugins[plugin];
// TODO(Babel 8): Store the plugins directly as transform- in @babel/compat-data
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
function proposalToTransform<Data extends { [name: string]: unknown }>(
plugins: Data,
): {
[Name in keyof Data as Name extends `proposal-${infer Feat}`
? `transform-${Feat}`
: Name]: Data[Name];
} {
const result = {} as any;
for (const plugin of keys(plugins)) {
result[(plugin as string).replace(/^proposal-/, "transform-")] =
plugins[plugin];
}
return result;
}

export { pluginsFiltered as plugins, bugfixPluginsFiltered as pluginsBugfixes };
24 changes: 12 additions & 12 deletions packages/babel-preset-env/src/shipped-proposals.ts
Expand Up @@ -11,19 +11,19 @@ const proposalSyntaxPlugins = ["syntax-import-assertions"] as const;

// use intermediary object to enforce alphabetical key order
const pluginSyntaxObject = {
"proposal-async-generator-functions": "syntax-async-generators",
"proposal-class-properties": "syntax-class-properties",
"proposal-class-static-block": "syntax-class-static-block",
"proposal-json-strings": "syntax-json-strings",
"proposal-nullish-coalescing-operator": "syntax-nullish-coalescing-operator",
"proposal-numeric-separator": "syntax-numeric-separator",
"proposal-object-rest-spread": "syntax-object-rest-spread",
"proposal-optional-catch-binding": "syntax-optional-catch-binding",
"proposal-optional-chaining": "syntax-optional-chaining",
"transform-async-generator-functions": "syntax-async-generators",
"transform-class-properties": "syntax-class-properties",
"transform-class-static-block": "syntax-class-static-block",
"transform-json-strings": "syntax-json-strings",
"transform-nullish-coalescing-operator": "syntax-nullish-coalescing-operator",
"transform-numeric-separator": "syntax-numeric-separator",
"transform-object-rest-spread": "syntax-object-rest-spread",
"transform-optional-catch-binding": "syntax-optional-catch-binding",
"transform-optional-chaining": "syntax-optional-chaining",
// note: we don't have syntax-private-methods
"proposal-private-methods": "syntax-class-properties",
"proposal-private-property-in-object": "syntax-private-property-in-object",
"proposal-unicode-property-regex": null as null,
"transform-private-methods": "syntax-class-properties",
"transform-private-property-in-object": "syntax-private-property-in-object",
"transform-unicode-property-regex": null as null,
} as const;

const pluginSyntaxEntries = Object.keys(pluginSyntaxObject).map<
Expand Down