From 10987e0471b8b96d2ed56ba5b5a5ef76a07bfbf9 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sat, 13 Sep 2025 17:26:55 +0900 Subject: [PATCH 1/2] refactor(rsc): use `addWatchFile` to invalidate server css virtual --- packages/plugin-rsc/src/plugin.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/plugin-rsc/src/plugin.ts b/packages/plugin-rsc/src/plugin.ts index acabe24c..50e9099e 100644 --- a/packages/plugin-rsc/src/plugin.ts +++ b/packages/plugin-rsc/src/plugin.ts @@ -2100,6 +2100,7 @@ function vitePluginRscCss( if (parsed?.type === 'rsc') { assert(this.environment.name === 'rsc') const importer = parsed.id + this.addWatchFile(importer) if (this.environment.mode === 'dev') { const result = collectCss(server.environments.rsc!, importer) const cssHrefs = result.hrefs.map((href) => href.slice(1)) @@ -2124,6 +2125,7 @@ function vitePluginRscCss( } }, hotUpdate(ctx) { + if (1) return if (this.environment.name === 'rsc') { const { server } = manager const mods = collectModuleDependents(ctx.modules) From 8adfeb5e76ee4cf99c2969c6b6dca78bae5c3c5f Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sun, 14 Sep 2025 15:15:37 +0900 Subject: [PATCH 2/2] fix: addWatchFile all dependencies --- packages/plugin-rsc/src/plugin.ts | 42 +++---------------------------- 1 file changed, 3 insertions(+), 39 deletions(-) diff --git a/packages/plugin-rsc/src/plugin.ts b/packages/plugin-rsc/src/plugin.ts index 50e9099e..a89b3fb8 100644 --- a/packages/plugin-rsc/src/plugin.ts +++ b/packages/plugin-rsc/src/plugin.ts @@ -2100,9 +2100,11 @@ function vitePluginRscCss( if (parsed?.type === 'rsc') { assert(this.environment.name === 'rsc') const importer = parsed.id - this.addWatchFile(importer) if (this.environment.mode === 'dev') { const result = collectCss(server.environments.rsc!, importer) + for (const file of [importer, ...result.visitedFiles]) { + this.addWatchFile(file) + } const cssHrefs = result.hrefs.map((href) => href.slice(1)) const deps = assetsURLOfDeps({ css: cssHrefs, js: [] }, manager) return generateResourcesCode( @@ -2124,21 +2126,6 @@ function vitePluginRscCss( } } }, - hotUpdate(ctx) { - if (1) return - if (this.environment.name === 'rsc') { - const { server } = manager - const mods = collectModuleDependents(ctx.modules) - for (const mod of mods) { - if (mod.id) { - invalidteModuleById( - server.environments.rsc!, - `\0` + toCssVirtual({ id: mod.id, type: 'rsc' }), - ) - } - } - } - }, }, createVirtualPlugin( 'vite-rsc/remove-duplicate-server-css', @@ -2173,29 +2160,6 @@ export default function RemoveDuplicateServerCss() { ] } -function invalidteModuleById(environment: DevEnvironment, id: string) { - const mod = environment.moduleGraph.getModuleById(id) - if (mod) { - environment.moduleGraph.invalidateModule(mod) - } - return mod -} - -function collectModuleDependents(mods: EnvironmentModuleNode[]) { - const visited = new Set() - function recurse(mod: EnvironmentModuleNode) { - if (visited.has(mod)) return - visited.add(mod) - for (const importer of mod.importers) { - recurse(importer) - } - } - for (const mod of mods) { - recurse(mod) - } - return [...visited] -} - function generateResourcesCode(depsCode: string, manager: RscPluginManager) { const ResourcesFn = ( React: typeof import('react'),