@@ -5,6 +5,7 @@ import { build } from './build';
55import { loadRslibConfig } from './init' ;
66import { inspect } from './inspect' ;
77import { startMFDevServer } from './mf' ;
8+ import { watchFilesForRestart } from './restart' ;
89
910export type CommonOptions = {
1011 root ?: string ;
@@ -62,11 +63,20 @@ export function runCli(): void {
6263 . description ( 'build the library for production' )
6364 . action ( async ( options : BuildOptions ) => {
6465 try {
65- const rslibConfig = await loadRslibConfig ( options ) ;
66- await build ( rslibConfig , {
67- lib : options . lib ,
68- watch : options . watch ,
69- } ) ;
66+ const cliBuild = async ( ) => {
67+ const { content : rslibConfig , filePath } =
68+ await loadRslibConfig ( options ) ;
69+
70+ await build ( rslibConfig , options ) ;
71+
72+ if ( options . watch ) {
73+ watchFilesForRestart ( [ filePath ] , async ( ) => {
74+ await cliBuild ( ) ;
75+ } ) ;
76+ }
77+ } ;
78+
79+ await cliBuild ( ) ;
7080 } catch ( err ) {
7181 logger . error ( 'Failed to build.' ) ;
7282 logger . error ( err ) ;
@@ -90,7 +100,7 @@ export function runCli(): void {
90100 . action ( async ( options : InspectOptions ) => {
91101 try {
92102 // TODO: inspect should output Rslib's config
93- const rslibConfig = await loadRslibConfig ( options ) ;
103+ const { content : rslibConfig } = await loadRslibConfig ( options ) ;
94104 await inspect ( rslibConfig , {
95105 lib : options . lib ,
96106 mode : options . mode ,
@@ -108,9 +118,18 @@ export function runCli(): void {
108118 . description ( 'start Rsbuild dev server of Module Federation format' )
109119 . action ( async ( options : CommonOptions ) => {
110120 try {
111- const rslibConfig = await loadRslibConfig ( options ) ;
112- // TODO: support lib option in mf dev server
113- await startMFDevServer ( rslibConfig ) ;
121+ const cliMfDev = async ( ) => {
122+ const { content : rslibConfig , filePath } =
123+ await loadRslibConfig ( options ) ;
124+ // TODO: support lib option in mf dev server
125+ await startMFDevServer ( rslibConfig ) ;
126+
127+ watchFilesForRestart ( [ filePath ] , async ( ) => {
128+ await cliMfDev ( ) ;
129+ } ) ;
130+ } ;
131+
132+ await cliMfDev ( ) ;
114133 } catch ( err ) {
115134 logger . error ( 'Failed to start mf dev.' ) ;
116135 logger . error ( err ) ;
0 commit comments