Skip to content

Commit 78a3f56

Browse files
authored
refactor(rsc): use addWatchFile to invalidate server css virtual (#847)
1 parent 2b7b90f commit 78a3f56

File tree

1 file changed

+3
-37
lines changed

1 file changed

+3
-37
lines changed

packages/plugin-rsc/src/plugin.ts

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,6 +2102,9 @@ function vitePluginRscCss(
21022102
const importer = parsed.id
21032103
if (this.environment.mode === 'dev') {
21042104
const result = collectCss(server.environments.rsc!, importer)
2105+
for (const file of [importer, ...result.visitedFiles]) {
2106+
this.addWatchFile(file)
2107+
}
21052108
const cssHrefs = result.hrefs.map((href) => href.slice(1))
21062109
const deps = assetsURLOfDeps({ css: cssHrefs, js: [] }, manager)
21072110
return generateResourcesCode(
@@ -2123,20 +2126,6 @@ function vitePluginRscCss(
21232126
}
21242127
}
21252128
},
2126-
hotUpdate(ctx) {
2127-
if (this.environment.name === 'rsc') {
2128-
const { server } = manager
2129-
const mods = collectModuleDependents(ctx.modules)
2130-
for (const mod of mods) {
2131-
if (mod.id) {
2132-
invalidteModuleById(
2133-
server.environments.rsc!,
2134-
`\0` + toCssVirtual({ id: mod.id, type: 'rsc' }),
2135-
)
2136-
}
2137-
}
2138-
}
2139-
},
21402129
},
21412130
createVirtualPlugin(
21422131
'vite-rsc/remove-duplicate-server-css',
@@ -2171,29 +2160,6 @@ export default function RemoveDuplicateServerCss() {
21712160
]
21722161
}
21732162

2174-
function invalidteModuleById(environment: DevEnvironment, id: string) {
2175-
const mod = environment.moduleGraph.getModuleById(id)
2176-
if (mod) {
2177-
environment.moduleGraph.invalidateModule(mod)
2178-
}
2179-
return mod
2180-
}
2181-
2182-
function collectModuleDependents(mods: EnvironmentModuleNode[]) {
2183-
const visited = new Set<EnvironmentModuleNode>()
2184-
function recurse(mod: EnvironmentModuleNode) {
2185-
if (visited.has(mod)) return
2186-
visited.add(mod)
2187-
for (const importer of mod.importers) {
2188-
recurse(importer)
2189-
}
2190-
}
2191-
for (const mod of mods) {
2192-
recurse(mod)
2193-
}
2194-
return [...visited]
2195-
}
2196-
21972163
function generateResourcesCode(depsCode: string, manager: RscPluginManager) {
21982164
const ResourcesFn = (
21992165
React: typeof import('react'),

0 commit comments

Comments
 (0)