pull/4525/head
Divyansh Singh 7 months ago
parent 9da44f6c8c
commit 18994427a6

@ -203,9 +203,8 @@ export async function createVitePressPlugin(
if (id.endsWith('.vue')) { if (id.endsWith('.vue')) {
return processClientJS(code, id) return processClientJS(code, id)
} else if (id.endsWith('.md')) { } else if (id.endsWith('.md')) {
console.log('transform', 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 } = await markdownToVue( const { vueSrc, deadLinks, includes, pageData } = await markdownToVue(
code, code,
id, id,
config.publicDir config.publicDir
@ -217,6 +216,22 @@ export async function createVitePressPlugin(
this.addWatchFile(i) this.addWatchFile(i)
}) })
} }
if (
this.environment.mode === 'dev' &&
this.environment.name === 'client'
) {
const relativePath = path.posix.relative(srcDir, id)
const payload: PageDataPayload = {
path: `/${siteConfig.rewrites.map[relativePath] || relativePath}`,
pageData
}
// notify the client to update page data
this.environment.hot.send({
type: 'custom',
event: 'vitepress:pageData',
data: payload
})
}
return processClientJS(vueSrc, id) return processClientJS(vueSrc, id)
} }
}, },
@ -363,10 +378,9 @@ export async function createVitePressPlugin(
} }
}, },
async hotUpdate(ctx) { async hotUpdate({ file }) {
if (this.environment.name !== 'client') return if (this.environment.name !== 'client') return
const { file, read } = ctx
if (file === configPath || configDeps.includes(file)) { if (file === configPath || configDeps.includes(file)) {
siteConfig.logger.info( siteConfig.logger.info(
c.green( c.green(
@ -387,33 +401,6 @@ export async function createVitePressPlugin(
await recreateServer?.() await recreateServer?.()
return return
} }
// hot reload .md files as .vue files
if (file.endsWith('.md')) {
const content = await read()
console.log('hotUpdate', file)
const { pageData, vueSrc } = await markdownToVue(
content,
file,
config.publicDir
)
const relativePath = slash(path.relative(srcDir, file))
const payload: PageDataPayload = {
path: `/${siteConfig.rewrites.map[relativePath] || relativePath}`,
pageData
}
// notify the client to update page data
this.environment.hot.send({
type: 'custom',
event: 'vitepress:pageData',
data: payload
})
// overwrite src so vue plugin can handle the HMR
ctx.read = () => vueSrc
}
} }
} }

@ -152,7 +152,7 @@ export const dynamicRoutesPlugin = async (
async hotUpdate({ file, modules: existingMods }) { async hotUpdate({ file, modules: existingMods }) {
if (this.environment.name !== 'client') return if (this.environment.name !== 'client') return
const modules = new Set<EnvironmentModuleNode>() const modules: EnvironmentModuleNode[] = []
const normalizedFile = normalizePath(file) const normalizedFile = normalizePath(file)
// Trigger update if a module or its dependencies changed. // Trigger update if a module or its dependencies changed.
@ -160,7 +160,7 @@ export const dynamicRoutesPlugin = async (
routeModuleCache.delete(id) routeModuleCache.delete(id)
const mod = this.environment.moduleGraph.getModuleById(id) const mod = this.environment.moduleGraph.getModuleById(id)
if (mod) { if (mod) {
modules.add(mod) modules.push(mod)
} }
} }
@ -174,7 +174,7 @@ export const dynamicRoutesPlugin = async (
} }
if ( if (
(modules.size && !normalizedFile.endsWith('.md')) || (modules.length && !normalizedFile.endsWith('.md')) ||
watchedFileChanged || watchedFileChanged ||
pathLoaderRE.test(normalizedFile) pathLoaderRE.test(normalizedFile)
) { ) {
@ -185,7 +185,7 @@ export const dynamicRoutesPlugin = async (
) )
} }
return modules.size ? [...existingMods, ...modules] : undefined return modules.length ? [...existingMods, ...modules] : undefined
} }
} }
} }

@ -156,6 +156,6 @@ export const staticDataPlugin: Plugin = {
} }
} }
return modules.length > 0 ? [...existingMods, ...modules] : undefined return modules.length ? [...existingMods, ...modules] : undefined
} }
} }

Loading…
Cancel
Save