diff --git a/packages/plugin-dts/src/index.ts b/packages/plugin-dts/src/index.ts index 5a5824b87..be80dcc02 100644 --- a/packages/plugin-dts/src/index.ts +++ b/packages/plugin-dts/src/index.ts @@ -8,6 +8,7 @@ import { cleanDtsFiles, cleanTsBuildInfoFile, clearTempDeclarationDir, + getDtsEmitPath, loadTsconfig, processSourceEntry, warnIfOutside, @@ -113,11 +114,11 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({ const { options: rawCompilerOptions } = tsConfigResult; const { declarationDir, outDir, composite, incremental } = rawCompilerOptions; - const dtsEmitPath = - options.distPath ?? - declarationDir ?? - outDir ?? - config.output?.distPath?.root; + const dtsEmitPath = getDtsEmitPath( + options.distPath, + declarationDir, + config.output?.distPath?.root, + ); // check whether declarationDir or outDir is outside from current project warnIfOutside(cwd, declarationDir, 'declarationDir'); diff --git a/packages/plugin-dts/src/utils.ts b/packages/plugin-dts/src/utils.ts index fcca489a3..8ebe4e0c2 100644 --- a/packages/plugin-dts/src/utils.ts +++ b/packages/plugin-dts/src/utils.ts @@ -506,6 +506,16 @@ export async function cleanTsBuildInfoFile( } } +// the priority of dtsEmitPath is dts.distPath > declarationDir > output.distPath.root +// outDir is not considered since in multiple formats, the dts files may not in the same directory as the js files +export function getDtsEmitPath( + pathFromPlugin: string | undefined, + declarationDir: string | undefined, + distPath: string, +): string { + return pathFromPlugin ?? declarationDir ?? distPath; +} + export function warnIfOutside( cwd: string, dir: string | undefined, diff --git a/packages/plugin-dts/tests/dts.test.ts b/packages/plugin-dts/tests/dts.test.ts new file mode 100644 index 000000000..ed4419894 --- /dev/null +++ b/packages/plugin-dts/tests/dts.test.ts @@ -0,0 +1,46 @@ +import { describe, expect, it } from 'vitest'; +import type { PluginDtsOptions } from '../src/index'; +import { getDtsEmitPath } from '../src/utils'; + +describe('getDtsEmitPath', () => { + const baseConfig = { + output: { + distPath: { + root: '/dist-config', + }, + }, + }; + const declarationDir = '/dist-declarationDir'; + + it('should return options.distPath with the highest priority', () => { + const options: PluginDtsOptions = { + distPath: '/dist-options', + }; + const result = getDtsEmitPath( + options.distPath, + declarationDir, + baseConfig.output.distPath.root, + ); + expect(result).toBe('/dist-options'); + }); + + it('should return declarationDir when options.distPath is undefined', () => { + const options: PluginDtsOptions = {}; + const result = getDtsEmitPath( + options.distPath, + declarationDir, + baseConfig.output.distPath.root, + ); + expect(result).toBe('/dist-declarationDir'); + }); + + it('should return config.output.distPath.root when both options.distPath and declarationDir are undefined', () => { + const options: PluginDtsOptions = {}; + const result = getDtsEmitPath( + options.distPath, + undefined, + baseConfig.output.distPath.root, + ); + expect(result).toBe('/dist-config'); + }); +});