Skip to content

Commit 95282ac

Browse files
eezhal922heal1
andauthored
fix(dts-plugin): infer root dir from tsconfig references to fix Vite 8 new template dev server failure (#4543)
Co-authored-by: Hanric <hanric.zhang@gmail.com>
1 parent f81bbea commit 95282ac

File tree

6 files changed

+72
-1
lines changed

6 files changed

+72
-1
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,32 @@ describe('hostPlugin', () => {
204204
});
205205
});
206206
});
207+
208+
describe('successfully parse tsconfig with project references', () => {
209+
it.each([
210+
{
211+
tsConfigFile: './testconfig-reference.test.json',
212+
expectedRootDir: resolve(__dirname),
213+
},
214+
{
215+
tsConfigFile: './testconfig-reference-2.test.json',
216+
expectedRootDir: resolve(__dirname),
217+
},
218+
{
219+
tsConfigFile: './testconfig-reference-relative.test.json',
220+
expectedRootDir: resolve(__dirname, '..'),
221+
},
222+
])(
223+
'infers rootDir from project references',
224+
({ tsConfigFile, expectedRootDir }) => {
225+
const tsConfigPath = join(__dirname, tsConfigFile);
226+
const { tsConfig } = retrieveRemoteConfig({
227+
moduleFederationConfig,
228+
tsConfigPath,
229+
});
230+
231+
expect(tsConfig.compilerOptions.rootDir).toBe(expectedRootDir);
232+
},
233+
);
234+
});
207235
});

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { existsSync } from 'fs';
2-
import { dirname, join, resolve, extname } from 'path';
2+
import { dirname, join, resolve, extname, isAbsolute } from 'path';
33
import { utils } from '@module-federation/managers';
44
import typescript from 'typescript';
55

@@ -49,6 +49,26 @@ 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 relativeReferences = parsedCommandLine.projectReferences.filter(
55+
(reference) => !isAbsolute(reference.originalPath ?? reference.path),
56+
);
57+
const referencesForRoot = relativeReferences.length
58+
? relativeReferences
59+
: parsedCommandLine.projectReferences;
60+
61+
const commonRoot = referencesForRoot
62+
.map((reference) => dirname(reference.path))
63+
.reduce((commonPath, filePath) => {
64+
while (!filePath.startsWith(commonPath)) {
65+
commonPath = dirname(commonPath);
66+
}
67+
return commonPath;
68+
}, dirname(referencesForRoot[0].path));
69+
return commonRoot;
70+
}
71+
5272
throw new Error(
5373
'Can not get effective rootDir, please set compilerOptions.rootDir !',
5474
);
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"files": [],
3+
"references": [
4+
{ "path": "../shared/testconfig.shared.test.json" },
5+
{ "path": "testconfig.local.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)