Skip to content

Commit d34718d

Browse files
committed
chore: add comments
1 parent 7b1bc45 commit d34718d

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

packages/runtime/src/helpers/watcher.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,26 @@ const fileList = (watched: Record<string, Array<string>>) =>
99

1010
const start = async (base: string) => {
1111
const watcher = watch(['middleware.js', 'middleware.ts', 'src/middleware.js', 'src/middleware.ts'], {
12+
// Try and ensure renames just emit one event
1213
atomic: true,
14+
// Don't emit for every watched file, just once after the scan is done
1315
ignoreInitial: true,
1416
cwd: base,
1517
})
1618

1719
const update = async (initial = false) => {
1820
try {
21+
// Start by deleting the old file. If we error out, we don't want to leave the old file around
1922
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
2228
const watchedFiles = fileList(watcher.getWatched())
2329
if (watchedFiles.length === 0) {
2430
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
2532
console.log('No middleware found')
2633
}
2734
return
@@ -30,7 +37,7 @@ const start = async (base: string) => {
3037
console.log('Multiple middleware files found:')
3138
console.log(watchedFiles.join('\n'))
3239
console.log('This is not supported.')
33-
40+
// Return without compiling anything
3441
return
3542
}
3643
console.log(`${initial ? 'Building' : 'Rebuilding'} middleware ${watchedFiles[0]}...`)
@@ -66,6 +73,7 @@ const start = async (base: string) => {
6673
})
6774
.on('ready', () => {
6875
console.log('Initial scan complete. Ready for changes')
76+
// This only happens on the first scan
6977
update(true)
7078
})
7179
await new Promise((resolve) => {

0 commit comments

Comments
 (0)