pull/4525/head
Divyansh Singh 7 months ago
parent 01b8db688d
commit f3eab24e05

@ -41,15 +41,48 @@ export function clearCache(id?: string) {
cache.find((_, key) => key.endsWith(id!) && cache.delete(key)) cache.find((_, key) => key.endsWith(id!) && cache.delete(key))
} }
let __pages: string[] = []
let __dynamicRoutes = new Map<string, string>()
let __rewrites = new Map<string, string>()
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( export async function createMarkdownToVueRenderFn(
srcDir: string, srcDir: string,
options: MarkdownOptions = {}, options: MarkdownOptions = {},
pages: string[],
isBuild = false, isBuild = false,
base = '/', base = '/',
includeLastUpdatedData = false, includeLastUpdatedData = false,
cleanUrls = false, cleanUrls = false,
siteConfig: SiteConfig | null = null siteConfig: SiteConfig
) { ) {
const md = await createMarkdownRenderer( const md = await createMarkdownRenderer(
srcDir, srcDir,
@ -58,27 +91,13 @@ export async function createMarkdownToVueRenderFn(
siteConfig?.logger 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 ( return async (
src: string, src: string,
file: string, file: string,
publicDir: string publicDir: string
): Promise<MarkdownCompileResult> => { ): Promise<MarkdownCompileResult> => {
const { pages, dynamicRoutes, rewrites } = getResolutionCache(siteConfig)
const fileOrig = dynamicRoutes.get(file) || file const fileOrig = dynamicRoutes.get(file) || file
file = rewrites.get(file) || file file = rewrites.get(file) || file
const relativePath = slash(path.relative(srcDir, file)) const relativePath = slash(path.relative(srcDir, file))
@ -318,10 +337,7 @@ const inferDescription = (frontmatter: Record<string, any>) => {
return (head && getHeadMetaContent(head, 'description')) || '' return (head && getHeadMetaContent(head, 'description')) || ''
} }
const getHeadMetaContent = ( const getHeadMetaContent = (head: HeadConfig[], name: string) => {
head: HeadConfig[],
name: string
): string | undefined => {
if (!head || !head.length) { if (!head || !head.length) {
return undefined return undefined
} }

@ -129,7 +129,6 @@ export async function createVitePressPlugin(
markdownToVue = await createMarkdownToVueRenderFn( markdownToVue = await createMarkdownToVueRenderFn(
srcDir, srcDir,
markdown, markdown,
siteConfig.pages,
config.command === 'build', config.command === 'build',
config.base, config.base,
lastUpdated, lastUpdated,

@ -57,7 +57,8 @@ export async function resolvePages(
return { return {
pages, pages,
dynamicRoutes, dynamicRoutes,
rewrites rewrites,
__dirty: true
} }
} }
@ -96,6 +97,8 @@ export type ResolvedRouteConfig = UserRouteConfig & {
fullPath: string fullPath: string
} }
const fileToModulesMap: Record<string, Set<string>> = {}
export const dynamicRoutesPlugin = async ( export const dynamicRoutesPlugin = async (
config: SiteConfig config: SiteConfig
): Promise<Plugin> => { ): Promise<Plugin> => {
@ -120,7 +123,7 @@ export const dynamicRoutesPlugin = async (
if (matched) { if (matched) {
const { route, params, content } = matched const { route, params, content } = matched
const routeFile = normalizePath(path.resolve(config.srcDir, route)) 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') let baseContent = fs.readFileSync(routeFile, 'utf-8')
@ -160,7 +163,7 @@ export const dynamicRoutesPlugin = async (
const modules: EnvironmentModuleNode[] = [] const modules: EnvironmentModuleNode[] = []
const mods = config.dynamicRoutes.fileToModulesMap[normalizedFile] const mods = fileToModulesMap[normalizedFile]
if (mods) { if (mods) {
// path loader module or deps updated, reset loaded routes // path loader module or deps updated, reset loaded routes
if (!normalizedFile.endsWith('.md')) { if (!normalizedFile.endsWith('.md')) {
@ -186,7 +189,7 @@ export async function resolveDynamicRoutes(
srcDir: string, srcDir: string,
routes: string[], routes: string[],
logger: Logger logger: Logger
): Promise<SiteConfig['dynamicRoutes']> { ) {
const pendingResolveRoutes: Promise<ResolvedRouteConfig[]>[] = [] const pendingResolveRoutes: Promise<ResolvedRouteConfig[]>[] = []
const routeFileToModulesMap: Record<string, Set<string>> = {} const routeFileToModulesMap: Record<string, Set<string>> = {}

@ -4,6 +4,7 @@ import type { SitemapStreamOptions } from 'sitemap'
import type { Logger, UserConfig as ViteConfig } from 'vite' import type { Logger, UserConfig as ViteConfig } from 'vite'
import type { SitemapItem } from './build/generateSitemap' import type { SitemapItem } from './build/generateSitemap'
import type { MarkdownOptions } from './markdown/markdown' import type { MarkdownOptions } from './markdown/markdown'
import type { ResolvedRouteConfig } from './plugins/dynamicRoutesPlugin'
import type { import type {
Awaitable, Awaitable,
HeadConfig, HeadConfig,
@ -30,26 +31,6 @@ export interface TransformContext {
assets: string[] assets: string[]
} }
interface UserRouteConfig {
params: Record<string, string>
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 { export interface TransformPageContext {
siteConfig: SiteConfig siteConfig: SiteConfig
} }
@ -242,7 +223,6 @@ export interface SiteConfig<ThemeConfig = any>
pages: string[] pages: string[]
dynamicRoutes: { dynamicRoutes: {
routes: ResolvedRouteConfig[] routes: ResolvedRouteConfig[]
fileToModulesMap: Record<string, Set<string>>
} }
rewrites: { rewrites: {
map: Record<string, string | undefined> map: Record<string, string | undefined>

Loading…
Cancel
Save