Skip to content

Commit 1fd2b10

Browse files
committed
feat!: prune references in the root config
1 parent b663164 commit 1fd2b10

6 files changed

Lines changed: 22 additions & 121 deletions

File tree

src/update-ts-references.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@ const execute = async ({
340340
let rootPaths = [];
341341
let tsconfigMap = {}
342342
let jsconfigMap = {}
343+
const referencesMap = {}
344+
const referencedPackageNames = new Set()
345+
const rootReferenceCandidates = []
343346
packagesMap.forEach((packageEntry, packageName) => {
344347
const detectedConfig = detectTSConfig(packageEntry.packageDir, configName, packageEntry.hasTsEntry && createTsConfig, cwd)
345348

@@ -379,18 +382,20 @@ const execute = async ({
379382
const detectedConfig = tsconfigMap[packageName]?.detectedConfig
380383

381384
if (detectedConfig) {
382-
rootReferences.push({
385+
rootReferenceCandidates.push(ensurePosixPathStyle({
383386
name: packageName,
384387
path: path.join(path.relative(cwd, packageEntry.packageDir), detectedConfig !== TSCONFIG_JSON ? detectedConfig : ''),
385388
folder: path.relative(cwd, packageEntry.packageDir),
386-
});
389+
}));
387390
const references = (getReferencesFromDependencies(
388391
configName,
389392
packageEntry,
390393
packageName,
391394
packagesMap,
392395
verbose
393396
) || []).map(ensurePosixPathStyle);
397+
referencesMap[packageName] = references
398+
references.forEach(({ name }) => referencedPackageNames.add(name))
394399

395400
const paths = getPathsFromReferences(references, tsconfigMap, jsconfigMap, ignorePathMappings)
396401

@@ -414,16 +419,11 @@ const execute = async ({
414419
// eslint-disable-next-line no-console
415420
console.log(`NO ${configName === TSCONFIG_JSON ? configName : `${configName} nor ${TSCONFIG_JSON}`} for ${packageName}`);
416421
}
417-
rootPaths.push({
418-
name: packageName,
419-
path: path.relative(cwd, packageEntry.packageDir),
420-
folder: path.relative(cwd, packageEntry.packageDir),
421-
});
422422
}
423423
});
424424

425-
rootReferences = (rootReferences || []).map(ensurePosixPathStyle);
426-
rootPaths = getPathsFromReferences((rootReferences || []).map(ensurePosixPathStyle), tsconfigMap, {}, ignorePathMappings)
425+
rootReferences = rootReferenceCandidates.filter(({ name }) => !referencedPackageNames.has(name));
426+
rootPaths = getPathsFromReferences(rootReferences, tsconfigMap, {}, ignorePathMappings)
427427

428428
if (verbose) {
429429
console.log('rootReferences', rootReferences);

test-scenarios/yarn-ws-check-no-changes/tsconfig.json

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,9 @@
77
{
88
"path": "workspace-a"
99
},
10-
{
11-
"path": "workspace-b"
12-
},
13-
{
14-
"path": "shared/workspace-c"
15-
},
1610
{
1711
"path": "shared/workspace-d"
18-
},
19-
{
20-
"path": "utils/foos/foo-a"
21-
},
22-
{
23-
"path": "utils/foos/foo-b"
2412
}
2513
]
2614
}
27-
15+
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
{
22
"files": [],
33
"compilerOptions": {
4-
"composite": true,
5-
"paths": { "foo-b": ["utils/foos/foo-b/src"] }
4+
"composite": true
65
},
76
"references": [
87
{
98
"path": "workspace-a"
109
},
11-
{
12-
"path": "workspace-b"
13-
},
14-
{
15-
"path": "utils/foos/foo-a"
16-
},
1710

1811
]
1912
}

tests/update-ts-references.check.test.js

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,9 @@ const rootTsConfig = [
4242
{
4343
path: 'workspace-a',
4444
},
45-
{
46-
path: 'workspace-b',
47-
},
48-
{
49-
path: 'shared/workspace-c',
50-
},
5145
{
5246
path: 'shared/workspace-d',
5347
},
54-
{
55-
path: 'utils/foos/foo-a',
56-
},
57-
{
58-
path: 'utils/foos/foo-b',
59-
},
6048
],
6149
},
6250
];
@@ -305,19 +293,13 @@ test('No changes paths detected with the --check option', async () => {
305293
{
306294
compilerOptions: {
307295
composite: true,
308-
paths: { "foo-b": ["utils/foos/foo-b/src"] }
296+
paths: undefined
309297
},
310298
files: [],
311299
references: [
312300
{
313301
path: 'workspace-a',
314302
},
315-
{
316-
path: 'workspace-b',
317-
},
318-
{
319-
path: 'utils/foos/foo-a',
320-
},
321303
],
322304
},
323305
];
@@ -362,5 +344,3 @@ test('No changes paths detected with the --check option', async () => {
362344
).toEqual(config);
363345
});
364346
});
365-
366-

tests/update-ts-references.test.js

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,9 @@ const rootTsConfig = [
4242
{
4343
path: 'workspace-a',
4444
},
45-
{
46-
path: 'workspace-b',
47-
},
48-
{
49-
path: 'shared/workspace-c',
50-
},
5145
{
5246
path: 'shared/workspace-d',
5347
},
54-
{
55-
path: 'utils/foos/foo-a',
56-
},
57-
{
58-
path: 'utils/foos/foo-b',
59-
},
6048
],
6149
},
6250
];
@@ -178,9 +166,6 @@ test('avoid adding an empty compilerOptions', async () => {
178166
{
179167
path: 'workspace-a',
180168
},
181-
{
182-
path: 'workspace-b',
183-
},
184169
],
185170
},
186171
];
@@ -227,6 +212,12 @@ test('Support yarn and npm workspaces', async () => {
227212

228213

229214
});
215+
expect(
216+
parse(fs.readFileSync(path.join(rootFolderYarn, 'tsconfig.json')).toString()).references
217+
).toEqual([
218+
{ path: 'workspace-a' },
219+
{ path: 'shared/workspace-d' },
220+
]);
230221
// still has the comment
231222
expect(fs.readFileSync(path.join(rootFolderYarn, 'tsconfig.json')).toString()).toMatch(/\/\* Basic Options \*\//)
232223
});
@@ -276,22 +267,13 @@ test('create paths mappings ', async () => {
276267
{
277268
compilerOptions: {
278269
composite: true,
279-
paths: { "foo-a": ["utils/foos/foo-a/src"], "foo-b": ["utils/foos/foo-b/src"], "workspace-a": ["workspace-a/src"], "workspace-b": ["workspace-b"] }
270+
paths: { "workspace-a": ["workspace-a/src"] }
280271
},
281272
files: [],
282273
references: [
283274
{
284275
path: 'workspace-a',
285276
},
286-
{
287-
path: 'workspace-b',
288-
},
289-
{
290-
path: 'utils/foos/foo-a',
291-
},
292-
{
293-
path: 'utils/foos/foo-b',
294-
},
295277
],
296278
},
297279
];
@@ -373,22 +355,13 @@ test('create paths mappings with ignorePathMappings', async () => {
373355
{
374356
compilerOptions: {
375357
composite: true,
376-
paths: { "foo-a": ["utils/foos/foo-a/src"], "foo-b": ["utils/foos/foo-b/src"], "workspace-b": ["workspace-b"] }
358+
paths: undefined
377359
},
378360
files: [],
379361
references: [
380362
{
381363
path: 'workspace-a',
382364
},
383-
{
384-
path: 'workspace-b',
385-
},
386-
{
387-
path: 'utils/foos/foo-a',
388-
},
389-
{
390-
path: 'utils/foos/foo-b',
391-
},
392365
],
393366
},
394367
];
@@ -471,9 +444,6 @@ test('Test create tsconfig', async () => {
471444
references: [
472445
{
473446
path: 'workspace-a',
474-
},
475-
{
476-
path: 'workspace-b',
477447
}
478448
],
479449
},
@@ -564,21 +534,9 @@ test('Support custom tsconfig names', async () => {
564534
{
565535
path: 'workspace-a/tsconfig.dev.json',
566536
},
567-
{
568-
path: 'workspace-b/tsconfig.dev.json',
569-
},
570-
{
571-
path: 'shared/workspace-c/tsconfig.dev.json',
572-
},
573537
{
574538
path: 'shared/workspace-d/tsconfig.dev.json',
575539
},
576-
{
577-
path: 'utils/foos/foo-a/tsconfig.dev.json',
578-
},
579-
{
580-
path: 'utils/foos/foo-b',
581-
},
582540
],
583541
},
584542
rootConfigName

tests/update-ts-references.yaml.test.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,9 @@ const rootTsConfig = [
2121
{
2222
path: 'workspace-a',
2323
},
24-
{
25-
path: 'workspace-b',
26-
},
27-
{
28-
path: 'shared/workspace-c',
29-
},
3024
{
3125
path: 'shared/workspace-d',
3226
},
33-
{
34-
path: 'utils/foos/foo-a',
35-
},
36-
{
37-
path: 'utils/foos/foo-b',
38-
},
3927
],
4028
},
4129
];
@@ -184,16 +172,13 @@ test('receive options via the config', async () => {
184172
{
185173
compilerOptions: {
186174
composite: true,
187-
paths:{"workspace-a": ["workspace-a/src"],"workspace-b": ["workspace-b/src"]}
175+
paths:{"workspace-a": ["workspace-a/src"]}
188176
},
189177
files: [],
190178
references: [
191179
{
192180
path: 'workspace-a/tsconfig.dev.json',
193181
},
194-
{
195-
path: 'workspace-b/tsconfig.dev.json',
196-
},
197182
],
198183
},
199184
];
@@ -241,16 +226,13 @@ test('receive options for a different usecase', async () => {
241226
{
242227
compilerOptions: {
243228
composite: true,
244-
paths:{"workspace-a": ["workspace-a/src"],"workspace-b": ["workspace-b/src"]}
229+
paths:{"workspace-a": ["workspace-a/src"]}
245230
},
246231
files: [],
247232
references: [
248233
{
249234
path: 'workspace-a/tsconfig.dev.json',
250235
},
251-
{
252-
path: 'workspace-b/tsconfig.dev.json',
253-
},
254236
],
255237
},
256238
];

0 commit comments

Comments
 (0)