@@ -5,6 +5,7 @@ import pm from 'picomatch'
55import {
66 loadConfigFromFile ,
77 normalizePath ,
8+ type EnvironmentModuleGraph ,
89 type EnvironmentModuleNode ,
910 type Logger ,
1011 type Plugin
@@ -130,6 +131,7 @@ export const dynamicRoutesPlugin = async (
130131) : Promise < Plugin > => {
131132 return {
132133 name : 'vitepress:dynamic-routes' ,
134+ enforce : 'pre' ,
133135
134136 resolveId ( id ) {
135137 if ( ! id . endsWith ( '.md' ) ) return
@@ -177,11 +179,7 @@ export const dynamicRoutesPlugin = async (
177179 const normalizedFile = normalizePath ( file )
178180
179181 // Trigger update if a module or its dependencies changed.
180- for ( const id of moduleGraph . delete ( normalizedFile ) ) {
181- routeModuleCache . delete ( id )
182- const mod = this . environment . moduleGraph . getModuleById ( id )
183- if ( mod ) modules . push ( mod )
184- }
182+ modules . push ( ...getModules ( normalizedFile , this . environment . moduleGraph ) )
185183
186184 // Also check if the file matches any custom watch patterns.
187185 let watchedFileChanged = false
@@ -192,11 +190,7 @@ export const dynamicRoutesPlugin = async (
192190 ) {
193191 route . routes = undefined
194192 watchedFileChanged = true
195-
196- for ( const id of moduleGraph . delete ( file ) ) {
197- const mod = this . environment . moduleGraph . getModuleById ( id )
198- if ( mod ) modules . push ( mod )
199- }
193+ modules . push ( ...getModules ( file , this . environment . moduleGraph , false ) )
200194 }
201195 }
202196
@@ -355,3 +349,16 @@ async function resolveDynamicRoutes(
355349
356350 return resolvedRoutes
357351}
352+
353+ function getModules (
354+ id : string ,
355+ envModuleGraph : EnvironmentModuleGraph ,
356+ deleteFromRouteModuleCache = true
357+ ) {
358+ const modules : EnvironmentModuleNode [ ] = [ ]
359+ for ( const file of moduleGraph . delete ( id ) ) {
360+ deleteFromRouteModuleCache && routeModuleCache . delete ( file )
361+ modules . push ( ...( envModuleGraph . getModulesByFile ( file ) ?. values ( ) ?? [ ] ) )
362+ }
363+ return modules
364+ }
0 commit comments