@@ -9,19 +9,26 @@ const fileList = (watched: Record<string, Array<string>>) =>
9
9
10
10
const start = async ( base : string ) => {
11
11
const watcher = watch ( [ 'middleware.js' , 'middleware.ts' , 'src/middleware.js' , 'src/middleware.ts' ] , {
12
+ // Try and ensure renames just emit one event
12
13
atomic : true ,
14
+ // Don't emit for every watched file, just once after the scan is done
13
15
ignoreInitial : true ,
14
16
cwd : base ,
15
17
} )
16
18
17
19
const update = async ( initial = false ) => {
18
20
try {
21
+ // Start by deleting the old file. If we error out, we don't want to leave the old file around
19
22
await promises . unlink ( join ( base , '.netlify' , 'middleware.js' ) )
20
- } catch { }
21
-
23
+ } catch {
24
+ // Ignore, because it's fine if it didn't exist
25
+ }
26
+ // The list of watched files is an object with the directory as the key and an array of files as the value.
27
+ // We need to flatten this into a list of files
22
28
const watchedFiles = fileList ( watcher . getWatched ( ) )
23
29
if ( watchedFiles . length === 0 ) {
24
30
if ( ! initial ) {
31
+ // Only log on subsequent builds, because having it on first build makes it seem like a warning, when it's a normal state
25
32
console . log ( 'No middleware found' )
26
33
}
27
34
return
@@ -30,7 +37,7 @@ const start = async (base: string) => {
30
37
console . log ( 'Multiple middleware files found:' )
31
38
console . log ( watchedFiles . join ( '\n' ) )
32
39
console . log ( 'This is not supported.' )
33
-
40
+ // Return without compiling anything
34
41
return
35
42
}
36
43
console . log ( `${ initial ? 'Building' : 'Rebuilding' } middleware ${ watchedFiles [ 0 ] } ...` )
@@ -66,6 +73,7 @@ const start = async (base: string) => {
66
73
} )
67
74
. on ( 'ready' , ( ) => {
68
75
console . log ( 'Initial scan complete. Ready for changes' )
76
+ // This only happens on the first scan
69
77
update ( true )
70
78
} )
71
79
await new Promise ( ( resolve ) => {
0 commit comments