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 c from 'picocolors'
import { glob } from 'tinyglobby' import { glob } from 'tinyglobby'
import { import {
EnvironmentModuleNode,
loadConfigFromFile, loadConfigFromFile,
normalizePath, normalizePath,
type Logger, type Logger,
type Plugin, type Plugin
type ViteDevServer
} from 'vite' } from 'vite'
import { type SiteConfig, type UserConfig } from '../siteConfig' import { type SiteConfig, type UserConfig } from '../siteConfig'
import { resolveRewrites } from './rewritesPlugin' import { resolveRewrites } from './rewritesPlugin'
@ -96,15 +96,9 @@ export type ResolvedRouteConfig = UserRouteConfig & {
export const dynamicRoutesPlugin = async ( export const dynamicRoutesPlugin = async (
config: SiteConfig config: SiteConfig
): Promise<Plugin> => { ): Promise<Plugin> => {
let server: ViteDevServer
return { return {
name: 'vitepress:dynamic-routes', name: 'vitepress:dynamic-routes',
configureServer(_server) {
server = _server
},
resolveId(id) { resolveId(id) {
if (!id.endsWith('.md')) return if (!id.endsWith('.md')) return
const normalizedId = id.startsWith(config.srcDir) 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 // 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( return `__VP_PARAMS_START${JSON.stringify(
params params
)}__VP_PARAMS_END__${baseContent}` )}__VP_PARAMS_END__${baseContent}`
} }
}, },
async handleHotUpdate(ctx) { async hotUpdate(ctx) {
routeModuleCache.delete(ctx.file) const file = ctx.file
const mods = config.dynamicRoutes.fileToModulesMap[ctx.file] const modules: EnvironmentModuleNode[] = []
const mods = config.dynamicRoutes.fileToModulesMap[file]
if (mods) { if (mods) {
// path loader module or deps updated, reset loaded routes // path loader module or deps updated, reset loaded routes
if (!ctx.file.endsWith('.md')) { if (!file.endsWith('.md')) {
Object.assign( Object.assign(
config, config,
await resolvePages(config.srcDir, config.userConfig, config.logger) await resolvePages(config.srcDir, config.userConfig, config.logger)
) )
} }
for (const id of mods) { 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) { if (existing) {
;({ watch, load } = existing) ;({ watch, load } = existing)
} else { } 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 // TS & native ESM support
const res = await loadConfigFromFile({} as any, id.replace(/\?.*$/, '')) const res = await loadConfigFromFile({} as any, id.replace(/\?.*$/, ''))
@ -125,8 +125,8 @@ export const staticDataPlugin: Plugin = {
hotUpdate(ctx) { hotUpdate(ctx) {
const file = ctx.file const file = ctx.file
const modules: EnvironmentModuleNode[] = [] const modules: EnvironmentModuleNode[] = []
// dependency of data loader changed // dependency of data loader changed
// (note the dep array includes the loader file itself) // (note the dep array includes the loader file itself)
if (file in depToLoaderModuleIdMap) { if (file in depToLoaderModuleIdMap) {

Loading…
Cancel
Save