pull/4525/head
Divyansh Singh 8 months ago
parent d1f2afdf0f
commit de2ce4e8fb

@ -3,11 +3,11 @@ import path from 'node:path'
import c from 'picocolors'
import { glob } from 'tinyglobby'
import {
EnvironmentModuleNode,
loadConfigFromFile,
normalizePath,
type Logger,
type Plugin,
type ViteDevServer
type Plugin
} from 'vite'
import { type SiteConfig, type UserConfig } from '../siteConfig'
import { resolveRewrites } from './rewritesPlugin'
@ -96,15 +96,9 @@ export type ResolvedRouteConfig = UserRouteConfig & {
export const dynamicRoutesPlugin = async (
config: SiteConfig
): Promise<Plugin> => {
let server: ViteDevServer
return {
name: 'vitepress:dynamic-routes',
configureServer(_server) {
server = _server
},
resolveId(id) {
if (!id.endsWith('.md')) return
const normalizedId = id.startsWith(config.srcDir)
@ -139,28 +133,32 @@ export const dynamicRoutesPlugin = async (
}
// params are injected with special markers and extracted as part of
// __pageData in ../markdownTovue.ts
// __pageData in ../markdownToVue.ts
return `__VP_PARAMS_START${JSON.stringify(
params
)}__VP_PARAMS_END__${baseContent}`
}
},
async handleHotUpdate(ctx) {
routeModuleCache.delete(ctx.file)
const mods = config.dynamicRoutes.fileToModulesMap[ctx.file]
async hotUpdate(ctx) {
const file = ctx.file
const modules: EnvironmentModuleNode[] = []
const mods = config.dynamicRoutes.fileToModulesMap[file]
if (mods) {
// path loader module or deps updated, reset loaded routes
if (!ctx.file.endsWith('.md')) {
if (!file.endsWith('.md')) {
Object.assign(
config,
await resolvePages(config.srcDir, config.userConfig, config.logger)
)
}
for (const id of mods) {
ctx.modules.push(server.moduleGraph.getModuleById(id)!)
modules.push(this.environment.moduleGraph.getModuleById(id)!)
}
}
return modules.length > 0 ? [...ctx.modules, ...modules] : undefined
}
}
}

@ -70,7 +70,7 @@ export const staticDataPlugin: Plugin = {
if (existing) {
;({ watch, load } = existing)
} else {
// use vite's load config util as a away to load Node.js file with
// use vite's load config util as a way to load Node.js file with
// TS & native ESM support
const res = await loadConfigFromFile({} as any, id.replace(/\?.*$/, ''))
@ -125,8 +125,8 @@ export const staticDataPlugin: Plugin = {
hotUpdate(ctx) {
const file = ctx.file
const modules: EnvironmentModuleNode[] = []
// dependency of data loader changed
// (note the dep array includes the loader file itself)
if (file in depToLoaderModuleIdMap) {

Loading…
Cancel
Save