Skip to content

Commit 88b41b1

Browse files
authored
fix: clean dts distPath earlier (#752)
1 parent 6c57682 commit 88b41b1

File tree

9 files changed

+97
-33
lines changed

9 files changed

+97
-33
lines changed

Diff for: packages/plugin-dts/src/dts.ts

+2-28
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@ import { logger } from '@rsbuild/core';
1212
import color from 'picocolors';
1313
import type { DtsGenOptions } from './index';
1414
import { emitDts } from './tsc';
15-
import {
16-
calcLongestCommonPath,
17-
cleanDtsFiles,
18-
cleanTsBuildInfoFile,
19-
clearTempDeclarationDir,
20-
ensureTempDeclarationDir,
21-
} from './utils';
15+
import { calcLongestCommonPath, ensureTempDeclarationDir } from './utils';
2216

2317
const isObject = (obj: unknown): obj is Record<string, any> =>
2418
Object.prototype.toString.call(obj) === '[object Object]';
@@ -115,11 +109,9 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
115109
const {
116110
bundle,
117111
dtsEntry,
112+
dtsEmitPath,
118113
tsconfigPath,
119114
tsConfigResult,
120-
distPath,
121-
rootDistPath,
122-
cleanDistPath,
123115
name,
124116
cwd,
125117
build,
@@ -138,24 +130,6 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
138130

139131
const { options: rawCompilerOptions, fileNames } = tsConfigResult;
140132

141-
const dtsEmitPath =
142-
distPath ?? rawCompilerOptions.declarationDir ?? rootDistPath;
143-
144-
// clean dts files
145-
if (cleanDistPath !== false) {
146-
await cleanDtsFiles(dtsEmitPath);
147-
}
148-
149-
// clean .rslib temp folder
150-
if (bundle) {
151-
await clearTempDeclarationDir(cwd);
152-
}
153-
154-
// clean tsbuildinfo file
155-
if (rawCompilerOptions.composite || rawCompilerOptions.incremental || build) {
156-
await cleanTsBuildInfoFile(tsconfigPath, rawCompilerOptions);
157-
}
158-
159133
// The longest common path of all non-declaration input files.
160134
// If composite is set, the default is instead the directory containing the tsconfig.json file.
161135
// see https://www.typescriptlang.org/tsconfig/#rootDir

Diff for: packages/plugin-dts/src/index.ts

+33-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { fileURLToPath } from 'node:url';
44
import { type RsbuildConfig, type RsbuildPlugin, logger } from '@rsbuild/core';
55
import color from 'picocolors';
66
import ts from 'typescript';
7-
import { loadTsconfig, processSourceEntry } from './utils';
7+
import {
8+
cleanDtsFiles,
9+
cleanTsBuildInfoFile,
10+
clearTempDeclarationDir,
11+
loadTsconfig,
12+
processSourceEntry,
13+
} from './utils';
814

915
const __filename = fileURLToPath(import.meta.url);
1016
const __dirname = dirname(__filename);
@@ -42,11 +48,10 @@ export type DtsGenOptions = PluginDtsOptions & {
4248
cwd: string;
4349
isWatch: boolean;
4450
dtsEntry: DtsEntry;
51+
dtsEmitPath: string;
4552
build?: boolean;
4653
tsconfigPath: string;
4754
tsConfigResult: ts.ParsedCommandLine;
48-
rootDistPath: string;
49-
cleanDistPath: NonNullable<RsbuildConfig['output']>['cleanDistPath'];
5055
userExternals?: NonNullable<RsbuildConfig['output']>['externals'];
5156
};
5257

@@ -104,6 +109,30 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
104109
}
105110

106111
const tsConfigResult = loadTsconfig(tsconfigPath);
112+
const { options: rawCompilerOptions } = tsConfigResult;
113+
const dtsEmitPath =
114+
options.distPath ??
115+
rawCompilerOptions.declarationDir ??
116+
config.output?.distPath?.root;
117+
118+
// clean dts files
119+
if (config.output.cleanDistPath !== false) {
120+
await cleanDtsFiles(dtsEmitPath);
121+
}
122+
123+
// clean .rslib temp folder
124+
if (options.bundle) {
125+
await clearTempDeclarationDir(cwd);
126+
}
127+
128+
// clean tsbuildinfo file
129+
if (
130+
rawCompilerOptions.composite ||
131+
rawCompilerOptions.incremental ||
132+
options.build
133+
) {
134+
await cleanTsBuildInfoFile(tsconfigPath, rawCompilerOptions);
135+
}
107136

108137
const jsExtension = extname(__filename);
109138
const childProcess = fork(join(__dirname, `./dts${jsExtension}`), [], {
@@ -115,11 +144,10 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
115144
const dtsGenOptions: DtsGenOptions = {
116145
...options,
117146
dtsEntry,
118-
rootDistPath: config.output?.distPath?.root,
147+
dtsEmitPath,
119148
userExternals: config.output.externals,
120149
tsconfigPath,
121150
tsConfigResult,
122-
cleanDistPath: config.output.cleanDistPath,
123151
name: environment.name,
124152
cwd,
125153
isWatch,

Diff for: pnpm-lock.yaml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: tests/integration/dts/copy/copy.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export type Copy = {
2+
from: string;
3+
to: string;
4+
context: string;
5+
};

Diff for: tests/integration/dts/copy/package.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "dts-copy-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}

Diff for: tests/integration/dts/copy/rslib.config.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
dts: true,
8+
}),
9+
],
10+
output: {
11+
copy: {
12+
patterns: [
13+
{
14+
from: './copy.d.ts',
15+
to: './copy.d.ts',
16+
context: __dirname,
17+
},
18+
],
19+
},
20+
},
21+
});

Diff for: tests/integration/dts/copy/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const a = 'hello world';
2+
export type A = string;

Diff for: tests/integration/dts/copy/tsconfig.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "@rslib/tsconfig/base",
3+
"compilerOptions": {
4+
"baseUrl": "./",
5+
"rootDir": "src",
6+
"composite": true
7+
},
8+
"include": ["src"]
9+
}

Diff for: tests/integration/dts/index.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -590,3 +590,20 @@ describe('dts when composite: true', () => {
590590
expect(existsSync(buildInfoPath)).toBeTruthy();
591591
});
592592
});
593+
594+
describe('use with other features', async () => {
595+
test('use output.copy to copy dts files', async () => {
596+
const fixturePath = join(__dirname, 'copy');
597+
const { files } = await buildAndGetResults({
598+
fixturePath,
599+
type: 'dts',
600+
});
601+
602+
expect(files.esm).toMatchInlineSnapshot(`
603+
[
604+
"<ROOT>/tests/integration/dts/copy/dist/esm/copy.d.ts",
605+
"<ROOT>/tests/integration/dts/copy/dist/esm/index.d.ts",
606+
]
607+
`);
608+
});
609+
});

0 commit comments

Comments
 (0)