diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index 0ba737f304774e..c0accc63e5bc71 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -42,6 +42,7 @@ export function getShortName(file: string, root: string): string { export async function handleHMRUpdate( file: string, server: ViteDevServer, + configOnly: boolean, ): Promise { const { ws, config, moduleGraph } = server const shortFile = getShortName(file, config.root) @@ -71,6 +72,10 @@ export async function handleHMRUpdate( return } + if (configOnly) { + return + } + debugHmr(`[file change] ${colors.dim(shortFile)}`) // (dev only) the client itself cannot be hot updated. diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 992c35b85b3d38..68d7b78796dd6e 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -484,16 +484,10 @@ export async function createServer( return setPackageData(id, pkg) } - watcher.on('change', async (file) => { - file = normalizePath(file) - if (file.endsWith('/package.json')) { - return invalidatePackageData(packageCache, file) - } - // invalidate module graph cache on file change - moduleGraph.onFileChange(file) + const onHMRUpdate = async (file: string, configOnly: boolean) => { if (serverConfig.hmr !== false) { try { - await handleHMRUpdate(file, server) + await handleHMRUpdate(file, server, configOnly) } catch (err) { ws.send({ type: 'error', @@ -501,15 +495,28 @@ export async function createServer( }) } } - }) + } - watcher.on('add', (file) => { - handleFileAddUnlink(normalizePath(file), server) - }) - watcher.on('unlink', (file) => { - handleFileAddUnlink(normalizePath(file), server) + const onFileAddUnlink = async (file: string) => { + file = normalizePath(file) + await handleFileAddUnlink(file, server) + await onHMRUpdate(file, true) + } + + watcher.on('change', async (file) => { + file = normalizePath(file) + if (file.endsWith('/package.json')) { + return invalidatePackageData(packageCache, file) + } + // invalidate module graph cache on file change + moduleGraph.onFileChange(file) + + await onHMRUpdate(file, false) }) + watcher.on('add', onFileAddUnlink) + watcher.on('unlink', onFileAddUnlink) + ws.on('vite:invalidate', async ({ path, message }: InvalidatePayload) => { const mod = moduleGraph.urlToModuleMap.get(path) if (mod && mod.isSelfAccepting && mod.lastHMRTimestamp > 0) {