-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
fix(gatsby): work around webpack watching issue #30194
Conversation
callback({ type: `SOURCE_FILE_CHANGED`, file }) | ||
|
||
// Webpack fires `invalid` event as soon as any file changes | ||
// but it doesn't start recompiling at this point. Instead, it aggregates changes with debounce | ||
// and recompile on a tail of debounce. | ||
// For example, you may save a file multiple times quickly but webpack will only recompile once. | ||
// Unfortunately webpack doesn't expose any event for aggregated changes. | ||
// But we actually need it. If we start recompiling immediately on `invalid` we can miss some changes. | ||
// Hack below is a workaround for this missing webpack event | ||
// @ts-ignore | ||
const watcher = compiler.watchFileSystem.watcher // Watchpack instance | ||
watcher.once(`aggregated`, () => { | ||
callback({ type: `SOURCE_FILE_CHANGED`, file }) | ||
}) | ||
|
||
// TODO: fallback to timeout? |
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.
from my own investigations - sometimes webpack/watchpack can emit aggregated
without emitting invalid
before - should this maybe be hoisted and just listening to watcher.on('aggregated')
and just avoid using hooks.invalid
?
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.
It is tricky because each compiliation has a new compiler.watchFileSystem.watcher
instance (see here). And we want to listen to current active instance. But also - if it doesn't emit invalid
- how do we even react to this now? Does it mean we don't emit SOURCE_FILE_CHANGED
at all? Or we do but in query-watcher?
The correct fix for this shipped in webpack: webpack/webpack#13399 So closing. |
Description
This PR aims to fix "double save" issues in Gatsby (when some saves are being lost). The issue is caused by
resume
andsuspend
flow of webpack. There are (at least) two issues in webpack itself with this flow that make it hard to make use ofsuspend
andresume
at random points in time:webpack/webpack#12882
webpack/webpack#12898
If those issues are fixed, then this PR won't be needed. Otherwise, we will have to properly coordinate calls of
resume
andsuspend
to make sure they are invoked at specific moments (and when webpack is in a specific state).I've published a canary
gatsby@3.1.0-alpha-hmr-fix.16+831f9a9d64
with this fix for anyone who wants to try it.Related Issues
Fixes #27609