Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dynamic routes plugin overhaul #4525

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions src/node/plugins/dynamicRoutesPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/node/plugins/staticDataPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(/\?.*$/, ''))

Expand Down Expand Up @@ -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) {
Expand Down
Loading