@@ -4,7 +4,13 @@ import { fileURLToPath } from 'node:url';
44import { type RsbuildConfig , type RsbuildPlugin , logger } from '@rsbuild/core' ;
55import color from 'picocolors' ;
66import 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
915const __filename = fileURLToPath ( import . meta. url ) ;
1016const __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,
0 commit comments