Skip to content

Commit 6b91b9b

Browse files
[babel 8] Remove default syntax plugins from preset-env (#15810)
1 parent 7a3e18a commit 6b91b9b

133 files changed

Lines changed: 534 additions & 254 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.yarnrc.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,43 @@ packageExtensions:
4646
# It doesn't support Prettier 3 yet
4747
prettier: "^2.0.0"
4848

49+
# @babel/preset-env depends on all these packages using the condition:
50+
# protocol, which does not properly propagate peer dependency requirements.
51+
# We know that we have the correct Babel version in the monorepo, so let's
52+
# just remove the peerDependency requirement to silence the warning.
53+
"@babel/plugin-proposal-private-property-in-object@*":
54+
peerDependencies: { "@babel/core": null }
55+
"@babel/plugin-syntax-async-generators@*":
56+
peerDependencies: { "@babel/core": null }
57+
"@babel/plugin-syntax-class-properties@*":
58+
peerDependencies: { "@babel/core": null }
59+
"@babel/plugin-syntax-class-static-block@*":
60+
peerDependencies: { "@babel/core": null }
61+
"@babel/plugin-syntax-dynamic-import@*":
62+
peerDependencies: { "@babel/core": null }
63+
"@babel/plugin-syntax-export-namespace-from@*":
64+
peerDependencies: { "@babel/core": null }
65+
"@babel/plugin-syntax-import-meta@*":
66+
peerDependencies: { "@babel/core": null }
67+
"@babel/plugin-syntax-json-strings@*":
68+
peerDependencies: { "@babel/core": null }
69+
"@babel/plugin-syntax-logical-assignment-operators@*":
70+
peerDependencies: { "@babel/core": null }
71+
"@babel/plugin-syntax-nullish-coalescing-operator@*":
72+
peerDependencies: { "@babel/core": null }
73+
"@babel/plugin-syntax-numeric-separator@*":
74+
peerDependencies: { "@babel/core": null }
75+
"@babel/plugin-syntax-object-rest-spread@*":
76+
peerDependencies: { "@babel/core": null }
77+
"@babel/plugin-syntax-optional-catch-binding@*":
78+
peerDependencies: { "@babel/core": null }
79+
"@babel/plugin-syntax-optional-chaining@*":
80+
peerDependencies: { "@babel/core": null }
81+
"@babel/plugin-syntax-private-property-in-object@*":
82+
peerDependencies: { "@babel/core": null }
83+
"@babel/plugin-syntax-top-level-await@*":
84+
peerDependencies: { "@babel/core": null }
85+
"@babel/plugin-syntax-unicode-sets-regex@*":
86+
peerDependencies: { "@babel/core": null }
87+
4988
yarnPath: .yarn/releases/yarn-3.6.1.cjs

packages/babel-helper-transform-fixture-test-runner/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,15 @@ async function run(task: Test) {
367367
validateFile(
368368
normalizeOutput(actualLogs.stdout, normalizationOpts),
369369
stdout.loc,
370-
stdout.code,
370+
process.env.BABEL_8_BREAKING
371+
? // In Babel 8, preset-env does not enable all the unnecessary syntax
372+
// plugins. For simplicity, just strip them fro the expected output
373+
// so that we do not need to separate tests for every fixture.
374+
stdout.code.replace(
375+
/\n\s*syntax-(?!import-attributes|import-assertions).*/g,
376+
"",
377+
)
378+
: stdout.code,
371379
);
372380
validateFile(
373381
normalizeOutput(actualLogs.stderr, normalizationOpts),

packages/babel-preset-env/package.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,24 @@
2323
"@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression": "workspace:^",
2424
"@babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining": "workspace:^",
2525
"@babel/plugin-proposal-private-property-in-object": "condition:BABEL_8_BREAKING ? : 7.21.0-placeholder-for-preset-env.2",
26-
"@babel/plugin-syntax-async-generators": "^7.8.4",
27-
"@babel/plugin-syntax-class-properties": "^7.12.13",
28-
"@babel/plugin-syntax-class-static-block": "^7.14.5",
29-
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
30-
"@babel/plugin-syntax-export-namespace-from": "^7.8.3",
26+
"@babel/plugin-syntax-async-generators": "condition:BABEL_8_BREAKING ? : ^7.8.4",
27+
"@babel/plugin-syntax-class-properties": "condition:BABEL_8_BREAKING ? : ^7.12.13",
28+
"@babel/plugin-syntax-class-static-block": "condition:BABEL_8_BREAKING ? : ^7.14.5",
29+
"@babel/plugin-syntax-dynamic-import": "condition:BABEL_8_BREAKING ? : ^7.8.3",
30+
"@babel/plugin-syntax-export-namespace-from": "condition:BABEL_8_BREAKING ? : ^7.8.3",
3131
"@babel/plugin-syntax-import-assertions": "workspace:^",
3232
"@babel/plugin-syntax-import-attributes": "workspace:^",
33-
"@babel/plugin-syntax-import-meta": "^7.10.4",
34-
"@babel/plugin-syntax-json-strings": "^7.8.3",
35-
"@babel/plugin-syntax-logical-assignment-operators": "^7.10.4",
36-
"@babel/plugin-syntax-nullish-coalescing-operator": "^7.8.3",
37-
"@babel/plugin-syntax-numeric-separator": "^7.10.4",
38-
"@babel/plugin-syntax-object-rest-spread": "^7.8.3",
39-
"@babel/plugin-syntax-optional-catch-binding": "^7.8.3",
40-
"@babel/plugin-syntax-optional-chaining": "^7.8.3",
41-
"@babel/plugin-syntax-private-property-in-object": "^7.14.5",
42-
"@babel/plugin-syntax-top-level-await": "^7.14.5",
43-
"@babel/plugin-syntax-unicode-sets-regex": "^7.18.6",
33+
"@babel/plugin-syntax-import-meta": "condition:BABEL_8_BREAKING ? : ^7.10.4",
34+
"@babel/plugin-syntax-json-strings": "condition:BABEL_8_BREAKING ? : ^7.8.3",
35+
"@babel/plugin-syntax-logical-assignment-operators": "condition:BABEL_8_BREAKING ? : ^7.10.4",
36+
"@babel/plugin-syntax-nullish-coalescing-operator": "condition:BABEL_8_BREAKING ? : ^7.8.3",
37+
"@babel/plugin-syntax-numeric-separator": "condition:BABEL_8_BREAKING ? : ^7.10.4",
38+
"@babel/plugin-syntax-object-rest-spread": "condition:BABEL_8_BREAKING ? : ^7.8.3",
39+
"@babel/plugin-syntax-optional-catch-binding": "condition:BABEL_8_BREAKING ? : ^7.8.3",
40+
"@babel/plugin-syntax-optional-chaining": "condition:BABEL_8_BREAKING ? : ^7.8.3",
41+
"@babel/plugin-syntax-private-property-in-object": "condition:BABEL_8_BREAKING ? : ^7.14.5",
42+
"@babel/plugin-syntax-top-level-await": "condition:BABEL_8_BREAKING ? : ^7.14.5",
43+
"@babel/plugin-syntax-unicode-sets-regex": "condition:BABEL_8_BREAKING ? : ^7.18.6",
4444
"@babel/plugin-transform-arrow-functions": "workspace:^",
4545
"@babel/plugin-transform-async-generator-functions": "workspace:^",
4646
"@babel/plugin-transform-async-to-generator": "workspace:^",

packages/babel-preset-env/src/available-plugins.ts

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,9 @@
22

33
declare const USE_ESM: boolean;
44

5-
import syntaxAsyncGenerators from "@babel/plugin-syntax-async-generators";
6-
import syntaxClassProperties from "@babel/plugin-syntax-class-properties";
7-
import syntaxClassStaticBlock from "@babel/plugin-syntax-class-static-block";
8-
import syntaxDynamicImport from "@babel/plugin-syntax-dynamic-import";
9-
import syntaxExportNamespaceFrom from "@babel/plugin-syntax-export-namespace-from";
105
import syntaxImportAssertions from "@babel/plugin-syntax-import-assertions";
116
import syntaxImportAttributes from "@babel/plugin-syntax-import-attributes";
12-
import syntaxImportMeta from "@babel/plugin-syntax-import-meta";
13-
import syntaxJsonStrings from "@babel/plugin-syntax-json-strings";
14-
import syntaxLogicalAssignmentOperators from "@babel/plugin-syntax-logical-assignment-operators";
15-
import syntaxNullishCoalescingOperator from "@babel/plugin-syntax-nullish-coalescing-operator";
16-
import syntaxNumericSeparator from "@babel/plugin-syntax-numeric-separator";
17-
import syntaxObjectRestSpread from "@babel/plugin-syntax-object-rest-spread";
18-
import syntaxOptionalCatchBinding from "@babel/plugin-syntax-optional-catch-binding";
19-
import syntaxOptionalChaining from "@babel/plugin-syntax-optional-chaining";
20-
import syntaxPrivatePropertyInObject from "@babel/plugin-syntax-private-property-in-object";
21-
import syntaxTopLevelAwait from "@babel/plugin-syntax-top-level-await";
7+
228
import proposalAsyncGeneratorFunctions from "@babel/plugin-transform-async-generator-functions";
239
import proposalClassProperties from "@babel/plugin-transform-class-properties";
2410
import proposalClassStaticBlock from "@babel/plugin-transform-class-static-block";
@@ -77,7 +63,8 @@ import bugfixSafariForShadowing from "@babel/preset-modules/lib/plugins/transfor
7763
import bugfixSafariIdDestructuringCollisionInFunctionExpression from "@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression";
7864
import bugfixV8SpreadParametersInOptionalChaining from "@babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining";
7965

80-
export default {
66+
export { availablePlugins as default };
67+
const availablePlugins = {
8168
"bugfix/transform-async-arrows-in-class": () => bugfixAsyncArrowsInClass,
8269
"bugfix/transform-edge-default-parameters": () => bugfixEdgeDefaultParameters,
8370
"bugfix/transform-edge-function-name": () => bugfixEdgeFunctionName,
@@ -88,37 +75,8 @@ export default {
8875
"bugfix/transform-tagged-template-caching": () => bugfixTaggedTemplateCaching,
8976
"bugfix/transform-v8-spread-parameters-in-optional-chaining": () =>
9077
bugfixV8SpreadParametersInOptionalChaining,
91-
"syntax-async-generators": () => syntaxAsyncGenerators,
92-
"syntax-class-properties": () => syntaxClassProperties,
93-
"syntax-class-static-block": () => syntaxClassStaticBlock,
94-
"syntax-dynamic-import": () => syntaxDynamicImport,
95-
"syntax-export-namespace-from": () => syntaxExportNamespaceFrom,
9678
"syntax-import-assertions": () => syntaxImportAssertions,
9779
"syntax-import-attributes": () => syntaxImportAttributes,
98-
"syntax-import-meta": () => syntaxImportMeta,
99-
"syntax-json-strings": () => syntaxJsonStrings,
100-
"syntax-logical-assignment-operators": () => syntaxLogicalAssignmentOperators,
101-
"syntax-nullish-coalescing-operator": () => syntaxNullishCoalescingOperator,
102-
"syntax-numeric-separator": () => syntaxNumericSeparator,
103-
"syntax-object-rest-spread": () => syntaxObjectRestSpread,
104-
"syntax-optional-catch-binding": () => syntaxOptionalCatchBinding,
105-
"syntax-optional-chaining": () => syntaxOptionalChaining,
106-
"syntax-private-property-in-object": () => syntaxPrivatePropertyInObject,
107-
"syntax-top-level-await": () => syntaxTopLevelAwait,
108-
// This is a CJS plugin that depends on a package from the monorepo, so it
109-
// breaks using ESM. Given that ESM builds are new enough to have this
110-
// syntax enabled by default, we can safely skip enabling it.
111-
"syntax-unicode-sets-regex": USE_ESM
112-
? null
113-
: // We cannot use the require call when bundling, because this is an ESM file.
114-
// Babel standalone uses a modern parser, so just include a known noop plugin.
115-
// Use `bind` so that it's not detected as a duplicate plugin when using it
116-
// together with the TLA
117-
IS_STANDALONE
118-
? // @ts-expect-error syntaxTopLevelAwait is a function when bundled
119-
() => syntaxTopLevelAwait.bind()
120-
: // eslint-disable-next-line no-restricted-globals
121-
() => require("@babel/plugin-syntax-unicode-sets-regex"),
12280
"transform-arrow-functions": () => transformArrowFunctions,
12381
"transform-async-generator-functions": () => proposalAsyncGeneratorFunctions,
12482
"transform-async-to-generator": () => transformAsyncToGenerator,
@@ -173,10 +131,50 @@ export default {
173131
"transform-unicode-sets-regex": () => transformUnicodeSetsRegex,
174132
};
175133

176-
export const minVersions = {
177-
"bugfix/transform-safari-id-destructuring-collision-in-function-expression":
178-
"7.16.0",
179-
"syntax-import-attributes": "7.22.0",
180-
"transform-class-static-block": "7.12.0",
181-
"transform-private-property-in-object": "7.10.0",
182-
};
134+
export const minVersions = {};
135+
136+
if (!process.env.BABEL_8_BREAKING) {
137+
Object.assign(minVersions, {
138+
"bugfix/transform-safari-id-destructuring-collision-in-function-expression":
139+
"7.16.0",
140+
"syntax-import-attributes": "7.22.0",
141+
"transform-class-static-block": "7.12.0",
142+
"transform-private-property-in-object": "7.10.0",
143+
});
144+
145+
const emptyPlugin = () => ({});
146+
147+
const babel7SyntaxPlugin = (name: string) => {
148+
// We cannot use the require call in ESM and when bundling.
149+
// Babel standalone uses a modern parser, so just include a noop plugin.
150+
// Use `bind` so that it's not detected as a duplicate plugin when using it.
151+
// @ts-expect-error key not recognized in the object
152+
availablePlugins[`syntax-${name}`] = USE_ESM
153+
? () => emptyPlugin.bind(null)
154+
: IS_STANDALONE
155+
? () => emptyPlugin.bind(null)
156+
: // eslint-disable-next-line no-restricted-globals
157+
() => require(`@babel/plugin-syntax-${name}`);
158+
};
159+
160+
babel7SyntaxPlugin("async-generators");
161+
babel7SyntaxPlugin("class-properties");
162+
babel7SyntaxPlugin("class-static-block");
163+
babel7SyntaxPlugin("dynamic-import");
164+
babel7SyntaxPlugin("export-namespace-from");
165+
babel7SyntaxPlugin("import-meta");
166+
babel7SyntaxPlugin("json-strings");
167+
babel7SyntaxPlugin("logical-assignment-operators");
168+
babel7SyntaxPlugin("nullish-coalescing-operator");
169+
babel7SyntaxPlugin("numeric-separator");
170+
babel7SyntaxPlugin("object-rest-spread");
171+
babel7SyntaxPlugin("optional-catch-binding");
172+
babel7SyntaxPlugin("optional-chaining");
173+
babel7SyntaxPlugin("private-property-in-object");
174+
babel7SyntaxPlugin("top-level-await");
175+
176+
// This is a CJS plugin that depends on a package from the monorepo, so it
177+
// breaks using ESM. Given that ESM builds are new enough to have this
178+
// syntax enabled by default, we can safely skip enabling it.
179+
if (!USE_ESM) babel7SyntaxPlugin("unicode-sets-regex");
180+
}

packages/babel-preset-env/src/index.ts

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -126,53 +126,47 @@ export const getModulesPluginNames = ({
126126
shouldTransformESM,
127127
shouldTransformDynamicImport,
128128
shouldTransformExportNamespaceFrom,
129-
shouldParseTopLevelAwait,
130129
}: {
131130
modules: ModuleOption;
132131
transformations: ModuleTransformationsType;
133132
shouldTransformESM: boolean;
134133
shouldTransformDynamicImport: boolean;
135134
shouldTransformExportNamespaceFrom: boolean;
136-
shouldParseTopLevelAwait: boolean;
137135
}) => {
138136
const modulesPluginNames = [];
139137
if (modules !== false && transformations[modules]) {
140138
if (shouldTransformESM) {
141139
modulesPluginNames.push(transformations[modules]);
142140
}
143141

144-
if (
145-
shouldTransformDynamicImport &&
146-
shouldTransformESM &&
147-
modules !== "umd"
148-
) {
149-
modulesPluginNames.push("transform-dynamic-import");
150-
} else {
151-
if (shouldTransformDynamicImport) {
142+
if (shouldTransformDynamicImport) {
143+
if (shouldTransformESM && modules !== "umd") {
144+
modulesPluginNames.push("transform-dynamic-import");
145+
} else {
152146
console.warn(
153-
"Dynamic import can only be supported when transforming ES modules" +
154-
" to AMD, CommonJS or SystemJS. Only the parser plugin will be enabled.",
147+
"Dynamic import can only be transformed when transforming ES" +
148+
" modules to AMD, CommonJS or SystemJS.",
155149
);
156150
}
157-
modulesPluginNames.push("syntax-dynamic-import");
158151
}
159-
} else {
160-
modulesPluginNames.push("syntax-dynamic-import");
161152
}
162153

163154
if (shouldTransformExportNamespaceFrom) {
164155
modulesPluginNames.push("transform-export-namespace-from");
165-
} else {
166-
modulesPluginNames.push("syntax-export-namespace-from");
167156
}
168157

169-
if (shouldParseTopLevelAwait) {
158+
if (!process.env.BABEL_8_BREAKING) {
159+
// Enable module-related syntax plugins for older Babel versions
160+
if (!shouldTransformDynamicImport) {
161+
modulesPluginNames.push("syntax-dynamic-import");
162+
}
163+
if (!shouldTransformExportNamespaceFrom) {
164+
modulesPluginNames.push("syntax-export-namespace-from");
165+
}
170166
modulesPluginNames.push("syntax-top-level-await");
167+
modulesPluginNames.push("syntax-import-meta");
171168
}
172169

173-
// Enable import meta for @babel/core < 7.10
174-
modulesPluginNames.push("syntax-import-meta");
175-
176170
return modulesPluginNames;
177171
};
178172

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

291-
function supportsTopLevelAwait(caller: CallerMetadata | undefined) {
292-
// @ts-expect-error supportsTopLevelAwait is not defined in CallerMetadata
293-
return !!caller?.supportsTopLevelAwait;
294-
}
295-
296285
export default declarePreset((api, opts: Options) => {
297286
api.assertVersion(7);
298287

@@ -382,8 +371,6 @@ option \`forceAllTransforms: true\` instead.
382371
shouldTransformDynamicImport:
383372
modules !== "auto" || !api.caller?.(supportsDynamicImport),
384373
shouldTransformExportNamespaceFrom: !shouldSkipExportNamespaceFrom,
385-
shouldParseTopLevelAwait:
386-
!api.caller || (api.caller(supportsTopLevelAwait) as boolean),
387374
});
388375

389376
const pluginNames = filterItems(

packages/babel-preset-env/src/shipped-proposals.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,26 @@ const proposalSyntaxPlugins = [
1313
] as const;
1414

1515
// use intermediary object to enforce alphabetical key order
16-
const pluginSyntaxObject = {
17-
"transform-async-generator-functions": "syntax-async-generators",
18-
"transform-class-properties": "syntax-class-properties",
19-
"transform-class-static-block": "syntax-class-static-block",
20-
"transform-json-strings": "syntax-json-strings",
21-
"transform-nullish-coalescing-operator": "syntax-nullish-coalescing-operator",
22-
"transform-numeric-separator": "syntax-numeric-separator",
23-
"transform-object-rest-spread": "syntax-object-rest-spread",
24-
"transform-optional-catch-binding": "syntax-optional-catch-binding",
25-
"transform-optional-chaining": "syntax-optional-chaining",
26-
// note: we don't have syntax-private-methods
27-
"transform-private-methods": "syntax-class-properties",
28-
"transform-private-property-in-object": "syntax-private-property-in-object",
29-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
30-
"transform-unicode-property-regex": null as null,
31-
} as const;
16+
const pluginSyntaxObject = process.env.BABEL_8_BREAKING
17+
? {}
18+
: ({
19+
"transform-async-generator-functions": "syntax-async-generators",
20+
"transform-class-properties": "syntax-class-properties",
21+
"transform-class-static-block": "syntax-class-static-block",
22+
"transform-json-strings": "syntax-json-strings",
23+
"transform-nullish-coalescing-operator":
24+
"syntax-nullish-coalescing-operator",
25+
"transform-numeric-separator": "syntax-numeric-separator",
26+
"transform-object-rest-spread": "syntax-object-rest-spread",
27+
"transform-optional-catch-binding": "syntax-optional-catch-binding",
28+
"transform-optional-chaining": "syntax-optional-chaining",
29+
// note: we don't have syntax-private-methods
30+
"transform-private-methods": "syntax-class-properties",
31+
"transform-private-property-in-object":
32+
"syntax-private-property-in-object",
33+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
34+
"transform-unicode-property-regex": null as null,
35+
} as const);
3236

3337
type PluginSyntaxObjectKeys = keyof typeof pluginSyntaxObject;
3438

packages/babel-preset-env/test/fixtures/bugfixes/_esmodules-babel-7/stdout.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Using plugins:
4444
bugfix/transform-tagged-template-caching { ios < 13, safari < 13 }
4545
transform-modules-commonjs
4646
transform-dynamic-import
47+
syntax-top-level-await
4748
syntax-import-meta
4849

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

packages/babel-preset-env/test/fixtures/bugfixes/_esmodules-no-bugfixes-babel-7/stdout.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Using plugins:
4242
transform-export-namespace-from { android < 72, chrome < 72, edge < 79, firefox < 80, ios < 14.5, opera < 60, safari < 14.1, samsung < 11.0 }
4343
transform-modules-commonjs
4444
transform-dynamic-import
45+
syntax-top-level-await
4546
syntax-import-meta
4647

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

packages/babel-preset-env/test/fixtures/bugfixes/edge-default-params-chrome-40/stdout.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Using plugins:
4747
transform-export-namespace-from { chrome < 72 }
4848
transform-modules-commonjs
4949
transform-dynamic-import
50+
syntax-top-level-await
5051
syntax-import-meta
5152

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

packages/babel-preset-env/test/fixtures/bugfixes/edge-default-params-chrome-70/stdout.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Using plugins:
2424
transform-export-namespace-from { chrome < 72 }
2525
transform-modules-commonjs
2626
transform-dynamic-import
27+
syntax-top-level-await
2728
syntax-import-meta
2829

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

0 commit comments

Comments
 (0)