fix: hmr of style blocks in dynamic routes (#4903)

pull/4905/head
Divyansh Singh 1 month ago committed by GitHub
parent 5bf835b507
commit 3d0fafba54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -5,6 +5,7 @@ import pm from 'picomatch'
import {
loadConfigFromFile,
normalizePath,
type EnvironmentModuleGraph,
type EnvironmentModuleNode,
type Logger,
type Plugin
@ -130,6 +131,7 @@ export const dynamicRoutesPlugin = async (
): Promise<Plugin> => {
return {
name: 'vitepress:dynamic-routes',
enforce: 'pre',
resolveId(id) {
if (!id.endsWith('.md')) return
@ -177,11 +179,7 @@ export const dynamicRoutesPlugin = async (
const normalizedFile = normalizePath(file)
// Trigger update if a module or its dependencies changed.
for (const id of moduleGraph.delete(normalizedFile)) {
routeModuleCache.delete(id)
const mod = this.environment.moduleGraph.getModuleById(id)
if (mod) modules.push(mod)
}
modules.push(...getModules(normalizedFile, this.environment.moduleGraph))
// Also check if the file matches any custom watch patterns.
let watchedFileChanged = false
@ -192,11 +190,7 @@ export const dynamicRoutesPlugin = async (
) {
route.routes = undefined
watchedFileChanged = true
for (const id of moduleGraph.delete(file)) {
const mod = this.environment.moduleGraph.getModuleById(id)
if (mod) modules.push(mod)
}
modules.push(...getModules(file, this.environment.moduleGraph, false))
}
}
@ -355,3 +349,16 @@ async function resolveDynamicRoutes(
return resolvedRoutes
}
function getModules(
id: string,
envModuleGraph: EnvironmentModuleGraph,
deleteFromRouteModuleCache = true
) {
const modules: EnvironmentModuleNode[] = []
for (const file of moduleGraph.delete(id)) {
deleteFromRouteModuleCache && routeModuleCache.delete(file)
modules.push(...(envModuleGraph.getModulesByFile(file)?.values() ?? []))
}
return modules
}

Loading…
Cancel
Save