|
|
@ -26,6 +26,8 @@ interface RouteModule {
|
|
|
|
dependencies: string[]
|
|
|
|
dependencies: string[]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const routeModuleCache = new Map<string, RouteModule>()
|
|
|
|
|
|
|
|
|
|
|
|
export type ResolvedRouteConfig = UserRouteConfig & {
|
|
|
|
export type ResolvedRouteConfig = UserRouteConfig & {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* the raw route (relative to src root), e.g. foo/[bar].md
|
|
|
|
* the raw route (relative to src root), e.g. foo/[bar].md
|
|
|
@ -92,6 +94,7 @@ export const dynamicRoutesPlugin = async (
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
async handleHotUpdate(ctx) {
|
|
|
|
async handleHotUpdate(ctx) {
|
|
|
|
|
|
|
|
routeModuleCache.delete(ctx.file)
|
|
|
|
const mods = config.dynamicRoutes.fileToModulesMap[ctx.file]
|
|
|
|
const mods = config.dynamicRoutes.fileToModulesMap[ctx.file]
|
|
|
|
if (mods) {
|
|
|
|
if (mods) {
|
|
|
|
// path loader module or deps updated, reset loaded routes
|
|
|
|
// path loader module or deps updated, reset loaded routes
|
|
|
@ -118,7 +121,7 @@ export async function resolveDynamicRoutes(
|
|
|
|
|
|
|
|
|
|
|
|
for (const route of routes) {
|
|
|
|
for (const route of routes) {
|
|
|
|
// locate corresponding route paths file
|
|
|
|
// locate corresponding route paths file
|
|
|
|
const fullPath = path.resolve(srcDir, route)
|
|
|
|
const fullPath = normalizePath(path.resolve(srcDir, route))
|
|
|
|
const jsPathsFile = fullPath.replace(/\.md$/, '.paths.js')
|
|
|
|
const jsPathsFile = fullPath.replace(/\.md$/, '.paths.js')
|
|
|
|
let pathsFile = jsPathsFile
|
|
|
|
let pathsFile = jsPathsFile
|
|
|
|
if (!fs.existsSync(jsPathsFile)) {
|
|
|
|
if (!fs.existsSync(jsPathsFile)) {
|
|
|
@ -135,15 +138,17 @@ export async function resolveDynamicRoutes(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// load the paths loader module
|
|
|
|
// load the paths loader module
|
|
|
|
let mod: RouteModule
|
|
|
|
let mod = routeModuleCache.get(pathsFile)
|
|
|
|
|
|
|
|
if (!mod) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
mod = (await loadConfigFromFile({} as any, pathsFile)) as RouteModule
|
|
|
|
mod = (await loadConfigFromFile({} as any, pathsFile)) as RouteModule
|
|
|
|
|
|
|
|
routeModuleCache.set(pathsFile, mod)
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(`invalid paths file export in ${pathsFile}.`)
|
|
|
|
console.warn(`invalid paths file export in ${pathsFile}.`)
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (mod) {
|
|
|
|
|
|
|
|
// this array represents the virtual modules affected by this route
|
|
|
|
// this array represents the virtual modules affected by this route
|
|
|
|
const matchedModuleIds = (routeFileToModulesMap[
|
|
|
|
const matchedModuleIds = (routeFileToModulesMap[
|
|
|
|
normalizePath(path.resolve(srcDir, route))
|
|
|
|
normalizePath(path.resolve(srcDir, route))
|
|
|
@ -153,12 +158,11 @@ export async function resolveDynamicRoutes(
|
|
|
|
// same array
|
|
|
|
// same array
|
|
|
|
for (const dep of mod.dependencies) {
|
|
|
|
for (const dep of mod.dependencies) {
|
|
|
|
// deps are resolved relative to cwd
|
|
|
|
// deps are resolved relative to cwd
|
|
|
|
routeFileToModulesMap[normalizePath(path.resolve(dep))] =
|
|
|
|
routeFileToModulesMap[normalizePath(path.resolve(dep))] = matchedModuleIds
|
|
|
|
matchedModuleIds
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const resolveRoute = async (): Promise<ResolvedRouteConfig[]> => {
|
|
|
|
const resolveRoute = async (): Promise<ResolvedRouteConfig[]> => {
|
|
|
|
const loader = mod.config.paths
|
|
|
|
const loader = mod!.config.paths
|
|
|
|
const paths = await (typeof loader === 'function' ? loader() : loader)
|
|
|
|
const paths = await (typeof loader === 'function' ? loader() : loader)
|
|
|
|
return paths.map((userConfig) => {
|
|
|
|
return paths.map((userConfig) => {
|
|
|
|
const resolvedPath = route.replace(
|
|
|
|
const resolvedPath = route.replace(
|
|
|
@ -175,7 +179,6 @@ export async function resolveDynamicRoutes(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pendingResolveRoutes.push(resolveRoute())
|
|
|
|
pendingResolveRoutes.push(resolveRoute())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
routes: (await Promise.all(pendingResolveRoutes)).flat(),
|
|
|
|
routes: (await Promise.all(pendingResolveRoutes)).flat(),
|
|
|
|