1+ import { fork } from 'node:child_process' ;
12import { basename , dirname , join , relative } from 'node:path' ;
2- import type { RsbuildPlugin } from '@rsbuild/core' ;
3+ import { type RsbuildPlugin , logger } from '@rsbuild/core' ;
34import { bundleDts } from './apiExtractor' ;
45import { emitDts } from './tsc' ;
56import { ensureTempDeclarationDir , loadTsconfig } from './utils' ;
@@ -24,78 +25,86 @@ export const pluginDts = (
2425 name : PLUGIN_DTS_NAME ,
2526
2627 setup ( api ) {
27- const { tsconfigPath, distPath, bundle, isWatch } = options ;
28- let isFirstCompile = true ;
29-
3028 api . onBeforeBuild ( async ( { environments } ) => {
31- const cwd = process . cwd ( ) ;
32- const configPath = tsconfigPath
33- ? join ( cwd , tsconfigPath )
34- : join ( cwd , 'tsconfig.json' ) ;
35- const { options : rawCompilerOptions } = loadTsconfig ( configPath ) ;
36- const rootDir = rawCompilerOptions . rootDir ?? 'src' ;
37- const outDir = distPath
38- ? distPath
39- : rawCompilerOptions . declarationDir || './dist' ;
29+ const childProcess = fork ( __filename , [ ] , {
30+ stdio : 'inherit' ,
31+ } ) ;
4032
41- const getDeclarationDir = ( bundle : boolean , distPath ?: string ) => {
42- if ( bundle ) {
43- return ensureTempDeclarationDir ( ) ;
44- }
45- return distPath ? distPath : rawCompilerOptions . declarationDir ;
46- } ;
33+ childProcess . send ( { options, environments } ) ;
34+ } ) ;
35+ } ,
36+ } ) ;
4737
48- const declarationDir = getDeclarationDir ( bundle , distPath ) || './dist' ;
49- let entry = '' ;
38+ if ( process . send ) {
39+ process . on ( 'message' , async ( data ) => {
40+ logger . info ( 'Generating DTS...' ) ;
41+ const { options : pluginOptions , environments } = data as any ;
42+ const cwd = process . cwd ( ) ;
43+ const configPath = pluginOptions . tsconfigPath
44+ ? join ( cwd , pluginOptions . tsconfigPath )
45+ : join ( cwd , 'tsconfig.json' ) ;
46+ const { options : rawCompilerOptions } = loadTsconfig ( configPath ) ;
47+ const rootDir = rawCompilerOptions . rootDir ?? 'src' ;
48+ const outDir = pluginOptions . distPath
49+ ? pluginOptions . distPath
50+ : rawCompilerOptions . declarationDir || './dist' ;
5051
51- if ( options . bundle === true ) {
52- const entrySourcePath = join (
53- cwd ,
54- environments . esm ?. config . source . entry ?. main as string ,
55- ) ;
56- const relativePath = relative ( rootDir , dirname ( entrySourcePath ) ) ;
57- entry = join (
58- declarationDir ! ,
59- relativePath ,
60- basename ( entrySourcePath ) ,
61- ) . replace ( / \. ( m ? j s | j s x ? | m ? t s | t s x ? | c ? j s ) $ / , '.d.ts' ) ;
52+ const getDeclarationDir = ( bundle : boolean , distPath ?: string ) => {
53+ if ( bundle ) {
54+ return ensureTempDeclarationDir ( ) ;
6255 }
56+ return distPath ? distPath : rawCompilerOptions . declarationDir ;
57+ } ;
6358
64- const onComplete = ( isSuccess : boolean ) => {
65- if ( isSuccess && options . bundle === true ) {
66- bundleDts ( {
67- cwd,
68- outDir,
69- entry,
70- tsconfigPath,
71- } ) ;
72- }
73- } ;
59+ const declarationDir =
60+ getDeclarationDir ( pluginOptions . bundle , pluginOptions . distPath ) ||
61+ './dist' ;
62+ let entry = '' ;
7463
75- emitDts (
76- {
77- cwd,
78- configPath,
79- rootDir,
80- declarationDir,
81- } ,
82- isFirstCompile ,
83- onComplete ,
84- isWatch ,
64+ if ( pluginOptions . bundle === true ) {
65+ const entrySourcePath = join (
66+ cwd ,
67+ environments . esm ?. config . source . entry ?. main as string ,
8568 ) ;
69+ const relativePath = relative ( rootDir , dirname ( entrySourcePath ) ) ;
70+ entry = join (
71+ declarationDir ! ,
72+ relativePath ,
73+ basename ( entrySourcePath ) ,
74+ ) . replace ( / \. ( m ? j s | j s x ? | m ? t s | t s x ? | c ? j s ) $ / , '.d.ts' ) ;
75+ }
8676
87- if ( options . bundle === true && ! isWatch ) {
77+ const onComplete = ( isSuccess : boolean ) => {
78+ if ( isSuccess && pluginOptions . bundle === true ) {
8879 bundleDts ( {
8980 cwd,
9081 outDir,
9182 entry,
92- tsconfigPath,
83+ tsconfigPath : pluginOptions . tsconfigPath ,
9384 } ) ;
9485 }
86+ } ;
9587
96- if ( isFirstCompile ) {
97- isFirstCompile = false ;
98- }
99- } ) ;
100- } ,
101- } ) ;
88+ emitDts (
89+ {
90+ cwd,
91+ configPath,
92+ rootDir,
93+ declarationDir,
94+ } ,
95+ onComplete ,
96+ pluginOptions . isWatch ,
97+ ) ;
98+
99+ if ( pluginOptions . bundle === true && ! pluginOptions . isWatch ) {
100+ bundleDts ( {
101+ cwd,
102+ outDir,
103+ entry,
104+ tsconfigPath : pluginOptions . tsconfigPath ,
105+ } ) ;
106+ }
107+
108+ // process.exit();
109+ } ) ;
110+ }
0 commit comments