diff --git a/src/node/markdownToVue.ts b/src/node/markdownToVue.ts index b6b00216..502bb2cd 100644 --- a/src/node/markdownToVue.ts +++ b/src/node/markdownToVue.ts @@ -41,15 +41,48 @@ export function clearCache(id?: string) { cache.find((_, key) => key.endsWith(id!) && cache.delete(key)) } +let __pages: string[] = [] +let __dynamicRoutes = new Map() +let __rewrites = new Map() + +function getResolutionCache(siteConfig: SiteConfig) { + // @ts-expect-error internal + if (siteConfig.__dirty !== false) { + __pages = siteConfig.pages.map((p) => slash(p.replace(/\.md$/, ''))) + + __dynamicRoutes = new Map( + siteConfig.dynamicRoutes.routes.map((r) => [ + r.fullPath, + slash(path.join(siteConfig.srcDir, r.route)) + ]) + ) + + __rewrites = new Map( + Object.entries(siteConfig.rewrites.map || {}).map(([key, value]) => [ + slash(path.join(siteConfig.srcDir, key)), + slash(path.join(siteConfig.srcDir, value!)) + ]) + ) + + // @ts-expect-error internal + siteConfig.__dirty = false + } + + return { + pages: __pages, + dynamicRoutes: __dynamicRoutes, + rewrites: __rewrites + } +} + export async function createMarkdownToVueRenderFn( srcDir: string, options: MarkdownOptions = {}, - pages: string[], isBuild = false, base = '/', includeLastUpdatedData = false, cleanUrls = false, - siteConfig: SiteConfig | null = null + siteConfig: SiteConfig ) { const md = await createMarkdownRenderer( srcDir, @@ -58,27 +91,13 @@ export async function createMarkdownToVueRenderFn( siteConfig?.logger ) - pages = pages.map((p) => slash(p.replace(/\.md$/, ''))) - - const dynamicRoutes = new Map( - siteConfig?.dynamicRoutes?.routes.map((r) => [ - r.fullPath, - slash(path.join(srcDir, r.route)) - ]) || [] - ) - - const rewrites = new Map( - Object.entries(siteConfig?.rewrites.map || {}).map(([key, value]) => [ - slash(path.join(srcDir, key)), - slash(path.join(srcDir, value!)) - ]) || [] - ) - return async ( src: string, file: string, publicDir: string ): Promise => { + const { pages, dynamicRoutes, rewrites } = getResolutionCache(siteConfig) + const fileOrig = dynamicRoutes.get(file) || file file = rewrites.get(file) || file const relativePath = slash(path.relative(srcDir, file)) @@ -318,10 +337,7 @@ const inferDescription = (frontmatter: Record) => { return (head && getHeadMetaContent(head, 'description')) || '' } -const getHeadMetaContent = ( - head: HeadConfig[], - name: string -): string | undefined => { +const getHeadMetaContent = (head: HeadConfig[], name: string) => { if (!head || !head.length) { return undefined } diff --git a/src/node/plugin.ts b/src/node/plugin.ts index 481edca3..d2047ca2 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -129,7 +129,6 @@ export async function createVitePressPlugin( markdownToVue = await createMarkdownToVueRenderFn( srcDir, markdown, - siteConfig.pages, config.command === 'build', config.base, lastUpdated, diff --git a/src/node/plugins/dynamicRoutesPlugin.ts b/src/node/plugins/dynamicRoutesPlugin.ts index d94f9556..8d82eddd 100644 --- a/src/node/plugins/dynamicRoutesPlugin.ts +++ b/src/node/plugins/dynamicRoutesPlugin.ts @@ -57,7 +57,8 @@ export async function resolvePages( return { pages, dynamicRoutes, - rewrites + rewrites, + __dirty: true } } @@ -96,6 +97,8 @@ export type ResolvedRouteConfig = UserRouteConfig & { fullPath: string } +const fileToModulesMap: Record> = {} + export const dynamicRoutesPlugin = async ( config: SiteConfig ): Promise => { @@ -120,7 +123,7 @@ export const dynamicRoutesPlugin = async ( if (matched) { const { route, params, content } = matched const routeFile = normalizePath(path.resolve(config.srcDir, route)) - config.dynamicRoutes.fileToModulesMap[routeFile].add(id) + fileToModulesMap[routeFile].add(id) let baseContent = fs.readFileSync(routeFile, 'utf-8') @@ -160,7 +163,7 @@ export const dynamicRoutesPlugin = async ( const modules: EnvironmentModuleNode[] = [] - const mods = config.dynamicRoutes.fileToModulesMap[normalizedFile] + const mods = fileToModulesMap[normalizedFile] if (mods) { // path loader module or deps updated, reset loaded routes if (!normalizedFile.endsWith('.md')) { @@ -186,7 +189,7 @@ export async function resolveDynamicRoutes( srcDir: string, routes: string[], logger: Logger -): Promise { +) { const pendingResolveRoutes: Promise[] = [] const routeFileToModulesMap: Record> = {} diff --git a/src/node/siteConfig.ts b/src/node/siteConfig.ts index ebd99096..47414622 100644 --- a/src/node/siteConfig.ts +++ b/src/node/siteConfig.ts @@ -4,6 +4,7 @@ import type { SitemapStreamOptions } from 'sitemap' import type { Logger, UserConfig as ViteConfig } from 'vite' import type { SitemapItem } from './build/generateSitemap' import type { MarkdownOptions } from './markdown/markdown' +import type { ResolvedRouteConfig } from './plugins/dynamicRoutesPlugin' import type { Awaitable, HeadConfig, @@ -30,26 +31,6 @@ export interface TransformContext { assets: string[] } -interface UserRouteConfig { - params: Record - content?: string -} - -export type ResolvedRouteConfig = UserRouteConfig & { - /** - * the raw route (relative to src root), e.g. foo/[bar].md - */ - route: string - /** - * the actual path with params resolved (relative to src root), e.g. foo/1.md - */ - path: string - /** - * absolute fs path - */ - fullPath: string -} - export interface TransformPageContext { siteConfig: SiteConfig } @@ -242,7 +223,6 @@ export interface SiteConfig pages: string[] dynamicRoutes: { routes: ResolvedRouteConfig[] - fileToModulesMap: Record> } rewrites: { map: Record