forked from adobe/react-spectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yarn.config.cjs
283 lines (255 loc) · 10.7 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
// @ts-check
/** @type {import('@yarnpkg/types')} */
const {defineConfig} = require('@yarnpkg/types');
/**
* @typedef {import('@yarnpkg/types').Yarn.Constraints.Workspace} Workspace
* @typedef {import('@yarnpkg/types').Yarn.Constraints.Dependency} Dependency
*/
/**
* This rule will enforce that a workspace MUST depend on the same version of
* a dependency as the one used by the other workspaces.
*
* @param {Context} context
*/
function enforceConsistentDependenciesAcrossTheProject({Yarn}) {
// enforce react/react-dom version
for (const dependency of Yarn.dependencies()) {
if (dependency.type === 'peerDependencies') {
if (dependency.ident === 'react' || dependency.ident === 'react-dom') {
if (dependency.workspace.ident === 'storybook-builder-parcel') {
dependency.update('*');
} else if (dependency.workspace.ident === '@react-spectrum/s2') {
dependency.update('^18.0.0 || ^19.0.0-rc.1');
} else {
dependency.update('^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1');
}
}
}
// for (const otherDependency of Yarn.dependencies({ident: dependency.ident})) {
// if (otherDependency.type === `peerDependencies`)
// continue;
//
// // useful, however, i haven't quite made it relaxed enough
// // dependency.update(otherDependency.range);
// }
}
// enforce swc helpers version, spectrum-css-temp, and where they are placed in the package.json
for (const workspace of Yarn.workspaces()) {
if (isPublishing(workspace)
// should these be included in the requirement?
&& workspace.ident !== '@adobe/react-spectrum'
&& workspace.ident !== 'react-aria'
&& workspace.ident !== 'react-stately'
&& workspace.ident !== '@internationalized/string-compiler'
&& workspace.ident !== 'tailwindcss-react-aria-components'
&& workspace.ident !== '@react-spectrum/s2'
&& workspace.manifest.rsp?.type !== 'cli'
) {
workspace.set('dependencies.@swc/helpers', '^0.5.0');
workspace.set('dependencies.@adobe/spectrum-css-temp');
if (workspace.ident.startsWith('@react-spectrum') && !workspace.ident.endsWith('/utils')) {
workspace.set('devDependencies.@adobe/spectrum-css-temp', '3.0.0-alpha.1');
}
// these should not be in dependencies, but should be in dev or peer
// can't change the error message, but the package knows if it even needs it
if (!workspace.ident.startsWith('@react-spectrum/test-utils-internal')) {
workspace.set('dependencies.@react-spectrum/test-utils');
workspace.set('dependencies.react');
workspace.set('dependencies.react-dom');
}
}
}
}
/** @param {Context} context */
function enforceNonPrivateDependencies({Yarn}) {
// enforce that public packages do not depend on private packages
for (const workspace of Yarn.workspaces()) {
if (workspace.manifest.private) {
for (const dependency of Yarn.dependencies({ident: workspace.ident})) {
if (dependency.type !== 'devDependencies' && !dependency.workspace.manifest.private) {
dependency.workspace.set('private', true);
workspace.set('private', false);
}
}
}
}
}
/** @param {Context} context */
function enforceNoCircularDependencies({Yarn}) {
// enforce that there are no circular dependencies between our packages
function addDep(workspace, seen = new Set()) {
if (seen.has(workspace.ident)) {
let arr = [...seen];
let index = arr.indexOf(workspace.ident);
// ok for pkg to depend on itself
if (arr.slice(index).length > 1) {
// better to error the constraints early for this for a more meaningful error message
throw new Error(`Circular dependency detected: ${arr.slice(index).join(' -> ')} -> ${workspace.ident}`);
} else {
return;
}
}
seen.add(workspace.ident);
for (let d of Yarn.dependencies({ident: workspace.ident})) {
addDep(d.workspace, seen);
}
seen.delete(workspace.ident);
}
for (const workspace of Yarn.workspaces()) {
addDep(workspace);
}
}
/** @param {Dependency} dependency */
function isOurPackage(dependency) {
let name = dependency.ident;
return name.includes('@react-spectrum')
|| name.includes('@react-aria')
|| name.includes('@react-stately')
|| name.includes('@react-types')
|| name.includes('@internationalized')
|| name.includes('@spectrum-icons')
|| name.startsWith('react-aria-components')
|| name.startsWith('tailwindcss-react-aria-components')
|| name.startsWith('react-aria')
|| name.startsWith('react-stately');
}
/** @param {Context} context */
function enforceWorkspaceDependencies({Yarn}) {
for (const dependency of Yarn.dependencies()) {
if (dependency.type === 'peerDependencies') {continue;}
for (const otherDependency of Yarn.dependencies({ident: dependency.ident})) {
if (otherDependency.type === 'peerDependencies') {continue;}
if (isOurPackage(dependency)) {
// change back to workspaces:^ when we're ready for yarn to handle versioning
// don't check for consistency on our own packages because they can individually version and we don't bump EVERY package because of one change
// dependency.update(otherDependency.range);
}
}
}
}
/** @param {Context} context */
function enforceCSS({Yarn}) {
for (const workspace of Yarn.workspaces()) {
let name = workspace.ident;
if (!name.startsWith('@react-spectrum/docs') && !name.startsWith('@react-spectrum/test-utils') && name.startsWith('@react-spectrum') && workspace.pkg.dependencies?.has('@adobe/spectrum-css-temp')) {
workspace.set('targets', {
main: {includeNodeModules: ['@adobe/spectrum-css-temp']},
module: {includeNodeModules: ['@adobe/spectrum-css-temp']}
});
}
}
}
/** @param {Workspace} workspace */
function isPublishing(workspace) {
let name = workspace.ident;
// should allowlist instead? workspace.manifest.private?
return !name.includes('@react-types')
&& !name.includes('@spectrum-icons')
&& !name.includes('@react-aria/example-theme')
&& !name.includes('@react-spectrum/style-macro-s1')
&& !name.includes('@react-spectrum/docs')
&& !name.includes('parcel')
&& !name.includes('@adobe/spectrum-css-temp')
&& !name.includes('css-module-types')
&& !name.includes('eslint')
&& !name.includes('optimize-locales-plugin')
&& name !== 'react-spectrum-monorepo';
}
/** @param {Context} context */
function enforcePublishing({Yarn}) {
// make sure fields required for publishing have been set
for (const workspace of Yarn.workspaces()) {
let name = workspace.ident;
if (isPublishing(workspace) || (workspace.manifest.rsp?.type === 'cli' && !workspace.manifest.private)) {
if (name.startsWith('@react-spectrum')) {
workspace.set('license', 'Apache-2.0');
}
if (!workspace.manifest.private) {
workspace.set('publishConfig', {access: 'public'});
}
workspace.set('repository', {
type: 'git',
url: name.startsWith('@internationalized/date') ?
'https://github.com/adobe/react-spectrum/tree/main/packages/@internationalized/date'
: 'https://github.com/adobe/react-spectrum'
});
}
}
}
function setExtension(filepath, ext = '.js') {
if (!filepath) {
return;
}
return filepath.replace(/\.[a-zA-Z0-9]*$/, ext);
}
/** @param {Context} context */
function enforceExports({Yarn}) {
// make sure build fields are correctly set
for (const workspace of Yarn.workspaces()) {
let name = workspace.ident;
if (isPublishing(workspace) && workspace.manifest.rsp?.type !== 'cli') {
let moduleExt = name === '@react-spectrum/s2' ? '.mjs' : '.js';
let cjsExt = name === '@react-spectrum/s2' ? '.cjs' : '.js';
if (workspace.manifest.main) {
workspace.set('main', setExtension(workspace.manifest.main, cjsExt));
} else {
workspace.set('main', setExtension('dist/main.js', cjsExt));
}
if (
name !== '@internationalized/string-compiler' &&
name !== 'tailwindcss-react-aria-components'
) {
workspace.set('module', setExtension('dist/module.js', moduleExt));
}
let exportsRequire = workspace.manifest?.exports?.require;
let exportsImport = workspace.manifest?.exports?.import;
if (workspace.manifest.exports?.['.']) {
for (let key in workspace.manifest.exports) {
if (workspace.manifest.exports[key]) {
let subExportsRequire = workspace.manifest.exports[key].require;
workspace.set(`exports["${key}"].require`, setExtension(subExportsRequire, cjsExt));
let subExportsImport = workspace.manifest.exports[key].import;
workspace.set(`exports["${key}"].import`, setExtension(subExportsImport, '.mjs'));
}
}
} else {
workspace.set('exports.require', setExtension(exportsRequire, cjsExt));
workspace.set('exports.import', setExtension(exportsImport, '.mjs'));
}
if ((!workspace.manifest.types || !workspace.manifest.types.endsWith('.d.ts'))) {
workspace.set('types', 'dist/types.d.ts');
}
if (name !== '@adobe/react-spectrum' && name !== 'react-aria' && name !== 'react-stately' && name !== '@internationalized/string-compiler' && name !== 'tailwindcss-react-aria-components') {
workspace.set('source', 'src/index.ts');
}
if (name !== '@adobe/react-spectrum' && name !== 'react-aria' && name !== 'react-stately' && name !== '@internationalized/string-compiler' && name !== 'tailwindcss-react-aria-components') {
if (!workspace.manifest.files || (!workspace.manifest.files.includes('dist') && !workspace.manifest.files.includes('src'))) {
workspace.set('files', [...workspace.manifest.files || [], 'dist', 'src']);
} else if (!workspace.manifest.files.includes('dist')) {
workspace.set('files', [...workspace.manifest.files, 'dist']);
} else if (!workspace.manifest.files.includes('src')) {
workspace.set('files', [...workspace.manifest.files, 'src']);
}
}
// better to do in enforceCSS? it doesn't match the set of packages handled
if (name !== 'react-aria-components') {
if (name.includes('@react-spectrum') || name.includes('@react-aria/visually-hidden')) {
workspace.set('sideEffects', ['*.css']);
} else {
workspace.set('sideEffects', false);
}
}
}
}
}
module.exports = defineConfig({
constraints: async ctx => {
enforceWorkspaceDependencies(ctx);
enforceConsistentDependenciesAcrossTheProject(ctx);
enforceCSS(ctx);
enforcePublishing(ctx);
enforceExports(ctx);
enforceNonPrivateDependencies(ctx);
enforceNoCircularDependencies(ctx);
}
});