forked from babel/babel
-
Notifications
You must be signed in to change notification settings - Fork 3
/
yarn.config.cjs
403 lines (379 loc) · 14.1 KB
/
yarn.config.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// @ts-check
/// <reference lib="es2015" />
/**
* @typedef {import('@yarnpkg/types').Yarn.Constraints.Context} Context
* */
const babel7plugins_babel8core = new Set(
require("./test/babel-7-8-compat/data.json")["babel7plugins-babel8core"]
);
/**
* Enforces that all workspaces depend on other workspaces using `workspace:^`
*
gen_enforced_dependency(WorkspaceCwd, DependencyIdent, 'workspace:^', DependencyType) :-
workspace_has_dependency(WorkspaceCwd, DependencyIdent, DependencyRange, DependencyType),
% Only consider dependency ranges that start with 'workspace:'
atom_concat('workspace:', _, DependencyRange),
% Only consider 'dependencies' and 'devDependencies'
(DependencyType = 'dependencies'; DependencyType = 'devDependencies').
* @param {Context} context
*/
function enforceWorkspaceDependencies({ Yarn }) {
for (const dependency of Yarn.dependencies()) {
if (
dependency.type === "dependencies" ||
dependency.type === "devDependencies"
) {
if (/^workspace:(?!\^$)/.test(dependency.range)) {
dependency.update("workspace:^");
}
}
}
}
/**
* @param {Context} context
*/
function enforcePackageInfo({ Yarn }) {
for (const workspace of Yarn.workspaces()) {
const info = {
license: "MIT",
repository: {
type: "git",
url: "https://github.com/babel/babel.git",
directory: workspace.cwd,
},
publishConfig: {
access: "public",
},
author: "The Babel Team (https://babel.dev/team)",
};
for (const key of Object.keys(info)) {
if (workspace.manifest.private) {
workspace.unset(key);
} else {
workspace.set(key, info[key]);
}
}
}
}
/**
* Enforces the engines.node field for all workspaces
gen_enforced_field(WorkspaceCwd, 'engines.node', '>=6.9.0') :-
\+ workspace_field(WorkspaceCwd, 'private', true),
% Get the workspace name
workspace_ident(WorkspaceCwd, WorkspaceIdent),
% Exempt from the rule as it supports '>=6.0.0'. TODO: remove with the next major
WorkspaceIdent \= '@babel/parser',
% Skip '@babel/eslint*' workspaces. TODO: remove with the next major
\+ atom_concat('@babel/eslint', _, WorkspaceIdent).
% Enforces the engines.node field for '@babel/eslint*' workspaces
gen_enforced_field(WorkspaceCwd, 'engines.node', '^10.13.0 || ^12.13.0 || >=14.0.0') :-
\+ workspace_field(WorkspaceCwd, 'private', true),
% Get the workspace name
workspace_ident(WorkspaceCwd, WorkspaceIdent),
% Only target '@babel/eslint*' workspaces
atom_concat('@babel/eslint', _, WorkspaceIdent).
% (Babel 8) Enforces the engines.node field for all workspaces except private ones
gen_enforced_field(WorkspaceCwd, 'conditions.BABEL_8_BREAKING.0.engines.node', '^16.20.0 || ^18.16.0 || >=20.0.0') :-
\+ workspace_field(WorkspaceCwd, 'private', true).
% Removes the 'engines.node' field from private workspaces
gen_enforced_field(WorkspaceCwd, 'engines.node', null) :-
workspace_field(WorkspaceCwd, 'private', true).
* @param {Context} context
*/
function enforceEnginesNodeForPublicUnsetForPrivate({ Yarn }) {
for (const workspace of Yarn.workspaces()) {
if (workspace.manifest.private) {
workspace.unset("engines.node");
} else {
if (!workspace.manifest.conditions?.BABEL_8_BREAKING?.[0].private) {
workspace.set(
"conditions.BABEL_8_BREAKING.0.engines.node",
"^18.20.0 || ^20.10.0 || >=21.0.0"
);
}
if (workspace.ident === "@babel/parser") continue;
if (workspace.ident?.startsWith("@babel/eslint")) {
workspace.set("engines.node", "^10.13.0 || ^12.13.0 || >=14.0.0");
} else {
workspace.set("engines.node", ">=6.9.0");
}
}
}
}
/**
* Ensure that the BABEL_8_BREAKING condition has both 'yes' and 'no' cases
gen_enforced_field(WorkspaceCwd, 'conditions.BABEL_8_BREAKING.1', {}) :-
workspace_field(WorkspaceCwd, 'conditions.BABEL_8_BREAKING.0', _),
\+ workspace_field(WorkspaceCwd, 'conditions.BABEL_8_BREAKING.1', _).
* @param {Context} context
*/
function enforceBothConditionsSetForBABEL_8_BREAKING({ Yarn }) {
for (const workspace of Yarn.workspaces()) {
const condition = workspace.manifest.conditions?.BABEL_8_BREAKING;
if (condition) {
if (!Array.isArray(condition)) {
workspace.error("The BABEL_8_BREAKING condition must be an array");
} else if (condition.length === 1) {
workspace.set("conditions.BABEL_8_BREAKING.1", {});
}
}
}
}
/**
* Enforces the main and types field to start with ./
gen_enforced_field(WorkspaceCwd, FieldName, ExpectedValue) :-
% Fields the rule applies to
member(FieldName, ['main', 'types']),
% Get current value
workspace_field(WorkspaceCwd, FieldName, CurrentValue),
% Must not start with ./ already
\+ atom_concat('./', _, CurrentValue),
% Store './' + CurrentValue in ExpectedValue
atom_concat('./', CurrentValue, ExpectedValue).
* @param {Context} context
*/
function enforceMainAndTypes({ Yarn }) {
for (const workspace of Yarn.workspaces()) {
const { main: currentMain, types: currentTypes } = workspace.manifest;
if (currentMain && !currentMain.startsWith("./")) {
workspace.set("main", "./" + currentMain);
}
if (currentTypes && !currentTypes?.startsWith("./")) {
workspace.set("types", "./" + currentTypes);
}
}
}
/**
* Enforces the type field to be set
gen_enforced_field(WorkspaceCwd, 'type', 'commonjs') :-
\+ workspace_field(WorkspaceCwd, 'type', 'module').
* @param {Context} context
*/
function enforceType({ Yarn }) {
for (const workspace of Yarn.workspaces()) {
if (!workspace.manifest.private && workspace.manifest.type !== "commonjs") {
workspace.set("type", "module");
}
}
}
/**
* Enforces that @babel/runtime-corejs2 must depend on core-js 2
gen_enforced_dependency(WorkspaceCwd, 'core-js', '^2.6.12', 'dependencies') :-
% Get the workspace name
% The rule works for @babel/runtime-corejs2 only
workspace_ident(WorkspaceCwd, '@babel/runtime-corejs2').
* @param {Context} context
*/
function enforceRuntimeCorejs2DependsOnCorejs2({ Yarn }) {
const workspace =
Yarn.workspace({ ident: "@babel/runtime-corejs2" }) ?? undefined;
const dep = Yarn.dependency({ workspace, ident: "core-js" });
if (dep === null) {
workspace?.error("@babel/runtime-corejs2 must depend on core-js");
return;
}
dep.update("^2.6.12");
}
/**
* Enforces that a dependency doesn't appear in both `dependencies` and `devDependencies`
gen_enforced_dependency(WorkspaceCwd, DependencyIdent, null, 'devDependencies') :-
workspace_has_dependency(WorkspaceCwd, DependencyIdent, _, 'devDependencies'),
workspace_has_dependency(WorkspaceCwd, DependencyIdent, _, 'dependencies').
* @param {Context} context
*/
function enforceNoDualTypeDependencies({ Yarn }) {
for (const dependency of Yarn.dependencies({ type: "devDependencies" })) {
if (
// TODO(Babel 8): Remove this check
// We use conditions to remove the dependency, but we still need it as a devDependency
dependency.workspace.ident === "@babel/plugin-transform-runtime" &&
dependency.ident === "babel-plugin-polyfill-corejs3"
) {
continue;
}
const otherDependency = Yarn.dependency({
workspace: dependency.workspace,
ident: dependency.ident,
type: "dependencies",
});
if (otherDependency !== null) {
dependency.delete();
}
}
}
/**
* Enforces a default 'conditions', unless it's already specified
gen_enforced_field(WorkspaceCwd, 'conditions', '{ "USE_ESM": [{ "type": "module" }, null] }') :-
\+ workspace_field(WorkspaceCwd, 'private', true),
\+ workspace_field(WorkspaceCwd, 'conditions', _),
\+ workspace_field(WorkspaceCwd, 'main', './lib/index.cjs'),
% Exclude some packages
workspace_ident(WorkspaceCwd, WorkspaceIdent),
WorkspaceIdent \= '@babel/compat-data'.
* @param {Context} context
*/
function enforceConditions({ Yarn }) {
for (const workspace of Yarn.workspaces()) {
if (workspace.manifest.private) continue;
if (workspace.manifest.main === "./lib/index.cjs") continue;
if (workspace.ident === "@babel/compat-data") continue;
if (workspace.manifest.type !== "commonjs") {
if (!workspace.manifest.conditions) {
workspace.set("conditions", { USE_ESM: [null, { type: "commonjs" }] });
} else if (!workspace.manifest.conditions.USE_ESM) {
workspace.set("conditions.USE_ESM", [null, { type: "commonjs" }]);
} else {
workspace.set("conditions.USE_ESM.1", { type: "commonjs" });
}
}
}
}
/**
* Enforces that @babel/helper-* must not depend on @babel/traverse, @babel/template, @babel/types if they peer-depend on @babel/core
gen_enforced_dependency(WorkspaceCwd, DependencyIdent, null, 'dependencies') :-
% Get the workspace name
workspace_ident(WorkspaceCwd, WorkspaceIdent),
atom_concat('@babel/helper-', _, WorkspaceIdent),
workspace_has_dependency(WorkspaceCwd, '@babel/core', _, 'peerDependencies'),
member(DependencyIdent, ['@babel/template', '@babel/traverse', '@babel/types']).
* @param {Context} context
*/
function enforceBabelHelperBabelDeps({ Yarn }) {
for (const workspace of Yarn.workspaces()) {
if (workspace.ident?.startsWith("@babel/helper-")) {
if (
workspace.pkg.peerDependencies.has("@babel/core") &&
// In some cases, we remove peerDependencies for Babel 7
workspace.manifest.conditions?.BABEL_8_BREAKING?.[1]
?.peerDependencies !== null
) {
workspace.unset("dependencies['@babel/traverse']");
workspace.unset("dependencies['@babel/template']");
workspace.unset("dependencies['@babel/types']");
}
}
}
}
/**
* Enforces that @babel/core must not be in dependency for most packages
gen_enforced_dependency(WorkspaceCwd, '@babel/core', null, 'dependencies') :-
% Get the workspace name
workspace_ident(WorkspaceCwd, WorkspaceIdent),
% Exclude some packages
\+ member(WorkspaceIdent, ['@babel/eslint-shared-fixtures', '@babel/eslint-tests', '@babel/helper-transform-fixture-test-runner']).
* Enforces that @babel/core should be in devDependencies if a package peer-depends on @babel/core and it does not list @babel/core in dependencies. Doing so will ensure that they are linked to an ESM @babel/core build in the e2e ESM tests.
gen_enforced_dependency(WorkspaceCwd, '@babel/core', 'workspace:^', 'devDependencies') :-
workspace_has_dependency(WorkspaceCwd, '@babel/core', _, 'peerDependencies'),
\+ workspace_has_dependency(WorkspaceCwd, '@babel/core', _, 'dependencies').
* @param {Context} context
*/
function enforceBabelCoreNotInDeps({ Yarn }) {
for (const workspace of Yarn.workspaces()) {
if (
workspace.pkg.peerDependencies.has("@babel/core") &&
!workspace.manifest.dependencies?.["@babel/core"]
) {
workspace.set("devDependencies['@babel/core']", "workspace:^");
}
if (
workspace.ident !== null &&
[
"@babel/eslint-shared-fixtures",
"@babel/eslint-tests",
"@babel/helper-transform-fixture-test-runner",
].includes(workspace.ident)
) {
continue;
}
workspace.unset("dependencies['@babel/core']");
}
}
/**
* Enforces `exports` to be consistent
*
gen_enforced_field(WorkspaceCwd, 'exports', '{ ".": "./lib/index.js", "./package.json": "./package.json" }') :-
\+ workspace_field(WorkspaceCwd, 'private', true),
% Exclude packages with more complex `exports`
workspace_ident(WorkspaceCwd, WorkspaceIdent),
WorkspaceIdent \= '@babel/compat-data',
WorkspaceIdent \= '@babel/helper-plugin-test-runner', % TODO: Remove in Babel 8
WorkspaceIdent \= '@babel/core', % TODO: Remove in Babel 8
WorkspaceIdent \= '@babel/parser',
WorkspaceIdent \= '@babel/plugin-transform-react-jsx', % TODO: Remove in Babel 8
WorkspaceIdent \= '@babel/standalone',
WorkspaceIdent \= '@babel/types', % @babel/types has types exports
\+ atom_concat('@babel/eslint-', _, WorkspaceIdent),
\+ atom_concat('@babel/runtime', _, WorkspaceIdent).
* @param {Context} context
*/
function enforceExports({ Yarn }) {
for (const workspace of Yarn.workspaces()) {
if (workspace.manifest.private) continue;
// Exclude packages with more complex `exports`
const packageName = workspace.pkg.ident;
if (
[
"@babel/compat-data",
"@babel/helper-plugin-test-runner", // TODO: Remove in Babel 8
"@babel/core", // TODO: Remove in Babel 8
"@babel/parser",
"@babel/plugin-transform-react-jsx", // TODO: Remove in Babel 8
"@babel/standalone",
"@babel/types", // @babel/types has types exports
].includes(packageName) ||
packageName.startsWith("@babel/eslint-") ||
packageName.startsWith("@babel/runtime")
) {
continue;
}
workspace.set("exports", {
".": {
types: "./lib/index.d.ts",
default: "./lib/index.js",
},
"./package.json": "./package.json",
});
}
}
function enforceBabelCodeVersionFor78Compat({ Yarn }, version) {
for (const workspace of Yarn.workspaces()) {
if (workspace.cwd === ".") {
workspace.set("devDependencies['@babel/core']", version);
} else if (
workspace.ident === "@babel/helper-transform-fixture-test-runner"
) {
workspace.set("dependencies['@babel/core']", version);
} else if (
workspace.ident &&
babel7plugins_babel8core.has(workspace.ident.replace("@babel/", "babel-"))
) {
workspace.set("devDependencies['@babel/core']", version);
}
}
}
/**
* @type {import('@yarnpkg/types').Yarn.Config}
*/
module.exports = {
constraints: async ctx => {
enforceWorkspaceDependencies(ctx);
enforcePackageInfo(ctx);
enforceEnginesNodeForPublicUnsetForPrivate(ctx);
enforceBothConditionsSetForBABEL_8_BREAKING(ctx);
enforceMainAndTypes(ctx);
enforceType(ctx);
enforceExports(ctx);
enforceConditions(ctx);
enforceNoDualTypeDependencies(ctx);
enforceRuntimeCorejs2DependsOnCorejs2(ctx);
enforceBabelHelperBabelDeps(ctx);
if (process.env.BABEL_CORE_DEV_DEP_VERSION) {
enforceBabelCodeVersionFor78Compat(
ctx,
process.env.BABEL_CORE_DEV_DEP_VERSION
);
} else {
enforceBabelCoreNotInDeps(ctx);
}
},
};