@@ -2,7 +2,14 @@ import { type ChildProcess, fork } from 'node:child_process';
22import { dirname , extname , join } from 'node:path' ;
33import { fileURLToPath } from 'node:url' ;
44import { type RsbuildConfig , type RsbuildPlugin , logger } from '@rsbuild/core' ;
5- import { processSourceEntry } from './utils' ;
5+ import ts from 'typescript' ;
6+ import {
7+ cleanDtsFiles ,
8+ cleanTsBuildInfoFile ,
9+ clearTempDeclarationDir ,
10+ loadTsconfig ,
11+ processSourceEntry ,
12+ } from './utils' ;
613
714const __filename = fileURLToPath ( import . meta. url ) ;
815const __dirname = dirname ( __filename ) ;
@@ -34,9 +41,10 @@ export type DtsGenOptions = PluginDtsOptions & {
3441 cwd : string ;
3542 isWatch : boolean ;
3643 dtsEntry : DtsEntry ;
37- rootDistPath : string ;
44+ dtsEmitPath : string ;
3845 build ?: boolean ;
39- tsconfigPath ?: string ;
46+ tsconfigPath : string ;
47+ tsConfigResult : ts . ParsedCommandLine ;
4048 userExternals ?: NonNullable < RsbuildConfig [ 'output' ] > [ 'externals' ] ;
4149} ;
4250
@@ -63,35 +71,69 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
6371 let childProcesses : ChildProcess [ ] = [ ] ;
6472
6573 api . onBeforeEnvironmentCompile (
66- ( { isWatch, isFirstCompile, environment } ) => {
74+ async ( { isWatch, isFirstCompile, environment } ) => {
6775 if ( ! isFirstCompile ) {
6876 return ;
6977 }
7078
7179 const { config } = environment ;
7280
81+ // TODO: @microsoft /api-extractor only support single entry to bundle DTS
82+ // use first element of Record<string, string> type entry config
83+ const dtsEntry = processSourceEntry (
84+ options . bundle ! ,
85+ config . source ?. entry ,
86+ ) ;
87+
88+ const cwd = api . context . rootPath ;
89+ const tsconfigPath = ts . findConfigFile (
90+ cwd ,
91+ ts . sys . fileExists ,
92+ config . source . tsconfigPath ,
93+ ) ;
94+
95+ if ( ! tsconfigPath ) {
96+ logger . error ( `tsconfig.json not found in ${ cwd } ` ) ;
97+ throw new Error ( ) ;
98+ }
99+
100+ const tsConfigResult = loadTsconfig ( tsconfigPath ) ;
101+ const dtsEmitPath =
102+ options . distPath ??
103+ tsConfigResult . options . declarationDir ??
104+ config . output ?. distPath ?. root ;
105+
73106 const jsExtension = extname ( __filename ) ;
74107 const childProcess = fork ( join ( __dirname , `./dts${ jsExtension } ` ) , [ ] , {
75108 stdio : 'inherit' ,
76109 } ) ;
77110
78111 childProcesses . push ( childProcess ) ;
79112
80- // TODO: @microsoft /api-extractor only support single entry to bundle DTS
81- // use first element of Record<string, string> type entry config
82- const dtsEntry = processSourceEntry (
83- options . bundle ! ,
84- config . source ?. entry ,
85- ) ;
113+ const { cleanDistPath } = config . output ;
114+
115+ // clean dts files
116+ if ( cleanDistPath !== false ) {
117+ await cleanDtsFiles ( dtsEmitPath ) ;
118+ }
119+
120+ // clean .rslib temp folder
121+ if ( options . bundle ) {
122+ await clearTempDeclarationDir ( cwd ) ;
123+ }
124+
125+ // clean tsbuildinfo file
126+ await cleanTsBuildInfoFile ( tsconfigPath , tsConfigResult ) ;
86127
87128 const dtsGenOptions : DtsGenOptions = {
88129 ...options ,
89130 dtsEntry,
131+ dtsEmitPath,
90132 userExternals : config . output . externals ,
91- rootDistPath : config . output ?. distPath ?. root ,
92- tsconfigPath : config . source . tsconfigPath ,
133+ tsconfigPath ,
134+ tsConfigResult ,
93135 name : environment . name ,
94- cwd : api . context . rootPath ,
136+ cwd,
95137 isWatch,
96138 } ;
97139
0 commit comments