Skip to content

Commit cc3724f

Browse files
authored
fix(server): watch env files creating and deleting (fix #12127) (#12129)
1 parent 2556f88 commit cc3724f

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

packages/vite/src/node/server/hmr.ts

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export function getShortName(file: string, root: string): string {
4242
export async function handleHMRUpdate(
4343
file: string,
4444
server: ViteDevServer,
45+
configOnly: boolean,
4546
): Promise<void> {
4647
const { ws, config, moduleGraph } = server
4748
const shortFile = getShortName(file, config.root)
@@ -71,6 +72,10 @@ export async function handleHMRUpdate(
7172
return
7273
}
7374

75+
if (configOnly) {
76+
return
77+
}
78+
7479
debugHmr(`[file change] ${colors.dim(shortFile)}`)
7580

7681
// (dev only) the client itself cannot be hot updated.

packages/vite/src/node/server/index.ts

+21-14
Original file line numberDiff line numberDiff line change
@@ -484,32 +484,39 @@ export async function createServer(
484484
return setPackageData(id, pkg)
485485
}
486486

487-
watcher.on('change', async (file) => {
488-
file = normalizePath(file)
489-
if (file.endsWith('/package.json')) {
490-
return invalidatePackageData(packageCache, file)
491-
}
492-
// invalidate module graph cache on file change
493-
moduleGraph.onFileChange(file)
487+
const onHMRUpdate = async (file: string, configOnly: boolean) => {
494488
if (serverConfig.hmr !== false) {
495489
try {
496-
await handleHMRUpdate(file, server)
490+
await handleHMRUpdate(file, server, configOnly)
497491
} catch (err) {
498492
ws.send({
499493
type: 'error',
500494
err: prepareError(err),
501495
})
502496
}
503497
}
504-
})
498+
}
505499

506-
watcher.on('add', (file) => {
507-
handleFileAddUnlink(normalizePath(file), server)
508-
})
509-
watcher.on('unlink', (file) => {
510-
handleFileAddUnlink(normalizePath(file), server)
500+
const onFileAddUnlink = async (file: string) => {
501+
file = normalizePath(file)
502+
await handleFileAddUnlink(file, server)
503+
await onHMRUpdate(file, true)
504+
}
505+
506+
watcher.on('change', async (file) => {
507+
file = normalizePath(file)
508+
if (file.endsWith('/package.json')) {
509+
return invalidatePackageData(packageCache, file)
510+
}
511+
// invalidate module graph cache on file change
512+
moduleGraph.onFileChange(file)
513+
514+
await onHMRUpdate(file, false)
511515
})
512516

517+
watcher.on('add', onFileAddUnlink)
518+
watcher.on('unlink', onFileAddUnlink)
519+
513520
ws.on('vite:invalidate', async ({ path, message }: InvalidatePayload) => {
514521
const mod = moduleGraph.urlToModuleMap.get(path)
515522
if (mod && mod.isSelfAccepting && mod.lastHMRTimestamp > 0) {

0 commit comments

Comments
 (0)