@@ -3,6 +3,7 @@ import c from 'picocolors'
3
3
import {
4
4
mergeConfig ,
5
5
searchForWorkspaceRoot ,
6
+ type ModuleNode ,
6
7
type Plugin ,
7
8
type ResolvedConfig ,
8
9
type Rollup ,
@@ -121,6 +122,7 @@ export async function createVitePressPlugin(
121
122
let siteData = site
122
123
let allDeadLinks : MarkdownCompileResult [ 'deadLinks' ] = [ ]
123
124
let config : ResolvedConfig
125
+ let importerMap : Record < string , Set < string > | undefined > = { }
124
126
125
127
const vitePressPlugin : Plugin = {
126
128
name : 'vitepress' ,
@@ -212,6 +214,7 @@ export async function createVitePressPlugin(
212
214
allDeadLinks . push ( ...deadLinks )
213
215
if ( includes . length ) {
214
216
includes . forEach ( ( i ) => {
217
+ ; ( importerMap [ slash ( i ) ] ??= new Set ( ) ) . add ( id )
215
218
this . addWatchFile ( i )
216
219
} )
217
220
}
@@ -245,12 +248,13 @@ export async function createVitePressPlugin(
245
248
configDeps . forEach ( ( file ) => server . watcher . add ( file ) )
246
249
}
247
250
248
- const onFileAddDelete = async ( added : boolean , file : string ) => {
251
+ const onFileAddDelete = async ( added : boolean , _file : string ) => {
252
+ const file = slash ( _file )
249
253
// restart server on theme file creation / deletion
250
- if ( themeRE . test ( slash ( file ) ) ) {
254
+ if ( themeRE . test ( file ) ) {
251
255
siteConfig . logger . info (
252
256
c . green (
253
- `${ path . relative ( process . cwd ( ) , file ) } ${
257
+ `${ path . relative ( process . cwd ( ) , _file ) } ${
254
258
added ? 'created' : 'deleted'
255
259
} , restarting server...\n`
256
260
) ,
@@ -267,6 +271,10 @@ export async function createVitePressPlugin(
267
271
await resolvePages ( siteConfig . srcDir , siteConfig . userConfig )
268
272
)
269
273
}
274
+
275
+ if ( ! added && importerMap [ file ] ) {
276
+ delete importerMap [ file ]
277
+ }
270
278
}
271
279
server . watcher
272
280
. on ( 'add' , onFileAddDelete . bind ( null , true ) )
@@ -403,10 +411,27 @@ export async function createVitePressPlugin(
403
411
}
404
412
}
405
413
414
+ const hmrFix : Plugin = {
415
+ name : 'vitepress:hmr-fix' ,
416
+ async handleHotUpdate ( { file, server, modules } ) {
417
+ const importers = [ ...( importerMap [ slash ( file ) ] || [ ] ) ]
418
+ if ( importers . length > 0 ) {
419
+ return [
420
+ ...modules ,
421
+ ...importers . map ( ( id ) => {
422
+ clearCache ( id )
423
+ return server . moduleGraph . getModuleById ( id )
424
+ } )
425
+ ] . filter ( Boolean ) as ModuleNode [ ]
426
+ }
427
+ }
428
+ }
429
+
406
430
return [
407
431
vitePressPlugin ,
408
432
rewritesPlugin ( siteConfig ) ,
409
433
vuePlugin ,
434
+ hmrFix ,
410
435
webFontsPlugin ( siteConfig . useWebFonts ) ,
411
436
...( userViteConfig ?. plugins || [ ] ) ,
412
437
await localSearchPlugin ( siteConfig ) ,
0 commit comments