Skip to content

Commit 4b1e0a4

Browse files
authored
fix: revert dtsEmitPath generate logic to respect output.distPath.root (#788)
1 parent 80f583f commit 4b1e0a4

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

packages/plugin-dts/src/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
cleanDtsFiles,
99
cleanTsBuildInfoFile,
1010
clearTempDeclarationDir,
11+
getDtsEmitPath,
1112
loadTsconfig,
1213
processSourceEntry,
1314
warnIfOutside,
@@ -113,11 +114,11 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
113114
const { options: rawCompilerOptions } = tsConfigResult;
114115
const { declarationDir, outDir, composite, incremental } =
115116
rawCompilerOptions;
116-
const dtsEmitPath =
117-
options.distPath ??
118-
declarationDir ??
119-
outDir ??
120-
config.output?.distPath?.root;
117+
const dtsEmitPath = getDtsEmitPath(
118+
options.distPath,
119+
declarationDir,
120+
config.output?.distPath?.root,
121+
);
121122

122123
// check whether declarationDir or outDir is outside from current project
123124
warnIfOutside(cwd, declarationDir, 'declarationDir');

packages/plugin-dts/src/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,16 @@ export async function cleanTsBuildInfoFile(
506506
}
507507
}
508508

509+
// the priority of dtsEmitPath is dts.distPath > declarationDir > output.distPath.root
510+
// outDir is not considered since in multiple formats, the dts files may not in the same directory as the js files
511+
export function getDtsEmitPath(
512+
pathFromPlugin: string | undefined,
513+
declarationDir: string | undefined,
514+
distPath: string,
515+
): string {
516+
return pathFromPlugin ?? declarationDir ?? distPath;
517+
}
518+
509519
export function warnIfOutside(
510520
cwd: string,
511521
dir: string | undefined,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { describe, expect, it } from 'vitest';
2+
import type { PluginDtsOptions } from '../src/index';
3+
import { getDtsEmitPath } from '../src/utils';
4+
5+
describe('getDtsEmitPath', () => {
6+
const baseConfig = {
7+
output: {
8+
distPath: {
9+
root: '/dist-config',
10+
},
11+
},
12+
};
13+
const declarationDir = '/dist-declarationDir';
14+
15+
it('should return options.distPath with the highest priority', () => {
16+
const options: PluginDtsOptions = {
17+
distPath: '/dist-options',
18+
};
19+
const result = getDtsEmitPath(
20+
options.distPath,
21+
declarationDir,
22+
baseConfig.output.distPath.root,
23+
);
24+
expect(result).toBe('/dist-options');
25+
});
26+
27+
it('should return declarationDir when options.distPath is undefined', () => {
28+
const options: PluginDtsOptions = {};
29+
const result = getDtsEmitPath(
30+
options.distPath,
31+
declarationDir,
32+
baseConfig.output.distPath.root,
33+
);
34+
expect(result).toBe('/dist-declarationDir');
35+
});
36+
37+
it('should return config.output.distPath.root when both options.distPath and declarationDir are undefined', () => {
38+
const options: PluginDtsOptions = {};
39+
const result = getDtsEmitPath(
40+
options.distPath,
41+
undefined,
42+
baseConfig.output.distPath.root,
43+
);
44+
expect(result).toBe('/dist-config');
45+
});
46+
});

0 commit comments

Comments
 (0)