@@ -3,11 +3,11 @@ import path from 'node:path'
3
3
import c from 'picocolors'
4
4
import { glob } from 'tinyglobby'
5
5
import {
6
+ EnvironmentModuleNode ,
6
7
loadConfigFromFile ,
7
8
normalizePath ,
8
9
type Logger ,
9
- type Plugin ,
10
- type ViteDevServer
10
+ type Plugin
11
11
} from 'vite'
12
12
import { type SiteConfig , type UserConfig } from '../siteConfig'
13
13
import { resolveRewrites } from './rewritesPlugin'
@@ -96,15 +96,9 @@ export type ResolvedRouteConfig = UserRouteConfig & {
96
96
export const dynamicRoutesPlugin = async (
97
97
config : SiteConfig
98
98
) : Promise < Plugin > => {
99
- let server : ViteDevServer
100
-
101
99
return {
102
100
name : 'vitepress:dynamic-routes' ,
103
101
104
- configureServer ( _server ) {
105
- server = _server
106
- } ,
107
-
108
102
resolveId ( id ) {
109
103
if ( ! id . endsWith ( '.md' ) ) return
110
104
const normalizedId = id . startsWith ( config . srcDir )
@@ -139,28 +133,32 @@ export const dynamicRoutesPlugin = async (
139
133
}
140
134
141
135
// params are injected with special markers and extracted as part of
142
- // __pageData in ../markdownTovue .ts
136
+ // __pageData in ../markdownToVue .ts
143
137
return `__VP_PARAMS_START${ JSON . stringify (
144
138
params
145
139
) } __VP_PARAMS_END__${ baseContent } `
146
140
}
147
141
} ,
148
142
149
- async handleHotUpdate ( ctx ) {
150
- routeModuleCache . delete ( ctx . file )
151
- const mods = config . dynamicRoutes . fileToModulesMap [ ctx . file ]
143
+ async hotUpdate ( ctx ) {
144
+ const file = ctx . file
145
+ const modules : EnvironmentModuleNode [ ] = [ ]
146
+
147
+ const mods = config . dynamicRoutes . fileToModulesMap [ file ]
152
148
if ( mods ) {
153
149
// path loader module or deps updated, reset loaded routes
154
- if ( ! ctx . file . endsWith ( '.md' ) ) {
150
+ if ( ! file . endsWith ( '.md' ) ) {
155
151
Object . assign (
156
152
config ,
157
153
await resolvePages ( config . srcDir , config . userConfig , config . logger )
158
154
)
159
155
}
160
156
for ( const id of mods ) {
161
- ctx . modules . push ( server . moduleGraph . getModuleById ( id ) ! )
157
+ modules . push ( this . environment . moduleGraph . getModuleById ( id ) ! )
162
158
}
163
159
}
160
+
161
+ return modules . length > 0 ? [ ...ctx . modules , ...modules ] : undefined
164
162
}
165
163
}
166
164
}
0 commit comments