Skip to content

Commit 75c30da

Browse files
committed
fix(dts-plugin): infer root dir from tsconfig references
1 parent 12240bb commit 75c30da

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

.changeset/quiet-clouds-report.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/dts-plugin': patch
3+
---
4+
5+
Infer root dir from "references" property for solution-style tsconfig

packages/dts-plugin/src/core/configurations/remotePlugin.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,19 @@ describe('hostPlugin', () => {
204204
});
205205
});
206206
});
207+
208+
describe('successfully parse tsconfig with project references', () => {
209+
it.each([
210+
'./testconfig-reference.test.json',
211+
'./testconfig-reference-2.test.json',
212+
])('infers rootDir from project references', (tsConfigFile) => {
213+
const tsConfigPath = join(__dirname, tsConfigFile);
214+
const { tsConfig } = retrieveRemoteConfig({
215+
moduleFederationConfig,
216+
tsConfigPath,
217+
});
218+
219+
expect(tsConfig.compilerOptions.rootDir).toBe(resolve(__dirname));
220+
});
221+
});
207222
});

packages/dts-plugin/src/core/configurations/remotePlugin.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ function getEffectiveRootDir(
4949
return commonRoot;
5050
}
5151

52+
// if there are project references, infer the commonRoot from references
53+
if (parsedCommandLine.projectReferences.length) {
54+
const commonRoot = parsedCommandLine.projectReferences
55+
.filter((file) => file.originalPath.startsWith('./'))
56+
.map((file) => file.path)
57+
.reduce((commonPath, filePath) => {
58+
while (!filePath.startsWith(commonPath)) {
59+
commonPath = dirname(commonPath);
60+
}
61+
return commonPath;
62+
}, dirname(parsedCommandLine.projectReferences[0].path));
63+
return commonRoot;
64+
}
65+
5266
throw new Error(
5367
'Can not get effective rootDir, please set compilerOptions.rootDir !',
5468
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"files": [],
3+
"references": [
4+
{ "path": "./testconfig.app.test.json" },
5+
{ "path": "./testconfig.node.test.json" }
6+
]
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"files": [],
3+
"references": [{ "path": "./testconfig.test.json" }]
4+
}

0 commit comments

Comments
 (0)