From 8d8a5ac281f090cd097bece792d9dd3ef00e5545 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Thu, 14 Aug 2025 10:38:08 +0530 Subject: [PATCH] fix: hmr working only once for markdown files closes #4909 --- src/node/plugin.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/node/plugin.ts b/src/node/plugin.ts index 680be01a..2bf46478 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -4,6 +4,7 @@ import { mergeConfig, normalizePath, searchForWorkspaceRoot, + type EnvironmentModuleNode, type Plugin, type ResolvedConfig, type Rollup, @@ -400,19 +401,22 @@ export async function createVitePressPlugin( const hmrFix: Plugin = { name: 'vitepress:hmr-fix', - async hotUpdate({ file, modules }) { + async hotUpdate({ file, modules: existingMods }) { if (this.environment.name !== 'client') return + const modules: EnvironmentModuleNode[] = [] - const importers = [...(importerMap[slash(file)] || [])] - if (importers.length > 0) { - return [ - ...modules, - ...importers.map((id) => { - clearCache(id) - return this.environment.moduleGraph.getModuleById(id) - }) - ].filter((mod) => mod !== undefined) + if (file.endsWith('.md')) { + const mod = this.environment.moduleGraph.getModuleById(file) + mod && modules.push(mod) } + + importerMap[slash(file)]?.forEach((importer) => { + clearCache(importer) + const mod = this.environment.moduleGraph.getModuleById(importer) + mod && modules.push(mod) + }) + + return modules.length ? [...existingMods, ...modules] : undefined } }