Skip to content
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: hmr with fsCache after concurrent file changes #16222

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 66 additions & 9 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,22 +768,33 @@ export async function _createServer(
await onHMRUpdate(isUnlink ? 'delete' : 'create', file)
}

watcher.on('change', async (file) => {
async function onFileChange(file: string) {
file = normalizePath(file)
await container.watchChange(file, { event: 'update' })
// invalidate module graph cache on file change
moduleGraph.onFileChange(file)
await onHMRUpdate('update', file)
})
}

getFsUtils(config).initWatcher?.(watcher)
const fsUtils = getFsUtils(config)

watcher.on('add', (file) => {
onFileAddUnlink(file, false)
})
watcher.on('unlink', (file) => {
onFileAddUnlink(file, true)
})
if (fsUtils.initWatcher) {
fsUtils.initWatcher(watcher)
setupDebouncedWatchEventsListeners(
watcher,
onFileAddUnlink,
onFileChange,
5,
)
} else {
watcher.on('add', (file) => {
onFileAddUnlink(file, false)
})
watcher.on('unlink', (file) => {
onFileAddUnlink(file, true)
})
watcher.on('change', onFileChange)
}

hot.on('vite:invalidate', async ({ path, message }) => {
const mod = moduleGraph.urlToModuleMap.get(path)
Expand Down Expand Up @@ -1260,3 +1271,49 @@ function setupOnCrawlEnd(onCrawlEnd: () => void): CrawlEndFinder {
cancel,
}
}

interface WatchEvent {
type: 'add' | 'unlink' | 'change'
file: string
}
function setupDebouncedWatchEventsListeners(
watcher: FSWatcher,
onFileAddUnlink: (file: string, isUnlink: boolean) => void,
onFileChange: (file: string) => void,
debounceMs: number,
) {
let watchEvents: WatchEvent[] = []
let processWatchEventsHandle: NodeJS.Timeout | null = null
function processWatchEvent(event: WatchEvent) {
switch (event.type) {
case 'add':
return onFileAddUnlink(event.file, true)
case 'unlink':
return onFileAddUnlink(event.file, false)
case 'change':
return onFileChange(event.file)
}
}
function debouncedProcessWatchEvents() {
if (processWatchEventsHandle) {
clearTimeout(processWatchEventsHandle)
processWatchEventsHandle = null
}
processWatchEventsHandle = setTimeout(() => {
watchEvents.forEach(processWatchEvent)
watchEvents = []
}, debounceMs)
}
watcher.on('add', (file) => {
watchEvents.push({ type: 'add', file })
debouncedProcessWatchEvents()
})
watcher.on('unlink', (file) => {
watchEvents.push({ type: 'unlink', file })
debouncedProcessWatchEvents()
})
watcher.on('change', (file) => {
watchEvents.push({ type: 'change', file })
debouncedProcessWatchEvents()
})
}