diff --git a/src/node/plugin.ts b/src/node/plugin.ts index 806aa3cf..35905912 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -203,9 +203,8 @@ export async function createVitePressPlugin( if (id.endsWith('.vue')) { return processClientJS(code, id) } else if (id.endsWith('.md')) { - console.log('transform', id) // 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, id, config.publicDir @@ -217,6 +216,22 @@ export async function createVitePressPlugin( 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) } }, @@ -363,10 +378,9 @@ export async function createVitePressPlugin( } }, - async hotUpdate(ctx) { + async hotUpdate({ file }) { if (this.environment.name !== 'client') return - const { file, read } = ctx if (file === configPath || configDeps.includes(file)) { siteConfig.logger.info( c.green( @@ -387,33 +401,6 @@ export async function createVitePressPlugin( await recreateServer?.() 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 - } } } diff --git a/src/node/plugins/dynamicRoutesPlugin.ts b/src/node/plugins/dynamicRoutesPlugin.ts index 78787428..44340524 100644 --- a/src/node/plugins/dynamicRoutesPlugin.ts +++ b/src/node/plugins/dynamicRoutesPlugin.ts @@ -152,7 +152,7 @@ export const dynamicRoutesPlugin = async ( async hotUpdate({ file, modules: existingMods }) { if (this.environment.name !== 'client') return - const modules = new Set() + const modules: EnvironmentModuleNode[] = [] const normalizedFile = normalizePath(file) // Trigger update if a module or its dependencies changed. @@ -160,7 +160,7 @@ export const dynamicRoutesPlugin = async ( routeModuleCache.delete(id) const mod = this.environment.moduleGraph.getModuleById(id) if (mod) { - modules.add(mod) + modules.push(mod) } } @@ -174,7 +174,7 @@ export const dynamicRoutesPlugin = async ( } if ( - (modules.size && !normalizedFile.endsWith('.md')) || + (modules.length && !normalizedFile.endsWith('.md')) || watchedFileChanged || pathLoaderRE.test(normalizedFile) ) { @@ -185,7 +185,7 @@ export const dynamicRoutesPlugin = async ( ) } - return modules.size ? [...existingMods, ...modules] : undefined + return modules.length ? [...existingMods, ...modules] : undefined } } } diff --git a/src/node/plugins/staticDataPlugin.ts b/src/node/plugins/staticDataPlugin.ts index 1833c733..8521603d 100644 --- a/src/node/plugins/staticDataPlugin.ts +++ b/src/node/plugins/staticDataPlugin.ts @@ -156,6 +156,6 @@ export const staticDataPlugin: Plugin = { } } - return modules.length > 0 ? [...existingMods, ...modules] : undefined + return modules.length ? [...existingMods, ...modules] : undefined } }