fix: hmr not working for snippet imports in dynamic routes

pull/1844/merge
Divyansh Singh 3 weeks ago
parent 7df3052512
commit 914467e17f

@ -32,14 +32,14 @@ export interface MarkdownCompileResult {
includes: string[] includes: string[]
} }
export function clearCache(id?: string) { export function clearCache(relativePath?: string) {
if (!id) { if (!relativePath) {
cache.clear() cache.clear()
return return
} }
id = JSON.stringify({ id }).slice(1) relativePath = JSON.stringify({ relativePath }).slice(1)
cache.find((_, key) => key.endsWith(id!) && cache.delete(key)) cache.find((_, key) => key.endsWith(relativePath!) && cache.delete(key))
} }
let __pages: string[] = [] let __pages: string[] = []
@ -114,12 +114,7 @@ export async function createMarkdownToVueRenderFn(
file = rewrites.get(file) || file file = rewrites.get(file) || file
const relativePath = slash(path.relative(srcDir, file)) const relativePath = slash(path.relative(srcDir, file))
const cacheKey = JSON.stringify({ const cacheKey = JSON.stringify({ src, ts, relativePath })
src,
ts,
file: relativePath,
id: fileOrig
})
if (isBuild || options.cache !== false) { if (isBuild || options.cache !== false) {
const cached = cache.get(cacheKey) const cached = cache.get(cacheKey)
if (cached) { if (cached) {

@ -201,6 +201,7 @@ export async function createVitePressPlugin(
return processClientJS(code, id) return processClientJS(code, id)
} }
if (id.endsWith('.md')) { if (id.endsWith('.md')) {
const relativePath = path.posix.relative(srcDir, id)
// transform .md files into vueSrc so plugin-vue can handle it // transform .md files into vueSrc so plugin-vue can handle it
const { vueSrc, deadLinks, includes, pageData } = await markdownToVue( const { vueSrc, deadLinks, includes, pageData } = await markdownToVue(
code, code,
@ -210,7 +211,7 @@ export async function createVitePressPlugin(
allDeadLinks.push(...deadLinks) allDeadLinks.push(...deadLinks)
if (includes.length) { if (includes.length) {
includes.forEach((i) => { includes.forEach((i) => {
;(importerMap[slash(i)] ??= new Set()).add(id) ;(importerMap[slash(i)] ??= new Set()).add(relativePath)
this.addWatchFile(i) this.addWatchFile(i)
}) })
} }
@ -218,7 +219,6 @@ export async function createVitePressPlugin(
this.environment.mode === 'dev' && this.environment.mode === 'dev' &&
this.environment.name === 'client' this.environment.name === 'client'
) { ) {
const relativePath = path.posix.relative(srcDir, id)
const payload: PageDataPayload = { const payload: PageDataPayload = {
path: `/${siteConfig.rewrites.map[relativePath] || relativePath}`, path: `/${siteConfig.rewrites.map[relativePath] || relativePath}`,
pageData pageData
@ -260,13 +260,13 @@ export async function createVitePressPlugin(
configDeps.forEach((file) => server.watcher.add(file)) 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) const relativePath = path.posix.relative(srcDir, file)
// restart server on theme file creation / deletion // restart server on theme file creation / deletion
if (themeRE.test(file)) { if (themeRE.test(relativePath)) {
siteConfig.logger.info( siteConfig.logger.info(
c.green( c.green(
`${path.relative(process.cwd(), _file)} ${added ? 'created' : 'deleted'}, restarting server...\n` `${relativePath} ${added ? 'created' : 'deleted'}, restarting server...\n`
), ),
{ clear: true, timestamp: true } { clear: true, timestamp: true }
) )
@ -275,10 +275,10 @@ export async function createVitePressPlugin(
} }
// update pages, dynamicRoutes and rewrites on md file creation / deletion // update pages, dynamicRoutes and rewrites on md file creation / deletion
if (file.endsWith('.md')) await resolvePages(siteConfig) if (relativePath.endsWith('.md')) await resolvePages(siteConfig)
if (!added && importerMap[file]) { if (!added && importerMap[relativePath]) {
delete importerMap[file] delete importerMap[relativePath]
} }
} }
server.watcher server.watcher
@ -410,9 +410,11 @@ export async function createVitePressPlugin(
mod && modules.push(mod) mod && modules.push(mod)
} }
importerMap[slash(file)]?.forEach((importer) => { importerMap[slash(file)]?.forEach((relativePath) => {
clearCache(importer) clearCache(relativePath)
const mod = this.environment.moduleGraph.getModuleById(importer) const mod = this.environment.moduleGraph.getModuleById(
path.posix.join(srcDir, relativePath)
)
mod && modules.push(mod) mod && modules.push(mod)
}) })

Loading…
Cancel
Save