Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/plugin-dts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
cleanDtsFiles,
cleanTsBuildInfoFile,
clearTempDeclarationDir,
getDtsEmitPath,
loadTsconfig,
processSourceEntry,
warnIfOutside,
Expand Down Expand Up @@ -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');
Expand Down
10 changes: 10 additions & 0 deletions packages/plugin-dts/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
46 changes: 46 additions & 0 deletions packages/plugin-dts/tests/dts.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
Loading