Skip to content

Commit 8bee065

Browse files
authored
fix: rootDir calculation should ignore declaration files (#389)
1 parent ce4dea9 commit 8bee065

File tree

8 files changed

+146
-1
lines changed

8 files changed

+146
-1
lines changed

packages/plugin-dts/src/dts.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,17 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
133133
throw new Error();
134134
}
135135
const { options: rawCompilerOptions, fileNames } = loadTsconfig(configPath);
136+
137+
// The longest common path of all non-declaration input files.
138+
// If composite is set, the default is instead the directory containing the tsconfig.json file.
139+
// see https://www.typescriptlang.org/tsconfig/#rootDir
136140
const rootDir =
137141
rawCompilerOptions.rootDir ??
138-
(await calcLongestCommonPath(fileNames)) ??
142+
(rawCompilerOptions.composite
143+
? dirname(configPath)
144+
: await calcLongestCommonPath(
145+
fileNames.filter((fileName) => !/\.d\.(ts|mts|cts)$/.test(fileName)),
146+
)) ??
139147
dirname(configPath);
140148

141149
const outDir = distPath

pnpm-lock.yaml

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ pxtorem
9393
quxx
9494
rebranded
9595
rolldown
96+
rootdir
9697
rsbuild
9798
rsdoctor
9899
rsfamily

tests/integration/dts/__snapshots__/index.test.ts.snap

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,44 @@ export { }
6060
",
6161
}
6262
`;
63+
64+
exports[`dts when bundle: true > rootdir calculation should ignore declaration files 3`] = `
65+
{
66+
"cjs": "export declare const num1 = 1;
67+
68+
export declare const num2 = 2;
69+
70+
export declare const num3 = 3;
71+
72+
export declare const numSum: number;
73+
74+
export declare const str1 = "str1";
75+
76+
export declare const str2 = "str2";
77+
78+
export declare const str3 = "str3";
79+
80+
export declare const strSum: string;
81+
82+
export { }
83+
",
84+
"esm": "export declare const num1 = 1;
85+
86+
export declare const num2 = 2;
87+
88+
export declare const num3 = 3;
89+
90+
export declare const numSum: number;
91+
92+
export declare const str1 = "str1";
93+
94+
export declare const str2 = "str2";
95+
96+
export declare const str3 = "str3";
97+
98+
export declare const strSum: string;
99+
100+
export { }
101+
",
102+
}
103+
`;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "dts-bundle-rootdir-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module",
6+
"devDependencies": {
7+
"@types/chromecast-caf-sender": "^1.0.10"
8+
}
9+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
dts: {
8+
bundle: true,
9+
},
10+
}),
11+
generateBundleCjsConfig({
12+
dts: {
13+
bundle: true,
14+
},
15+
}),
16+
],
17+
source: {
18+
entry: {
19+
index: '../__fixtures__/src/index.ts',
20+
},
21+
},
22+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "@rslib/tsconfig/base",
3+
"include": ["../__fixtures__/src", "node_modules/@types"]
4+
}

tests/integration/dts/index.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,28 @@ describe('dts when bundle: true', () => {
221221
`,
222222
);
223223
});
224+
225+
test('rootdir calculation should ignore declaration files', async () => {
226+
const fixturePath = join(__dirname, 'bundle', 'rootdir');
227+
const { files, entries } = await buildAndGetResults({
228+
fixturePath,
229+
type: 'dts',
230+
});
231+
232+
expect(files.esm).toMatchInlineSnapshot(`
233+
[
234+
"<ROOT>/tests/integration/dts/bundle/rootdir/dist/esm/index.d.ts",
235+
]
236+
`);
237+
238+
expect(files.cjs).toMatchInlineSnapshot(`
239+
[
240+
"<ROOT>/tests/integration/dts/bundle/rootdir/dist/cjs/index.d.ts",
241+
]
242+
`);
243+
244+
expect(entries).toMatchSnapshot();
245+
});
224246
});
225247

226248
describe('dts when build: true', () => {

0 commit comments

Comments
 (0)