Skip to content

Commit c37f3a0

Browse files
authored
fix: respect declarationDir if dts.distPath is not set (#420)
1 parent dacb1d2 commit c37f3a0

File tree

9 files changed

+80
-31
lines changed

9 files changed

+80
-31
lines changed

packages/core/src/config.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ const composeDtsConfig = async (
911911
libConfig: LibConfig,
912912
dtsExtension: string,
913913
): Promise<RsbuildConfig> => {
914-
const { output, autoExternal, banner, footer } = libConfig;
914+
const { autoExternal, banner, footer } = libConfig;
915915

916916
let { dts } = libConfig;
917917

@@ -929,10 +929,10 @@ const composeDtsConfig = async (
929929
plugins: [
930930
pluginDts({
931931
// Only setting ⁠dts.bundle to true will generate the bundled d.ts.
932-
bundle: dts?.bundle ?? false,
933-
distPath: dts?.distPath ?? output?.distPath?.root ?? './dist',
934-
build: dts?.build ?? false,
935-
abortOnError: dts?.abortOnError ?? true,
932+
bundle: dts?.bundle,
933+
distPath: dts?.distPath,
934+
build: dts?.build,
935+
abortOnError: dts?.abortOnError,
936936
dtsExtension: dts?.autoExtension ? dtsExtension : '.d.ts',
937937
autoExternal,
938938
banner: banner?.dts,

packages/plugin-dts/src/apiExtractor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { addBannerAndFooter, getTimeCost } from './utils';
1212
export type BundleOptions = {
1313
name: string;
1414
cwd: string;
15-
outDir: string;
15+
distPath: string;
1616
dtsExtension: string;
1717
banner?: string;
1818
footer?: string;
@@ -25,7 +25,7 @@ export async function bundleDts(options: BundleOptions): Promise<void> {
2525
const {
2626
name,
2727
cwd,
28-
outDir,
28+
distPath,
2929
dtsExtension,
3030
banner,
3131
footer,
@@ -40,7 +40,7 @@ export async function bundleDts(options: BundleOptions): Promise<void> {
4040
const start = Date.now();
4141
const untrimmedFilePath = join(
4242
cwd,
43-
relative(cwd, outDir),
43+
relative(cwd, distPath),
4444
`${dtsEntry.name}${dtsExtension}`,
4545
);
4646
const mainEntryPointFilePath = dtsEntry.path!;

packages/plugin-dts/src/dts.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
125125
userExternals,
126126
banner,
127127
footer,
128+
rootDistPath,
128129
} = data;
129130
logger.start(`Generating DTS... ${color.gray(`(${name})`)}`);
130131
const configPath = ts.findConfigFile(cwd, ts.sys.fileExists, tsconfigPath);
@@ -146,9 +147,12 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
146147
)) ??
147148
dirname(configPath);
148149

149-
const outDir = distPath
150-
? distPath
151-
: rawCompilerOptions.declarationDir || './dist';
150+
const dtsEmitPath =
151+
distPath ?? rawCompilerOptions.declarationDir ?? rootDistPath;
152+
153+
const resolvedDtsEmitPath = normalize(
154+
resolve(dirname(configPath), dtsEmitPath),
155+
);
152156

153157
if (build) {
154158
// do not allow to use bundle DTS when 'build: true' since temp declarationDir should be set by user in tsconfig
@@ -159,34 +163,24 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
159163
// can not set '--declarationDir' or '--outDir' when 'build: true'.
160164
if (
161165
(!rawCompilerOptions.outDir ||
162-
normalize(rawCompilerOptions.outDir) !==
163-
normalize(resolve(dirname(configPath), outDir))) &&
166+
normalize(rawCompilerOptions.outDir) !== resolvedDtsEmitPath) &&
164167
(!rawCompilerOptions.declarationDir ||
165-
normalize(rawCompilerOptions.declarationDir) !==
166-
normalize(resolve(dirname(configPath), outDir)))
168+
normalize(rawCompilerOptions.declarationDir) !== resolvedDtsEmitPath)
167169
) {
168170
const info =
169171
rawCompilerOptions.outDir && !rawCompilerOptions.declarationDir
170172
? 'outDir'
171173
: 'declarationDir';
172174
throw Error(
173-
`Please set ${info}: "${outDir}" in ${color.underline(
175+
`Please set ${info}: "${dtsEmitPath}" in ${color.underline(
174176
configPath,
175-
)} to keep it same as "dts.distPath" or "output.distPath" field in lib config.`,
177+
)} to keep it same as "dts.distPath" or "output.distPath.root" field in lib config.`,
176178
);
177179
}
178180
}
179181

180-
const getDeclarationDir = (bundle: boolean, distPath?: string) => {
181-
if (bundle) {
182-
return ensureTempDeclarationDir(cwd);
183-
}
184-
return distPath
185-
? distPath
186-
: (rawCompilerOptions.declarationDir ?? './dist');
187-
};
182+
const declarationDir = bundle ? ensureTempDeclarationDir(cwd) : dtsEmitPath;
188183

189-
const declarationDir = getDeclarationDir(bundle!, distPath);
190184
const { name: entryName, path: entryPath } = dtsEntry;
191185
let entry = '';
192186

@@ -211,7 +205,7 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
211205
await bundleDts({
212206
name,
213207
cwd,
214-
outDir,
208+
distPath: dtsEmitPath,
215209
dtsEntry: {
216210
name: entryName,
217211
path: entry,

packages/plugin-dts/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export type DtsGenOptions = PluginDtsOptions & {
3434
cwd: string;
3535
isWatch: boolean;
3636
dtsEntry: DtsEntry;
37+
rootDistPath: string;
3738
build?: boolean;
3839
tsconfigPath?: string;
3940
userExternals?: NonNullable<RsbuildConfig['output']>['externals'];
@@ -68,8 +69,6 @@ export const pluginDts = (options: PluginDtsOptions): RsbuildPlugin => ({
6869

6970
const { config } = environment;
7071

71-
options.distPath = options.distPath ?? config.output?.distPath?.root;
72-
7372
const jsExtension = extname(__filename);
7473
const childProcess = fork(join(__dirname, `./dts${jsExtension}`), [], {
7574
stdio: 'inherit',
@@ -86,6 +85,7 @@ export const pluginDts = (options: PluginDtsOptions): RsbuildPlugin => ({
8685
...options,
8786
dtsEntry,
8887
userExternals: config.output.externals,
88+
rootDistPath: config.output?.distPath?.root,
8989
tsconfigPath: config.source.tsconfigPath,
9090
name: environment.name,
9191
cwd: api.context.rootPath,

pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "dts-bundle-false-declaration-dir-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
bundle: false,
8+
dts: {
9+
bundle: false,
10+
},
11+
}),
12+
],
13+
source: {
14+
entry: {
15+
index: ['../__fixtures__/src/**'],
16+
},
17+
},
18+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "@rslib/tsconfig/base",
3+
"compilerOptions": {
4+
"baseUrl": "./",
5+
"declarationDir": "./dist-types"
6+
},
7+
"include": ["../__fixtures__/src"]
8+
}

tests/integration/dts/index.test.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { existsSync } from 'node:fs';
22
import { join } from 'node:path';
3-
import { buildAndGetResults } from 'test-helper';
3+
import { buildAndGetResults, globContentJSON } from 'test-helper';
44
import { describe, expect, test } from 'vitest';
55

66
describe('dts when bundle: false', () => {
@@ -90,6 +90,27 @@ describe('dts when bundle: false', () => {
9090
]
9191
`);
9292
});
93+
94+
test('should use declarationDir when not set dts.distPath', async () => {
95+
const fixturePath = join(__dirname, 'bundle-false', 'declaration-dir');
96+
const distTypesPath = join(fixturePath, 'dist-types');
97+
98+
await buildAndGetResults({ fixturePath, type: 'dts' });
99+
100+
const distTypeFiles = await globContentJSON(distTypesPath, {
101+
absolute: true,
102+
});
103+
const distTypeFilePaths = Object.keys(distTypeFiles).sort();
104+
105+
expect(distTypeFilePaths).toMatchInlineSnapshot(`
106+
[
107+
"<ROOT>/tests/integration/dts/bundle-false/declaration-dir/dist-types/index.d.ts",
108+
"<ROOT>/tests/integration/dts/bundle-false/declaration-dir/dist-types/sum.d.ts",
109+
"<ROOT>/tests/integration/dts/bundle-false/declaration-dir/dist-types/utils/numbers.d.ts",
110+
"<ROOT>/tests/integration/dts/bundle-false/declaration-dir/dist-types/utils/strings.d.ts",
111+
]
112+
`);
113+
});
93114
});
94115

95116
describe('dts when bundle: true', () => {
@@ -309,7 +330,7 @@ describe('dts when build: true', () => {
309330
expect(isSuccess).toBe(true);
310331
});
311332

312-
test('tsconfig missing some fields', async () => {
333+
test('tsconfig missing some fields - declarationDir or outDir', async () => {
313334
const fixturePath = join(__dirname, 'composite', 'tsconfig');
314335
try {
315336
await buildAndGetResults({

0 commit comments

Comments
 (0)