Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/node/plugins/dynamicRoutesPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import pm from 'picomatch'
import {
loadConfigFromFile,
normalizePath,
type EnvironmentModuleGraph,
type EnvironmentModuleNode,
type Logger,
type Plugin
Expand Down Expand Up @@ -130,6 +131,7 @@ export const dynamicRoutesPlugin = async (
): Promise<Plugin> => {
return {
name: 'vitepress:dynamic-routes',
enforce: 'pre',

resolveId(id) {
if (!id.endsWith('.md')) return
Expand Down Expand Up @@ -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
Expand All @@ -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))
}
}

Expand Down Expand Up @@ -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