-
Notifications
You must be signed in to change notification settings - Fork 88
fix: use separate watcher script for middleware in dev #1831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
0eb9eb1
fix: watch middleware in src folder
ascorbic 7b1bc45
fix: move watcher into subprocess
ascorbic d34718d
chore: add comments
ascorbic b94f132
chore: use join
ascorbic 848faee
Merge branch 'main' into mk/dev-watch
ascorbic bc0c49d
chore: changes from review
ascorbic 3058c24
feat: add support for running watcher once
ascorbic 6795c91
chore: add onPreDev tests
ascorbic 6cab2cc
chore: add watcher tests
ascorbic 6562611
chore: add more tests
ascorbic 5013442
chore: snap
ascorbic 82f5f71
chore: remove duplicates
ascorbic c61caf0
chore: run jest serially
ascorbic 514cb1d
chore: skip the watcher test
ascorbic 4dac4a2
chore: re-enable some tests
ascorbic 5d6a551
chore: enable more
ascorbic bd8bf3d
chore: wait to kill processes
ascorbic 75181f7
chore: re-enable tests
ascorbic 1f58588
chore: try and make it testable
ascorbic 839771b
chore: just one worker
ascorbic e20dfe3
chore: disable predev test
ascorbic 0f9e28b
chore: so much logging
ascorbic 7bbf3ed
Merge branch 'main' into mk/dev-watch
ericapisani ff45167
Merge remote-tracking branch 'origin/main' into mk/dev-watch
nickytonline 8b8ed08
chore: added some comments and renamed some functions
nickytonline 559b48d
Update test/index.spec.js
nickytonline 29dad6f
chore: removed some white space
nickytonline f88a68d
chore: fixed tests
nickytonline 3d26c71
test: skipping some tests for onPreDev middleware as they only run su…
nickytonline 713a740
chore: removed console.logs that were for debugging
nickytonline ab838e9
chore: added another test to skip for now
nickytonline c64bb95
Merge branch 'main' into mk/dev-watch
nickytonline File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { promises } from 'fs' | ||
import { join } from 'path' | ||
|
||
import { build } from '@netlify/esbuild' | ||
import { watch } from 'chokidar' | ||
|
||
const fileList = (watched: Record<string, Array<string>>) => | ||
Object.entries(watched).flatMap(([dir, files]) => files.map((file) => join(dir, file))) | ||
|
||
const start = async (base: string) => { | ||
const watcher = watch(['middleware.js', 'middleware.ts', 'src/middleware.js', 'src/middleware.ts'], { | ||
// Try and ensure renames just emit one event | ||
atomic: true, | ||
// Don't emit for every watched file, just once after the scan is done | ||
ignoreInitial: true, | ||
cwd: base, | ||
}) | ||
|
||
const update = async (initial = false) => { | ||
try { | ||
// Start by deleting the old file. If we error out, we don't want to leave the old file around | ||
await promises.unlink(join(base, '.netlify', 'middleware.js')) | ||
} catch { | ||
// Ignore, because it's fine if it didn't exist | ||
} | ||
// The list of watched files is an object with the directory as the key and an array of files as the value. | ||
// We need to flatten this into a list of files | ||
const watchedFiles = fileList(watcher.getWatched()) | ||
if (watchedFiles.length === 0) { | ||
if (!initial) { | ||
// Only log on subsequent builds, because having it on first build makes it seem like a warning, when it's a normal state | ||
console.log('No middleware found') | ||
} | ||
return | ||
} | ||
if (watchedFiles.length > 1) { | ||
console.log('Multiple middleware files found:') | ||
console.log(watchedFiles.join('\n')) | ||
console.log('This is not supported.') | ||
// Return without compiling anything | ||
return | ||
} | ||
console.log(`${initial ? 'Building' : 'Rebuilding'} middleware ${watchedFiles[0]}...`) | ||
try { | ||
await build({ | ||
entryPoints: watchedFiles, | ||
outfile: join(base, '.netlify', 'middleware.js'), | ||
bundle: true, | ||
format: 'esm', | ||
target: 'esnext', | ||
}) | ||
} catch (error) { | ||
console.error(error) | ||
return | ||
} | ||
|
||
console.log('...done') | ||
} | ||
|
||
try { | ||
watcher | ||
.on('change', (path) => { | ||
console.log(`File ${path} has been changed`) | ||
update() | ||
}) | ||
.on('add', (path) => { | ||
console.log(`File ${path} has been added`) | ||
update() | ||
}) | ||
.on('unlink', (path) => { | ||
console.log(`File ${path} has been removed`) | ||
update() | ||
}) | ||
.on('ready', () => { | ||
console.log('Initial scan complete. Ready for changes') | ||
// This only happens on the first scan | ||
update(true) | ||
}) | ||
await new Promise((resolve) => { | ||
watcher.on('close', resolve) | ||
}) | ||
} finally { | ||
await watcher.close() | ||
} | ||
} | ||
|
||
start(process.argv[2]).catch((error) => { | ||
console.error(error) | ||
// eslint-disable-next-line n/no-process-exit | ||
process.exit(1) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading the contents of the
update
method, I think this could be broken down into several helper functions and then have them be invoked withinupdate
:removeOldMiddlewareFile
:The logging in the branches of
watchedFiles.length === 0
andwatchedFiles.length > 1
could be it's own distinct method, with thereturn
statement happening after that logging is done if necessary.buildMiddlewareFile
: