pull/4525/head
Divyansh Singh 7 months ago
parent d3ec222553
commit 26f276fee3

@ -1,3 +1,4 @@
import type { PageData } from 'client/shared'
import paths from './paths' import paths from './paths'
export default { export default {
@ -5,5 +6,9 @@ export default {
console.log('watchedFiles', watchedFiles) console.log('watchedFiles', watchedFiles)
return paths return paths
}, },
watch: ['**/data-loading/**/*.json'] watch: ['**/data-loading/**/*.json'],
async transformPageData(pageData: PageData) {
console.log('transformPageData', pageData.filePath)
pageData.title += ' - transformed'
}
} }

@ -9,6 +9,7 @@ import {
type MarkdownOptions, type MarkdownOptions,
type MarkdownRenderer type MarkdownRenderer
} from './markdown/markdown' } from './markdown/markdown'
import { getPageDataTransformer } from './plugins/dynamicRoutesPlugin'
import { import {
EXTERNAL_URL_RE, EXTERNAL_URL_RE,
getLocaleForPath, getLocaleForPath,
@ -42,8 +43,9 @@ export function clearCache(id?: string) {
} }
let __pages: string[] = [] let __pages: string[] = []
let __dynamicRoutes = new Map<string, string>() let __dynamicRoutes = new Map<string, [string, string]>()
let __rewrites = new Map<string, string>() let __rewrites = new Map<string, string>()
let __ts: number
function getResolutionCache(siteConfig: SiteConfig) { function getResolutionCache(siteConfig: SiteConfig) {
// @ts-expect-error internal // @ts-expect-error internal
@ -53,7 +55,7 @@ function getResolutionCache(siteConfig: SiteConfig) {
__dynamicRoutes = new Map( __dynamicRoutes = new Map(
siteConfig.dynamicRoutes.map((r) => [ siteConfig.dynamicRoutes.map((r) => [
r.fullPath, r.fullPath,
slash(path.join(siteConfig.srcDir, r.route)) [slash(path.join(siteConfig.srcDir, r.route)), r.loaderPath]
]) ])
) )
@ -64,6 +66,8 @@ function getResolutionCache(siteConfig: SiteConfig) {
]) ])
) )
__ts = Date.now()
// @ts-expect-error internal // @ts-expect-error internal
siteConfig.__dirty = false siteConfig.__dirty = false
} }
@ -71,7 +75,8 @@ function getResolutionCache(siteConfig: SiteConfig) {
return { return {
pages: __pages, pages: __pages,
dynamicRoutes: __dynamicRoutes, dynamicRoutes: __dynamicRoutes,
rewrites: __rewrites rewrites: __rewrites,
ts: __ts
} }
} }
@ -96,13 +101,25 @@ export async function createMarkdownToVueRenderFn(
file: string, file: string,
publicDir: string publicDir: string
): Promise<MarkdownCompileResult> => { ): Promise<MarkdownCompileResult> => {
const { pages, dynamicRoutes, rewrites } = getResolutionCache(siteConfig) const { pages, dynamicRoutes, rewrites, ts } =
getResolutionCache(siteConfig)
const dynamicRoute = dynamicRoutes.get(file)
const fileOrig = dynamicRoute?.[0] || file
const transformPageData = [
siteConfig?.transformPageData,
getPageDataTransformer(dynamicRoute?.[1]!)
].filter((fn) => fn != null)
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))
const cacheKey = JSON.stringify({ src, file: relativePath, id: fileOrig }) const cacheKey = JSON.stringify({
src,
ts,
file: relativePath,
id: fileOrig
})
if (isBuild || options.cache !== false) { if (isBuild || options.cache !== false) {
const cached = cache.get(cacheKey) const cached = cache.get(cacheKey)
if (cached) { if (cached) {
@ -224,10 +241,9 @@ export async function createMarkdownToVueRenderFn(
} }
} }
if (siteConfig?.transformPageData) { for (const fn of transformPageData) {
const dataToMerge = await siteConfig.transformPageData(pageData, { if (fn) {
siteConfig const dataToMerge = await fn(pageData, { siteConfig })
})
if (dataToMerge) { if (dataToMerge) {
pageData = { pageData = {
...pageData, ...pageData,
@ -235,6 +251,7 @@ export async function createMarkdownToVueRenderFn(
} }
} }
} }
}
const vueSrc = [ const vueSrc = [
...injectPageDataCode( ...injectPageDataCode(

@ -50,6 +50,7 @@ interface ResolvedRouteModule {
watch: string[] | undefined watch: string[] | undefined
routes: ResolvedRouteConfig[] | undefined routes: ResolvedRouteConfig[] | undefined
loader: RouteModule['paths'] loader: RouteModule['paths']
transformPageData?: UserConfig['transformPageData']
} }
const dynamicRouteRE = /\[(\w+?)\]/g const dynamicRouteRE = /\[(\w+?)\]/g
@ -196,6 +197,12 @@ export const dynamicRoutesPlugin = async (
} }
} }
export function getPageDataTransformer(
loaderPath: string
): UserConfig['transformPageData'] | undefined {
return routeModuleCache.get(loaderPath)?.transformPageData
}
async function resolveDynamicRoutes( async function resolveDynamicRoutes(
srcDir: string, srcDir: string,
routes: string[], routes: string[],
@ -227,6 +234,7 @@ async function resolveDynamicRoutes(
// load the paths loader module // load the paths loader module
let watch: ResolvedRouteModule['watch'] let watch: ResolvedRouteModule['watch']
let loader: ResolvedRouteModule['loader'] let loader: ResolvedRouteModule['loader']
let extras: Partial<ResolvedRouteModule>
const loaderPath = normalizePath(pathsFile) const loaderPath = normalizePath(pathsFile)
const existing = routeModuleCache.get(loaderPath) const existing = routeModuleCache.get(loaderPath)
@ -238,7 +246,7 @@ async function resolveDynamicRoutes(
continue continue
} }
;({ watch, loader } = existing) ;({ watch, loader, ...extras } = existing)
} else { } else {
let mod let mod
try { try {
@ -265,9 +273,9 @@ async function resolveDynamicRoutes(
continue continue
} }
const routeModule = mod.config as RouteModule // @ts-ignore
;({ paths: loader, watch, ...extras } = mod.config)
loader = routeModule.paths
if (!loader) { if (!loader) {
logger.warn( logger.warn(
c.yellow( c.yellow(
@ -278,10 +286,7 @@ async function resolveDynamicRoutes(
continue continue
} }
watch = watch = typeof watch === 'string' ? [watch] : watch
typeof routeModule.watch === 'string'
? [routeModule.watch]
: routeModule.watch
if (watch) { if (watch) {
watch = watch.map((p) => watch = watch.map((p) =>
p.startsWith('.') p.startsWith('.')
@ -329,7 +334,7 @@ async function resolveDynamicRoutes(
} }
}) })
routeModuleCache.set(loaderPath, { watch, routes, loader }) routeModuleCache.set(loaderPath, { ...extras, watch, routes, loader })
return routes return routes
} }

Loading…
Cancel
Save