@@ -3,6 +3,7 @@ import c from 'picocolors'
33import {
44 mergeConfig ,
55 searchForWorkspaceRoot ,
6+ type ModuleNode ,
67 type Plugin ,
78 type ResolvedConfig ,
89 type Rollup ,
@@ -121,6 +122,7 @@ export async function createVitePressPlugin(
121122 let siteData = site
122123 let allDeadLinks : MarkdownCompileResult [ 'deadLinks' ] = [ ]
123124 let config : ResolvedConfig
125+ let importerMap : Record < string , Set < string > | undefined > = { }
124126
125127 const vitePressPlugin : Plugin = {
126128 name : 'vitepress' ,
@@ -212,6 +214,7 @@ export async function createVitePressPlugin(
212214 allDeadLinks . push ( ...deadLinks )
213215 if ( includes . length ) {
214216 includes . forEach ( ( i ) => {
217+ ; ( importerMap [ slash ( i ) ] ??= new Set ( ) ) . add ( id )
215218 this . addWatchFile ( i )
216219 } )
217220 }
@@ -245,12 +248,13 @@ export async function createVitePressPlugin(
245248 configDeps . forEach ( ( file ) => server . watcher . add ( file ) )
246249 }
247250
248- const onFileAddDelete = async ( added : boolean , file : string ) => {
251+ const onFileAddDelete = async ( added : boolean , _file : string ) => {
252+ const file = slash ( _file )
249253 // restart server on theme file creation / deletion
250- if ( themeRE . test ( slash ( file ) ) ) {
254+ if ( themeRE . test ( file ) ) {
251255 siteConfig . logger . info (
252256 c . green (
253- `${ path . relative ( process . cwd ( ) , file ) } ${
257+ `${ path . relative ( process . cwd ( ) , _file ) } ${
254258 added ? 'created' : 'deleted'
255259 } , restarting server...\n`
256260 ) ,
@@ -267,6 +271,10 @@ export async function createVitePressPlugin(
267271 await resolvePages ( siteConfig . srcDir , siteConfig . userConfig )
268272 )
269273 }
274+
275+ if ( ! added && importerMap [ file ] ) {
276+ delete importerMap [ file ]
277+ }
270278 }
271279 server . watcher
272280 . on ( 'add' , onFileAddDelete . bind ( null , true ) )
@@ -403,10 +411,27 @@ export async function createVitePressPlugin(
403411 }
404412 }
405413
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+
406430 return [
407431 vitePressPlugin ,
408432 rewritesPlugin ( siteConfig ) ,
409433 vuePlugin ,
434+ hmrFix ,
410435 webFontsPlugin ( siteConfig . useWebFonts ) ,
411436 ...( userViteConfig ?. plugins || [ ] ) ,
412437 await localSearchPlugin ( siteConfig ) ,
0 commit comments