fix(build): track include importers by module id

importerMap values were srcDir-relative paths computed with
path.posix.relative, which garbles on Windows drive-letter case
mismatches, and the delete cleanup used a relative key against the
absolute-keyed map so it never removed anything. Store module ids
instead, derive the cache key with a win32-aware relative, and prune
both the include entry and page references after invalidating.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
pull/5014/head
Divyansh Singh 2 months ago
parent 3cf3f37b17
commit c9616559f5

@ -212,7 +212,6 @@ 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,
@ -226,7 +225,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(relativePath) ;(importerMap[slash(i)] ??= new Set()).add(slash(id))
this.addWatchFile(i) this.addWatchFile(i)
}) })
} }
@ -368,10 +367,6 @@ export async function createVitePressPlugin(
await resolvePages(siteConfig) await resolvePages(siteConfig)
} }
if (type === 'delete') {
delete importerMap[relativePath]
}
if ( if (
file === configPath || file === configPath ||
configDeps.includes(file) || configDeps.includes(file) ||
@ -403,24 +398,33 @@ export async function createVitePressPlugin(
const hmrFix: Plugin = { const hmrFix: Plugin = {
name: 'vitepress:hmr-fix', name: 'vitepress:hmr-fix',
async hotUpdate({ file, modules: existingMods }) { async hotUpdate({ file, type, modules: existingMods }) {
if (this.environment.name !== 'client') return if (this.environment.name !== 'client') return
const modules: EnvironmentModuleNode[] = [] const modules: EnvironmentModuleNode[] = []
const fileId = slash(file)
if (file.endsWith('.md')) { if (file.endsWith('.md')) {
const mod = this.environment.moduleGraph.getModuleById(file) const mod = this.environment.moduleGraph.getModuleById(file)
mod && modules.push(mod) mod && modules.push(mod)
} }
importerMap[slash(file)]?.forEach((relativePath) => { importerMap[fileId]?.forEach((importerId) => {
const relativePath = slash(path.relative(srcDir, importerId))
// the compile cache is keyed by the rewritten path // the compile cache is keyed by the rewritten path
clearCache(siteConfig.rewrites.map[relativePath] || relativePath) clearCache(siteConfig.rewrites.map[relativePath] || relativePath)
const mod = this.environment.moduleGraph.getModuleById( const mod = this.environment.moduleGraph.getModuleById(importerId)
path.posix.join(srcDir, relativePath)
)
mod && modules.push(mod) mod && modules.push(mod)
}) })
if (type === 'delete') {
// a deleted include: its importers were just invalidated above
delete importerMap[fileId]
// a deleted page: prune it from every importer set
for (const importers of Object.values(importerMap)) {
importers?.delete(fileId)
}
}
return modules.length ? [...existingMods, ...modules] : undefined return modules.length ? [...existingMods, ...modules] : undefined
} }
} }

Loading…
Cancel
Save