Skip to content

Commit

Permalink
[babel 8] Remove default syntax plugins from preset-env (#15810)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jul 24, 2023
1 parent 7a3e18a commit 6b91b9b
Show file tree
Hide file tree
Showing 133 changed files with 534 additions and 254 deletions.
39 changes: 39 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,43 @@ packageExtensions:
# It doesn't support Prettier 3 yet
prettier: "^2.0.0"

# @babel/preset-env depends on all these packages using the condition:
# protocol, which does not properly propagate peer dependency requirements.
# We know that we have the correct Babel version in the monorepo, so let's
# just remove the peerDependency requirement to silence the warning.
"@babel/plugin-proposal-private-property-in-object@*":
peerDependencies: { "@babel/core": null }
"@babel/plugin-syntax-async-generators@*":
peerDependencies: { "@babel/core": null }
"@babel/plugin-syntax-class-properties@*":
peerDependencies: { "@babel/core": null }
"@babel/plugin-syntax-class-static-block@*":
peerDependencies: { "@babel/core": null }
"@babel/plugin-syntax-dynamic-import@*":
peerDependencies: { "@babel/core": null }
"@babel/plugin-syntax-export-namespace-from@*":
peerDependencies: { "@babel/core": null }
"@babel/plugin-syntax-import-meta@*":
peerDependencies: { "@babel/core": null }
"@babel/plugin-syntax-json-strings@*":
peerDependencies: { "@babel/core": null }
"@babel/plugin-syntax-logical-assignment-operators@*":
peerDependencies: { "@babel/core": null }
"@babel/plugin-syntax-nullish-coalescing-operator@*":
peerDependencies: { "@babel/core": null }
"@babel/plugin-syntax-numeric-separator@*":
peerDependencies: { "@babel/core": null }
"@babel/plugin-syntax-object-rest-spread@*":
peerDependencies: { "@babel/core": null }
"@babel/plugin-syntax-optional-catch-binding@*":
peerDependencies: { "@babel/core": null }
"@babel/plugin-syntax-optional-chaining@*":
peerDependencies: { "@babel/core": null }
"@babel/plugin-syntax-private-property-in-object@*":
peerDependencies: { "@babel/core": null }
"@babel/plugin-syntax-top-level-await@*":
peerDependencies: { "@babel/core": null }
"@babel/plugin-syntax-unicode-sets-regex@*":
peerDependencies: { "@babel/core": null }

yarnPath: .yarn/releases/yarn-3.6.1.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,15 @@ async function run(task: Test) {
validateFile(
normalizeOutput(actualLogs.stdout, normalizationOpts),
stdout.loc,
stdout.code,
process.env.BABEL_8_BREAKING
? // In Babel 8, preset-env does not enable all the unnecessary syntax
// plugins. For simplicity, just strip them fro the expected output
// so that we do not need to separate tests for every fixture.
stdout.code.replace(
/\n\s*syntax-(?!import-attributes|import-assertions).*/g,
"",
)
: stdout.code,
);
validateFile(
normalizeOutput(actualLogs.stderr, normalizationOpts),
Expand Down
32 changes: 16 additions & 16 deletions packages/babel-preset-env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@
"@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression": "workspace:^",
"@babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining": "workspace:^",
"@babel/plugin-proposal-private-property-in-object": "condition:BABEL_8_BREAKING ? : 7.21.0-placeholder-for-preset-env.2",
"@babel/plugin-syntax-async-generators": "^7.8.4",
"@babel/plugin-syntax-class-properties": "^7.12.13",
"@babel/plugin-syntax-class-static-block": "^7.14.5",
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
"@babel/plugin-syntax-export-namespace-from": "^7.8.3",
"@babel/plugin-syntax-async-generators": "condition:BABEL_8_BREAKING ? : ^7.8.4",
"@babel/plugin-syntax-class-properties": "condition:BABEL_8_BREAKING ? : ^7.12.13",
"@babel/plugin-syntax-class-static-block": "condition:BABEL_8_BREAKING ? : ^7.14.5",
"@babel/plugin-syntax-dynamic-import": "condition:BABEL_8_BREAKING ? : ^7.8.3",
"@babel/plugin-syntax-export-namespace-from": "condition:BABEL_8_BREAKING ? : ^7.8.3",
"@babel/plugin-syntax-import-assertions": "workspace:^",
"@babel/plugin-syntax-import-attributes": "workspace:^",
"@babel/plugin-syntax-import-meta": "^7.10.4",
"@babel/plugin-syntax-json-strings": "^7.8.3",
"@babel/plugin-syntax-logical-assignment-operators": "^7.10.4",
"@babel/plugin-syntax-nullish-coalescing-operator": "^7.8.3",
"@babel/plugin-syntax-numeric-separator": "^7.10.4",
"@babel/plugin-syntax-object-rest-spread": "^7.8.3",
"@babel/plugin-syntax-optional-catch-binding": "^7.8.3",
"@babel/plugin-syntax-optional-chaining": "^7.8.3",
"@babel/plugin-syntax-private-property-in-object": "^7.14.5",
"@babel/plugin-syntax-top-level-await": "^7.14.5",
"@babel/plugin-syntax-unicode-sets-regex": "^7.18.6",
"@babel/plugin-syntax-import-meta": "condition:BABEL_8_BREAKING ? : ^7.10.4",
"@babel/plugin-syntax-json-strings": "condition:BABEL_8_BREAKING ? : ^7.8.3",
"@babel/plugin-syntax-logical-assignment-operators": "condition:BABEL_8_BREAKING ? : ^7.10.4",
"@babel/plugin-syntax-nullish-coalescing-operator": "condition:BABEL_8_BREAKING ? : ^7.8.3",
"@babel/plugin-syntax-numeric-separator": "condition:BABEL_8_BREAKING ? : ^7.10.4",
"@babel/plugin-syntax-object-rest-spread": "condition:BABEL_8_BREAKING ? : ^7.8.3",
"@babel/plugin-syntax-optional-catch-binding": "condition:BABEL_8_BREAKING ? : ^7.8.3",
"@babel/plugin-syntax-optional-chaining": "condition:BABEL_8_BREAKING ? : ^7.8.3",
"@babel/plugin-syntax-private-property-in-object": "condition:BABEL_8_BREAKING ? : ^7.14.5",
"@babel/plugin-syntax-top-level-await": "condition:BABEL_8_BREAKING ? : ^7.14.5",
"@babel/plugin-syntax-unicode-sets-regex": "condition:BABEL_8_BREAKING ? : ^7.18.6",
"@babel/plugin-transform-arrow-functions": "workspace:^",
"@babel/plugin-transform-async-generator-functions": "workspace:^",
"@babel/plugin-transform-async-to-generator": "workspace:^",
Expand Down
102 changes: 50 additions & 52 deletions packages/babel-preset-env/src/available-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,9 @@

declare const USE_ESM: boolean;

import syntaxAsyncGenerators from "@babel/plugin-syntax-async-generators";
import syntaxClassProperties from "@babel/plugin-syntax-class-properties";
import syntaxClassStaticBlock from "@babel/plugin-syntax-class-static-block";
import syntaxDynamicImport from "@babel/plugin-syntax-dynamic-import";
import syntaxExportNamespaceFrom from "@babel/plugin-syntax-export-namespace-from";
import syntaxImportAssertions from "@babel/plugin-syntax-import-assertions";
import syntaxImportAttributes from "@babel/plugin-syntax-import-attributes";
import syntaxImportMeta from "@babel/plugin-syntax-import-meta";
import syntaxJsonStrings from "@babel/plugin-syntax-json-strings";
import syntaxLogicalAssignmentOperators from "@babel/plugin-syntax-logical-assignment-operators";
import syntaxNullishCoalescingOperator from "@babel/plugin-syntax-nullish-coalescing-operator";
import syntaxNumericSeparator from "@babel/plugin-syntax-numeric-separator";
import syntaxObjectRestSpread from "@babel/plugin-syntax-object-rest-spread";
import syntaxOptionalCatchBinding from "@babel/plugin-syntax-optional-catch-binding";
import syntaxOptionalChaining from "@babel/plugin-syntax-optional-chaining";
import syntaxPrivatePropertyInObject from "@babel/plugin-syntax-private-property-in-object";
import syntaxTopLevelAwait from "@babel/plugin-syntax-top-level-await";

import proposalAsyncGeneratorFunctions from "@babel/plugin-transform-async-generator-functions";
import proposalClassProperties from "@babel/plugin-transform-class-properties";
import proposalClassStaticBlock from "@babel/plugin-transform-class-static-block";
Expand Down Expand Up @@ -77,7 +63,8 @@ import bugfixSafariForShadowing from "@babel/preset-modules/lib/plugins/transfor
import bugfixSafariIdDestructuringCollisionInFunctionExpression from "@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression";
import bugfixV8SpreadParametersInOptionalChaining from "@babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining";

export default {
export { availablePlugins as default };
const availablePlugins = {
"bugfix/transform-async-arrows-in-class": () => bugfixAsyncArrowsInClass,
"bugfix/transform-edge-default-parameters": () => bugfixEdgeDefaultParameters,
"bugfix/transform-edge-function-name": () => bugfixEdgeFunctionName,
Expand All @@ -88,37 +75,8 @@ export default {
"bugfix/transform-tagged-template-caching": () => bugfixTaggedTemplateCaching,
"bugfix/transform-v8-spread-parameters-in-optional-chaining": () =>
bugfixV8SpreadParametersInOptionalChaining,
"syntax-async-generators": () => syntaxAsyncGenerators,
"syntax-class-properties": () => syntaxClassProperties,
"syntax-class-static-block": () => syntaxClassStaticBlock,
"syntax-dynamic-import": () => syntaxDynamicImport,
"syntax-export-namespace-from": () => syntaxExportNamespaceFrom,
"syntax-import-assertions": () => syntaxImportAssertions,
"syntax-import-attributes": () => syntaxImportAttributes,
"syntax-import-meta": () => syntaxImportMeta,
"syntax-json-strings": () => syntaxJsonStrings,
"syntax-logical-assignment-operators": () => syntaxLogicalAssignmentOperators,
"syntax-nullish-coalescing-operator": () => syntaxNullishCoalescingOperator,
"syntax-numeric-separator": () => syntaxNumericSeparator,
"syntax-object-rest-spread": () => syntaxObjectRestSpread,
"syntax-optional-catch-binding": () => syntaxOptionalCatchBinding,
"syntax-optional-chaining": () => syntaxOptionalChaining,
"syntax-private-property-in-object": () => syntaxPrivatePropertyInObject,
"syntax-top-level-await": () => syntaxTopLevelAwait,
// This is a CJS plugin that depends on a package from the monorepo, so it
// breaks using ESM. Given that ESM builds are new enough to have this
// syntax enabled by default, we can safely skip enabling it.
"syntax-unicode-sets-regex": USE_ESM
? null
: // We cannot use the require call when bundling, because this is an ESM file.
// Babel standalone uses a modern parser, so just include a known noop plugin.
// Use `bind` so that it's not detected as a duplicate plugin when using it
// together with the TLA
IS_STANDALONE
? // @ts-expect-error syntaxTopLevelAwait is a function when bundled
() => syntaxTopLevelAwait.bind()
: // eslint-disable-next-line no-restricted-globals
() => require("@babel/plugin-syntax-unicode-sets-regex"),
"transform-arrow-functions": () => transformArrowFunctions,
"transform-async-generator-functions": () => proposalAsyncGeneratorFunctions,
"transform-async-to-generator": () => transformAsyncToGenerator,
Expand Down Expand Up @@ -173,10 +131,50 @@ export default {
"transform-unicode-sets-regex": () => transformUnicodeSetsRegex,
};

export const minVersions = {
"bugfix/transform-safari-id-destructuring-collision-in-function-expression":
"7.16.0",
"syntax-import-attributes": "7.22.0",
"transform-class-static-block": "7.12.0",
"transform-private-property-in-object": "7.10.0",
};
export const minVersions = {};

if (!process.env.BABEL_8_BREAKING) {
Object.assign(minVersions, {
"bugfix/transform-safari-id-destructuring-collision-in-function-expression":
"7.16.0",
"syntax-import-attributes": "7.22.0",
"transform-class-static-block": "7.12.0",
"transform-private-property-in-object": "7.10.0",
});

const emptyPlugin = () => ({});

const babel7SyntaxPlugin = (name: string) => {
// We cannot use the require call in ESM and when bundling.
// Babel standalone uses a modern parser, so just include a noop plugin.
// Use `bind` so that it's not detected as a duplicate plugin when using it.
// @ts-expect-error key not recognized in the object
availablePlugins[`syntax-${name}`] = USE_ESM
? () => emptyPlugin.bind(null)
: IS_STANDALONE
? () => emptyPlugin.bind(null)
: // eslint-disable-next-line no-restricted-globals
() => require(`@babel/plugin-syntax-${name}`);
};

babel7SyntaxPlugin("async-generators");
babel7SyntaxPlugin("class-properties");
babel7SyntaxPlugin("class-static-block");
babel7SyntaxPlugin("dynamic-import");
babel7SyntaxPlugin("export-namespace-from");
babel7SyntaxPlugin("import-meta");
babel7SyntaxPlugin("json-strings");
babel7SyntaxPlugin("logical-assignment-operators");
babel7SyntaxPlugin("nullish-coalescing-operator");
babel7SyntaxPlugin("numeric-separator");
babel7SyntaxPlugin("object-rest-spread");
babel7SyntaxPlugin("optional-catch-binding");
babel7SyntaxPlugin("optional-chaining");
babel7SyntaxPlugin("private-property-in-object");
babel7SyntaxPlugin("top-level-await");

// This is a CJS plugin that depends on a package from the monorepo, so it
// breaks using ESM. Given that ESM builds are new enough to have this
// syntax enabled by default, we can safely skip enabling it.
if (!USE_ESM) babel7SyntaxPlugin("unicode-sets-regex");
}
43 changes: 15 additions & 28 deletions packages/babel-preset-env/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,53 +126,47 @@ export const getModulesPluginNames = ({
shouldTransformESM,
shouldTransformDynamicImport,
shouldTransformExportNamespaceFrom,
shouldParseTopLevelAwait,
}: {
modules: ModuleOption;
transformations: ModuleTransformationsType;
shouldTransformESM: boolean;
shouldTransformDynamicImport: boolean;
shouldTransformExportNamespaceFrom: boolean;
shouldParseTopLevelAwait: boolean;
}) => {
const modulesPluginNames = [];
if (modules !== false && transformations[modules]) {
if (shouldTransformESM) {
modulesPluginNames.push(transformations[modules]);
}

if (
shouldTransformDynamicImport &&
shouldTransformESM &&
modules !== "umd"
) {
modulesPluginNames.push("transform-dynamic-import");
} else {
if (shouldTransformDynamicImport) {
if (shouldTransformDynamicImport) {
if (shouldTransformESM && modules !== "umd") {
modulesPluginNames.push("transform-dynamic-import");
} else {
console.warn(
"Dynamic import can only be supported when transforming ES modules" +
" to AMD, CommonJS or SystemJS. Only the parser plugin will be enabled.",
"Dynamic import can only be transformed when transforming ES" +
" modules to AMD, CommonJS or SystemJS.",
);
}
modulesPluginNames.push("syntax-dynamic-import");
}
} else {
modulesPluginNames.push("syntax-dynamic-import");
}

if (shouldTransformExportNamespaceFrom) {
modulesPluginNames.push("transform-export-namespace-from");
} else {
modulesPluginNames.push("syntax-export-namespace-from");
}

if (shouldParseTopLevelAwait) {
if (!process.env.BABEL_8_BREAKING) {
// Enable module-related syntax plugins for older Babel versions
if (!shouldTransformDynamicImport) {
modulesPluginNames.push("syntax-dynamic-import");
}
if (!shouldTransformExportNamespaceFrom) {
modulesPluginNames.push("syntax-export-namespace-from");
}
modulesPluginNames.push("syntax-top-level-await");
modulesPluginNames.push("syntax-import-meta");
}

// Enable import meta for @babel/core < 7.10
modulesPluginNames.push("syntax-import-meta");

return modulesPluginNames;
};

Expand Down Expand Up @@ -288,11 +282,6 @@ function supportsExportNamespaceFrom(caller: CallerMetadata | undefined) {
return !!caller?.supportsExportNamespaceFrom;
}

function supportsTopLevelAwait(caller: CallerMetadata | undefined) {
// @ts-expect-error supportsTopLevelAwait is not defined in CallerMetadata
return !!caller?.supportsTopLevelAwait;
}

export default declarePreset((api, opts: Options) => {
api.assertVersion(7);

Expand Down Expand Up @@ -382,8 +371,6 @@ option \`forceAllTransforms: true\` instead.
shouldTransformDynamicImport:
modules !== "auto" || !api.caller?.(supportsDynamicImport),
shouldTransformExportNamespaceFrom: !shouldSkipExportNamespaceFrom,
shouldParseTopLevelAwait:
!api.caller || (api.caller(supportsTopLevelAwait) as boolean),
});

const pluginNames = filterItems(
Expand Down
36 changes: 20 additions & 16 deletions packages/babel-preset-env/src/shipped-proposals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,26 @@ const proposalSyntaxPlugins = [
] as const;

// use intermediary object to enforce alphabetical key order
const pluginSyntaxObject = {
"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
"transform-private-methods": "syntax-class-properties",
"transform-private-property-in-object": "syntax-private-property-in-object",
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
"transform-unicode-property-regex": null as null,
} as const;
const pluginSyntaxObject = process.env.BABEL_8_BREAKING
? {}
: ({
"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
"transform-private-methods": "syntax-class-properties",
"transform-private-property-in-object":
"syntax-private-property-in-object",
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
"transform-unicode-property-regex": null as null,
} as const);

type PluginSyntaxObjectKeys = keyof typeof pluginSyntaxObject;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Using plugins:
bugfix/transform-tagged-template-caching { ios < 13, safari < 13 }
transform-modules-commonjs
transform-dynamic-import
syntax-top-level-await
syntax-import-meta

Using polyfills: No polyfills were added, since the `useBuiltIns` option was not set.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Using plugins:
transform-export-namespace-from { android < 72, chrome < 72, edge < 79, firefox < 80, ios < 14.5, opera < 60, safari < 14.1, samsung < 11.0 }
transform-modules-commonjs
transform-dynamic-import
syntax-top-level-await
syntax-import-meta

Using polyfills: No polyfills were added, since the `useBuiltIns` option was not set.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Using plugins:
transform-export-namespace-from { chrome < 72 }
transform-modules-commonjs
transform-dynamic-import
syntax-top-level-await
syntax-import-meta

Using polyfills: No polyfills were added, since the `useBuiltIns` option was not set.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Using plugins:
transform-export-namespace-from { chrome < 72 }
transform-modules-commonjs
transform-dynamic-import
syntax-top-level-await
syntax-import-meta

Using polyfills: No polyfills were added, since the `useBuiltIns` option was not set.
Loading

0 comments on commit 6b91b9b

Please sign in to comment.