11import { fork } from 'node:child_process' ;
22import { basename , dirname , join , relative } from 'node:path' ;
3- import { type RsbuildPlugin , logger } from '@rsbuild/core' ;
3+ import {
4+ type EnvironmentContext ,
5+ type RsbuildPlugin ,
6+ logger ,
7+ } from '@rsbuild/core' ;
48import { bundleDts } from './apiExtractor' ;
59import { emitDts } from './tsc' ;
610import { ensureTempDeclarationDir , loadTsconfig } from './utils' ;
@@ -12,6 +16,11 @@ export type pluginDtsOptions = {
1216 isWatch ?: boolean ;
1317} ;
1418
19+ export type DATA = {
20+ options : pluginDtsOptions ;
21+ environments : Record < string , EnvironmentContext > ;
22+ } ;
23+
1524export const PLUGIN_DTS_NAME = 'rsbuild:dts' ;
1625
1726// use ts compiler API to generate bundleless dts
@@ -36,17 +45,18 @@ export const pluginDts = (
3645} ) ;
3746
3847if ( process . send ) {
39- process . on ( 'message' , async ( data ) => {
48+ process . on ( 'message' , async ( data : DATA ) => {
4049 logger . info ( 'Generating DTS...' ) ;
41- const { options : pluginOptions , environments } = data as any ;
50+ const { options : pluginOptions , environments } = data ;
51+ const { tsconfigPath, distPath, bundle, isWatch } = pluginOptions ;
4252 const cwd = process . cwd ( ) ;
43- const configPath = pluginOptions . tsconfigPath
44- ? join ( cwd , pluginOptions . tsconfigPath )
53+ const configPath = tsconfigPath
54+ ? join ( cwd , tsconfigPath )
4555 : join ( cwd , 'tsconfig.json' ) ;
4656 const { options : rawCompilerOptions } = loadTsconfig ( configPath ) ;
4757 const rootDir = rawCompilerOptions . rootDir ?? 'src' ;
48- const outDir = pluginOptions . distPath
49- ? pluginOptions . distPath
58+ const outDir = distPath
59+ ? distPath
5060 : rawCompilerOptions . declarationDir || './dist' ;
5161
5262 const getDeclarationDir = ( bundle : boolean , distPath ?: string ) => {
@@ -56,12 +66,10 @@ if (process.send) {
5666 return distPath ? distPath : rawCompilerOptions . declarationDir ;
5767 } ;
5868
59- const declarationDir =
60- getDeclarationDir ( pluginOptions . bundle , pluginOptions . distPath ) ||
61- './dist' ;
69+ const declarationDir = getDeclarationDir ( bundle , distPath ) || './dist' ;
6270 let entry = '' ;
6371
64- if ( pluginOptions . bundle === true ) {
72+ if ( bundle === true ) {
6573 const entrySourcePath = join (
6674 cwd ,
6775 environments . esm ?. config . source . entry ?. main as string ,
@@ -75,12 +83,12 @@ if (process.send) {
7583 }
7684
7785 const onComplete = ( isSuccess : boolean ) => {
78- if ( isSuccess && pluginOptions . bundle === true ) {
86+ if ( isSuccess && bundle === true ) {
7987 bundleDts ( {
8088 cwd,
8189 outDir,
8290 entry,
83- tsconfigPath : pluginOptions . tsconfigPath ,
91+ tsconfigPath,
8492 } ) ;
8593 }
8694 } ;
@@ -93,18 +101,19 @@ if (process.send) {
93101 declarationDir,
94102 } ,
95103 onComplete ,
96- pluginOptions . isWatch ,
104+ isWatch ,
97105 ) ;
98106
99- if ( pluginOptions . bundle === true && ! pluginOptions . isWatch ) {
107+ if ( bundle === true && ! isWatch ) {
100108 bundleDts ( {
101109 cwd,
102110 outDir,
103111 entry,
104- tsconfigPath : pluginOptions . tsconfigPath ,
112+ tsconfigPath,
105113 } ) ;
106114 }
107-
108- // process.exit();
115+ if ( ! isWatch ) {
116+ process . exit ( ) ;
117+ }
109118 } ) ;
110119}
0 commit comments