@@ -889,21 +889,41 @@ async function buildEnvironment(
889889 prepareOutDir ( resolvedOutDirs , emptyOutDir , environment )
890890 }
891891
892- const res : RolldownOutput [ ] = [ ]
893- for ( const output of normalizedOutputs ) {
894- res . push ( await bundle [ options . write ? 'write' : 'generate' ] ( output ) )
895- }
892+ const res = await build ( )
893+
896894 logger . info (
897895 `${ colors . green ( `✓ built in ${ displayTime ( Date . now ( ) - startTime ) } ` ) } ` ,
898896 )
899897
900- if ( server ) {
901- for ( const output of res ) {
902- for ( const outputFile of output . output ) {
903- server . memoryFiles [ outputFile . fileName ] = outputFile . type === 'chunk' ? outputFile . code : outputFile . source ;
898+ async function build ( ) {
899+ const res : RolldownOutput [ ] = [ ]
900+ for ( const output of normalizedOutputs ) {
901+ // TODO(underfin): using the generate at development build could be improve performance.
902+ res . push ( await bundle ! [ options . write ? 'write' : 'generate' ] ( output ) )
903+ }
904+
905+ if ( server ) {
906+ // watching the files
907+ for ( const file of bundle ! . watchFiles ) {
908+ if ( path . isAbsolute ( file ) && fs . existsSync ( file ) ) {
909+ server . watcher . add ( file )
910+ }
911+ }
912+
913+ // Write the output files to memory
914+ for ( const output of res ) {
915+ for ( const outputFile of output . output ) {
916+ server . memoryFiles [ outputFile . fileName ] = outputFile . type === 'chunk' ? outputFile . code : outputFile . source ;
917+ }
904918 }
905919 }
920+ return res
921+ }
922+
923+
924+ if ( server ) {
906925 server . watcher . on ( 'change' , async ( file ) => {
926+ const startTime = Date . now ( )
907927 const patch = await bundle ! . generateHmrPatch ( [ file ] ) ;
908928 if ( patch ) {
909929 const url = `${ Date . now ( ) } .js` ;
@@ -913,10 +933,14 @@ async function buildEnvironment(
913933 server . ws . send ( {
914934 type : 'update' ,
915935 url
916- } )
936+ } ) ;
937+ logger . info (
938+ `${ colors . green ( `✓ Found ${ path . relative ( root , file ) } changed, rebuilt in ${ displayTime ( Date . now ( ) - startTime ) } ` ) } ` ,
939+ )
940+
941+ await build ( ) ;
917942 }
918943 } )
919- // server.watcher = watcher
920944 }
921945
922946 return Array . isArray ( outputs ) ? res : res [ 0 ]
@@ -931,7 +955,8 @@ async function buildEnvironment(
931955 }
932956 throw e
933957 } finally {
934- if ( bundle ) await bundle . close ( )
958+ // close the bundle will make the rolldown hmr invalid, so dev build need to disable it.
959+ if ( bundle && ! server ) await bundle . close ( )
935960 }
936961}
937962
0 commit comments