@@ -6,39 +6,53 @@ import getConstants from "../constants";
6
6
const { LOG_PREFIX , SRC_DIR , IS_PRODUCTION } = getConstants ( ) ;
7
7
const LIVE_RELOAD_WEBSOCKET_PATH = "__brisa_live_reload__" ;
8
8
const LIVE_RELOAD_COMMAND = "reload" ;
9
+ let semaphore = false ;
10
+ let waitFilename = "" ;
9
11
10
12
if ( ! IS_PRODUCTION ) {
11
13
console . log ( LOG_PREFIX . INFO , "hot reloading enabled" ) ;
12
14
watch ( SRC_DIR , { recursive : true } , async ( event , filename ) => {
13
15
if ( event !== "change" ) return ;
14
16
15
17
console . log ( LOG_PREFIX . WAIT , `recompiling ${ filename } ...` ) ;
16
- globalThis . Loader . registry . clear ( ) ;
17
-
18
- const nsStart = Bun . nanoseconds ( ) ;
19
- const { exitCode, stderr } = Bun . spawnSync ( {
20
- cmd : [ process . execPath , path . join ( import . meta. dir , ".." , "build.js" ) ] ,
21
- env : process . env ,
22
- stderr : "pipe" ,
23
- stdout : "inherit" ,
24
- } ) ;
25
- const nsEnd = Bun . nanoseconds ( ) ;
26
- const ms = ( ( nsEnd - nsStart ) / 1000000 ) . toFixed ( 2 ) ;
27
-
28
- if ( exitCode !== 0 ) {
29
- console . log (
30
- LOG_PREFIX . ERROR ,
31
- `failed to recompile ${ filename } ` ,
32
- stderr . toString ( ) ,
33
- ) ;
34
- return ;
35
- }
36
-
37
- console . log ( LOG_PREFIX . READY , `recompiled successfully in ${ ms } ms` ) ;
38
- globalThis ?. ws ?. send ( LIVE_RELOAD_COMMAND ) ;
18
+ if ( semaphore ) waitFilename = filename as string ;
19
+ else recompile ( filename as string ) ;
39
20
} ) ;
40
21
}
41
22
23
+ function recompile ( filename : string ) {
24
+ semaphore = true ;
25
+ globalThis . Loader . registry . clear ( ) ;
26
+
27
+ const nsStart = Bun . nanoseconds ( ) ;
28
+ const { exitCode, stderr } = Bun . spawnSync ( {
29
+ cmd : [ process . execPath , path . join ( import . meta. dir , ".." , "build.js" ) ] ,
30
+ env : process . env ,
31
+ stderr : "pipe" ,
32
+ } ) ;
33
+ const nsEnd = Bun . nanoseconds ( ) ;
34
+ const ms = ( ( nsEnd - nsStart ) / 1000000 ) . toFixed ( 2 ) ;
35
+
36
+ if ( exitCode !== 0 ) {
37
+ console . log (
38
+ LOG_PREFIX . ERROR ,
39
+ `failed to recompile ${ filename } ` ,
40
+ stderr . toString ( ) ,
41
+ ) ;
42
+ semaphore = false ;
43
+ return ;
44
+ }
45
+
46
+ console . log ( LOG_PREFIX . READY , `recompiled successfully in ${ ms } ms` ) ;
47
+ globalThis ?. ws ?. send ( LIVE_RELOAD_COMMAND ) ;
48
+ if ( waitFilename ) {
49
+ let popFilename = waitFilename ;
50
+ waitFilename = "" ;
51
+ recompile ( popFilename ) ;
52
+ }
53
+ semaphore = false ;
54
+ }
55
+
42
56
export function LiveReloadScript ( {
43
57
port,
44
58
children,
0 commit comments