From e5807c4bc06be3718f4bd6aa68fb7c7d1aca2a22 Mon Sep 17 00:00:00 2001 From: Russell Davis <551404+russelldavis@users.noreply.github.com> Date: Wed, 4 Jan 2023 08:09:15 -0800 Subject: [PATCH] fix(css): fix stale css when reloading with hmr disabled (#10270) (#11506) --- packages/vite/src/node/server/hmr.ts | 21 ++----------- packages/vite/src/node/server/moduleGraph.ts | 33 +++++++++++--------- 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index 82fbf1d99a7522..0ba737f304774e 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -128,7 +128,7 @@ export function updateModules( file: string, modules: ModuleNode[], timestamp: number, - { config, ws }: ViteDevServer, + { config, ws, moduleGraph }: ViteDevServer, afterInvalidation?: boolean, ): void { const updates: Update[] = [] @@ -136,7 +136,7 @@ export function updateModules( let needFullReload = false for (const mod of modules) { - invalidate(mod, timestamp, invalidatedModules) + moduleGraph.invalidateModule(mod, invalidatedModules, timestamp, true) if (needFullReload) { continue } @@ -317,23 +317,6 @@ function propagateUpdate( return false } -function invalidate(mod: ModuleNode, timestamp: number, seen: Set) { - if (seen.has(mod)) { - return - } - seen.add(mod) - mod.lastHMRTimestamp = timestamp - mod.transformResult = null - mod.ssrModule = null - mod.ssrError = null - mod.ssrTransformResult = null - mod.importers.forEach((importer) => { - if (!importer.acceptedHmrDeps.has(mod)) { - invalidate(importer, timestamp, seen) - } - }) -} - export function handlePrunedModules( mods: Set, { ws }: ViteDevServer, diff --git a/packages/vite/src/node/server/moduleGraph.ts b/packages/vite/src/node/server/moduleGraph.ts index 895ebe02f4960d..4db3bcbca9a0f4 100644 --- a/packages/vite/src/node/server/moduleGraph.ts +++ b/packages/vite/src/node/server/moduleGraph.ts @@ -48,16 +48,6 @@ export class ModuleNode { } } -function invalidateSSRModule(mod: ModuleNode, seen: Set) { - if (seen.has(mod)) { - return - } - seen.add(mod) - mod.ssrModule = null - mod.ssrError = null - mod.importers.forEach((importer) => invalidateSSRModule(importer, seen)) -} - export type ResolvedUrl = [ url: string, resolvedId: string, @@ -108,15 +98,30 @@ export class ModuleGraph { mod: ModuleNode, seen: Set = new Set(), timestamp: number = Date.now(), + isHmr: boolean = false, ): void { - // Save the timestamp for this invalidation, so we can avoid caching the result of possible already started - // processing being done for this module - mod.lastInvalidationTimestamp = timestamp + if (seen.has(mod)) { + return + } + seen.add(mod) + if (isHmr) { + mod.lastHMRTimestamp = timestamp + } else { + // Save the timestamp for this invalidation, so we can avoid caching the result of possible already started + // processing being done for this module + mod.lastInvalidationTimestamp = timestamp + } // Don't invalidate mod.info and mod.meta, as they are part of the processing pipeline // Invalidating the transform result is enough to ensure this module is re-processed next time it is requested mod.transformResult = null mod.ssrTransformResult = null - invalidateSSRModule(mod, seen) + mod.ssrModule = null + mod.ssrError = null + mod.importers.forEach((importer) => { + if (!importer.acceptedHmrDeps.has(mod)) { + this.invalidateModule(importer, seen, timestamp, isHmr) + } + }) } invalidateAll(): void {