Skip to content

Commit f775a58

Browse files
committed
chore: update
1 parent f0e2b7e commit f775a58

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

packages/plugin-dts/src/index.ts

Lines changed: 6 additions & 2 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,
@@ -115,8 +116,11 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
115116
rawCompilerOptions;
116117
// the priority of dtsEmitPath is dts.distPath > declarationDir > output.distPath.root
117118
// outDir is not considered since in multiple formats, the dts files may not in the same directory as the js files
118-
const dtsEmitPath =
119-
options.distPath ?? declarationDir ?? config.output?.distPath?.root;
119+
const dtsEmitPath = getDtsEmitPath(
120+
options.distPath,
121+
declarationDir,
122+
config.output?.distPath?.root,
123+
);
120124

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

packages/plugin-dts/src/utils.ts

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

509+
export function getDtsEmitPath(
510+
pathFromPlugin: string | undefined,
511+
declarationDir: string | undefined,
512+
distPath: string,
513+
): string {
514+
return pathFromPlugin ?? declarationDir ?? distPath;
515+
}
516+
509517
export function warnIfOutside(
510518
cwd: string,
511519
dir: string | undefined,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
it('should return declarationDir when options.distPath is undefined', () => {
27+
const options: PluginDtsOptions = {};
28+
const result = getDtsEmitPath(
29+
options.distPath,
30+
declarationDir,
31+
baseConfig.output.distPath.root,
32+
);
33+
expect(result).toBe('/dist-declarationDir');
34+
});
35+
36+
it('should return config.output.distPath.root when both options.distPath and declarationDir are undefined', () => {
37+
const options: PluginDtsOptions = {};
38+
const result = getDtsEmitPath(
39+
options.distPath,
40+
undefined,
41+
baseConfig.output.distPath.root,
42+
);
43+
expect(result).toBe('/dist-config');
44+
});
45+
});

0 commit comments

Comments
 (0)