@@ -5,6 +5,7 @@ import pm from 'picomatch'
5
5
import {
6
6
loadConfigFromFile ,
7
7
normalizePath ,
8
+ type EnvironmentModuleGraph ,
8
9
type EnvironmentModuleNode ,
9
10
type Logger ,
10
11
type Plugin
@@ -130,6 +131,7 @@ export const dynamicRoutesPlugin = async (
130
131
) : Promise < Plugin > => {
131
132
return {
132
133
name : 'vitepress:dynamic-routes' ,
134
+ enforce : 'pre' ,
133
135
134
136
resolveId ( id ) {
135
137
if ( ! id . endsWith ( '.md' ) ) return
@@ -177,11 +179,7 @@ export const dynamicRoutesPlugin = async (
177
179
const normalizedFile = normalizePath ( file )
178
180
179
181
// 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 ) )
185
183
186
184
// Also check if the file matches any custom watch patterns.
187
185
let watchedFileChanged = false
@@ -192,11 +190,7 @@ export const dynamicRoutesPlugin = async (
192
190
) {
193
191
route . routes = undefined
194
192
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 ) )
200
194
}
201
195
}
202
196
@@ -355,3 +349,16 @@ async function resolveDynamicRoutes(
355
349
356
350
return resolvedRoutes
357
351
}
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