Skip to content

Commit

Permalink
fix(hmr): handle hmr in imported code snippets (#3005)
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Sep 23, 2023
1 parent 7fbfe71 commit e84f313
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/node/markdownToVue.ts
Expand Up @@ -29,8 +29,14 @@ export interface MarkdownCompileResult {
includes: string[]
}

export function clearCache() {
cache.clear()
export function clearCache(file?: string) {
if (!file) {
cache.clear()
return
}

file = JSON.stringify({ file }).slice(1)
cache.find((_, key) => key.endsWith(file!) && cache.delete(key))
}

export async function createMarkdownToVueRenderFn(
Expand Down
31 changes: 28 additions & 3 deletions src/node/plugin.ts
Expand Up @@ -3,6 +3,7 @@ import c from 'picocolors'
import {
mergeConfig,
searchForWorkspaceRoot,
type ModuleNode,
type Plugin,
type ResolvedConfig,
type Rollup,
Expand Down Expand Up @@ -121,6 +122,7 @@ export async function createVitePressPlugin(
let siteData = site
let allDeadLinks: MarkdownCompileResult['deadLinks'] = []
let config: ResolvedConfig
let importerMap: Record<string, Set<string> | undefined> = {}

const vitePressPlugin: Plugin = {
name: 'vitepress',
Expand Down Expand Up @@ -212,6 +214,7 @@ export async function createVitePressPlugin(
allDeadLinks.push(...deadLinks)
if (includes.length) {
includes.forEach((i) => {
;(importerMap[slash(i)] ??= new Set()).add(id)
this.addWatchFile(i)
})
}
Expand Down Expand Up @@ -245,12 +248,13 @@ export async function createVitePressPlugin(
configDeps.forEach((file) => server.watcher.add(file))
}

const onFileAddDelete = async (added: boolean, file: string) => {
const onFileAddDelete = async (added: boolean, _file: string) => {
const file = slash(_file)
// restart server on theme file creation / deletion
if (themeRE.test(slash(file))) {
if (themeRE.test(file)) {
siteConfig.logger.info(
c.green(
`${path.relative(process.cwd(), file)} ${
`${path.relative(process.cwd(), _file)} ${
added ? 'created' : 'deleted'
}, restarting server...\n`
),
Expand All @@ -267,6 +271,10 @@ export async function createVitePressPlugin(
await resolvePages(siteConfig.srcDir, siteConfig.userConfig)
)
}

if (!added && importerMap[file]) {
delete importerMap[file]
}
}
server.watcher
.on('add', onFileAddDelete.bind(null, true))
Expand Down Expand Up @@ -403,10 +411,27 @@ export async function createVitePressPlugin(
}
}

const hmrFix: Plugin = {
name: 'vitepress:hmr-fix',
async handleHotUpdate({ file, server, modules }) {
const importers = [...(importerMap[slash(file)] || [])]
if (importers.length > 0) {
return [
...modules,
...importers.map((id) => {
clearCache(id)
return server.moduleGraph.getModuleById(id)
})
].filter(Boolean) as ModuleNode[]
}
}
}

return [
vitePressPlugin,
rewritesPlugin(siteConfig),
vuePlugin,
hmrFix,
webFontsPlugin(siteConfig.useWebFonts),
...(userViteConfig?.plugins || []),
await localSearchPlugin(siteConfig),
Expand Down

0 comments on commit e84f313

Please sign in to comment.