forked from yarnpkg/berry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yarn.config.cjs
229 lines (193 loc) Β· 7.31 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
// @ts-check
/** @type {import('@yarnpkg/types')} */
const {defineConfig} = require(`@yarnpkg/types`);
/**
* @typedef {import('@yarnpkg/types').Yarn.Constraints.Context} Context
* @typedef {import('@yarnpkg/types').Yarn.Constraints.Workspace} Workspace
* @typedef {import('@yarnpkg/types').Yarn.Constraints.Dependency} Dependency
*/
const IGNORE_CONSISTENT_DEPENDENCIES_FOR = new Set([
`.`,
`packages/docusaurus`,
]);
/**
* This rule will enforce that a workspace MUST depend on the same version of a dependency as the one used by the other workspaces
* We allow Docusaurus to have different dependencies for now; will be addressed later (when we remove Gatsby)
* @param {Context} context
*/
function enforceConsistentDependenciesAcrossTheProject({Yarn}) {
for (const dependency of Yarn.dependencies()) {
if (IGNORE_CONSISTENT_DEPENDENCIES_FOR.has(dependency.workspace.cwd))
continue;
if (dependency.type === `peerDependencies`)
continue;
for (const otherDependency of Yarn.dependencies({ident: dependency.ident})) {
if (IGNORE_CONSISTENT_DEPENDENCIES_FOR.has(otherDependency.workspace.cwd))
continue;
if (otherDependency.type === `peerDependencies`)
continue;
if ((dependency.type === `devDependencies` || otherDependency.type === `devDependencies`) && Yarn.workspace({ident: otherDependency.ident}))
continue;
dependency.update(otherDependency.range);
}
}
}
/**
* This rule will enforce that a workspace MUST depend on the same version of a dependency as the one used by the other workspaces
* We allow Docusaurus to have different dependencies for now; will be addressed later (when we remove Gatsby)
* @param {Context} context
*/
function enforceWorkspaceDependenciesWhenPossible({Yarn}) {
for (const dependency of Yarn.dependencies()) {
if (!Yarn.workspace({ident: dependency.ident}))
continue;
dependency.update(`workspace:^`);
}
}
/**
* @param {Context} context
* @param {string} ident
* @param {string} explanation
*/
function forbidDependency({Yarn}, ident, explanation) {
for (const dependency of Yarn.dependencies({ident})) {
dependency.error(explanation);
}
}
/**
* @param {Context} context
* @param {Record<string, ((workspace: Workspace) => any) | string>} fields
*/
function enforceFieldsOnAllWorkspaces({Yarn}, fields) {
for (const workspace of Yarn.workspaces()) {
for (const [field, value] of Object.entries(fields)) {
workspace.set(field, typeof value === `function` ? value(workspace) : value);
}
}
}
/**
* @param {Context} context
*/
function enforceUpdateLocalScripts({Yarn}) {
const cli = Yarn.workspace({ident: `@yarnpkg/cli`});
if (!cli)
throw new Error(`Assertion failed: We need the @yarnpkg/cli workspace to be present`);
for (const workspace of Yarn.workspaces()) {
if (!workspace.ident?.startsWith(`@yarnpkg/plugin-`))
continue;
if (cli.manifest[`@yarnpkg/builder`].bundles.standard.includes(workspace.ident))
continue;
if (!workspace.manifest.scripts?.[`update-local`]) {
workspace.error(`This workspace is missing an update-local script`);
}
}
}
/**
* @param {Context} context
*/
function enforcePrepackScripts({Yarn}) {
const OMIT_FROM_PREPACK = new Set([
// This package is built using Rollup, so we allow it to configure its build scripts itself
`@yarnpkg/pnp`,
// Those packages use a different build
`@yarnpkg/eslint-config`,
`@yarnpkg/libui`,
]);
for (const workspace of Yarn.workspaces()) {
if (workspace.manifest.private)
continue;
if (!workspace.ident || OMIT_FROM_PREPACK.has(workspace.ident))
continue;
workspace.set(`scripts.prepack`, `run build:compile "$(pwd)"`);
workspace.set(`scripts.postpack`, `rm -rf lib`);
}
}
/**
* @param {Context} context
* @param {string} ident
* @param {string} otherIdent
* @param {boolean} mustExist
*/
function enforceDependencyRelationship({Yarn}, ident, otherIdent, mustExist) {
for (const dependency of Yarn.dependencies({ident})) {
if (dependency.type === `peerDependencies`)
continue;
const hasOtherDependency = Yarn.dependency({
workspace: dependency.workspace,
ident: otherIdent,
});
if (mustExist) {
if (hasOtherDependency)
continue;
dependency.error(`The presence of ${ident} in ${dependency.type} mandates the presence of ${otherIdent}`);
} else {
if (!hasOtherDependency)
continue;
dependency.error(`The presence of ${ident} in ${dependency.type} forbids the presence of ${otherIdent}`);
}
}
}
/**
* Validate that all peer dependencies are provided. If one isn't, the
* constraint will try to fix it by looking at what's used in the other
* workspaces of the project. If it doesn't find any way to satisfy the
* dependency, it will generate an error.
*
* @param {Context} context
*/
function enforcePeerDependencyPresence({Yarn}) {
for (const workspace of Yarn.workspaces()) {
// The Gatsby website is pretty much deprecated anyway
if (workspace.cwd === `packages/gatsby`)
continue;
for (const dependency of Yarn.dependencies({workspace})) {
if (dependency.type === `peerDependencies`)
continue;
if (!dependency.resolution)
continue;
for (const peerName of dependency.resolution.peerDependencies.keys()) {
// Webpack plugins have peer dependencies but don't often need it; weird
if (peerName === `webpack`)
continue;
if (dependency.resolution.dependencies.has(peerName))
continue;
const otherDeps = Yarn.dependencies({ident: peerName})
.filter(otherDep => otherDep.type !== `peerDependencies`);
if (otherDeps.length === 0)
workspace.error(`Missing dependency on ${peerName} (required by ${dependency.ident})`);
// If the workspace has itself a peer dependency of the same name, then
// we assume that it'll be fulfilled by its ancestors in the dependency
// tree, so we only need to add the dependency to devDependencies.
const autofixTarget = Yarn.dependency({workspace, ident: peerName, type: `peerDependencies`})
? `devDependencies`
: `dependencies`;
for (const otherDep of otherDeps) {
workspace.set([autofixTarget, peerName], otherDep.range);
}
}
}
}
}
module.exports = defineConfig({
constraints: async ctx => {
enforceConsistentDependenciesAcrossTheProject(ctx);
enforceWorkspaceDependenciesWhenPossible(ctx);
forbidDependency(ctx, `inquirer`, `Don't depend on inquirer - we use enquirer instead`);
enforceDependencyRelationship(ctx, `typescript`, `tslib`, true);
enforceUpdateLocalScripts(ctx);
enforcePrepackScripts(ctx);
enforcePeerDependencyPresence(ctx);
enforceFieldsOnAllWorkspaces(ctx, {
license: `BSD-2-Clause`,
// When changing the engines.node value check https://node.green/ for
// which ECMAScript version is fully supported and update the following files as needed:
// - tsconfig.json
// - packages/eslint-config/index.js
// - packages/yarnpkg-builder/sources/commands/new/plugin.ts
[`engines.node`]: `>=18.12.0`,
[`repository.type`]: `git`,
[`repository.directory`]: workspace => workspace.cwd,
});
},
});