@@ -4,7 +4,13 @@ import { fileURLToPath } from 'node:url';
4
4
import { type RsbuildConfig , type RsbuildPlugin , logger } from '@rsbuild/core' ;
5
5
import color from 'picocolors' ;
6
6
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' ;
8
14
9
15
const __filename = fileURLToPath ( import . meta. url ) ;
10
16
const __dirname = dirname ( __filename ) ;
@@ -42,11 +48,10 @@ export type DtsGenOptions = PluginDtsOptions & {
42
48
cwd : string ;
43
49
isWatch : boolean ;
44
50
dtsEntry : DtsEntry ;
51
+ dtsEmitPath : string ;
45
52
build ?: boolean ;
46
53
tsconfigPath : string ;
47
54
tsConfigResult : ts . ParsedCommandLine ;
48
- rootDistPath : string ;
49
- cleanDistPath : NonNullable < RsbuildConfig [ 'output' ] > [ 'cleanDistPath' ] ;
50
55
userExternals ?: NonNullable < RsbuildConfig [ 'output' ] > [ 'externals' ] ;
51
56
} ;
52
57
@@ -104,6 +109,30 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
104
109
}
105
110
106
111
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
+ }
107
136
108
137
const jsExtension = extname ( __filename ) ;
109
138
const childProcess = fork ( join ( __dirname , `./dts${ jsExtension } ` ) , [ ] , {
@@ -115,11 +144,10 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
115
144
const dtsGenOptions : DtsGenOptions = {
116
145
...options ,
117
146
dtsEntry,
118
- rootDistPath : config . output ?. distPath ?. root ,
147
+ dtsEmitPath ,
119
148
userExternals : config . output . externals ,
120
149
tsconfigPath,
121
150
tsConfigResult,
122
- cleanDistPath : config . output . cleanDistPath ,
123
151
name : environment . name ,
124
152
cwd,
125
153
isWatch,
0 commit comments