fix: dyamic routes w/ srcDir + relative imports

pull/2005/head
Evan You 2 years ago
parent 24fa862c39
commit b075ee5be6

@ -197,13 +197,14 @@ function resolvePageImports(
page = config.rewrites.inv[page] || page page = config.rewrites.inv[page] || page
// find the page's js chunk and inject script tags for its imports so that // find the page's js chunk and inject script tags for its imports so that
// they start fetching as early as possible // they start fetching as early as possible
let srcPath = page let srcPath = path.resolve(config.srcDir, page)
try { try {
srcPath = normalizePath(fs.realpathSync(path.resolve(config.srcDir, page))) srcPath = fs.realpathSync(srcPath)
} catch (e) { } catch (e) {
// if the page is a virtual page generated by a dynamic route this would // if the page is a virtual page generated by a dynamic route this would
// fail, which is expected // fail, which is expected
} }
srcPath = normalizePath(srcPath)
const pageChunk = result.output.find( const pageChunk = result.output.find(
(chunk) => chunk.type === 'chunk' && chunk.facadeModuleId === srcPath (chunk) => chunk.type === 'chunk' && chunk.facadeModuleId === srcPath
) as OutputChunk ) as OutputChunk

@ -435,7 +435,7 @@ export async function resolvePages(srcDir: string, userConfig: UserConfig) {
const dynamicRouteFiles = allMarkdownFiles.filter((p) => const dynamicRouteFiles = allMarkdownFiles.filter((p) =>
dynamicRouteRE.test(p) dynamicRouteRE.test(p)
) )
const dynamicRoutes = await resolveDynamicRoutes(dynamicRouteFiles) const dynamicRoutes = await resolveDynamicRoutes(srcDir, dynamicRouteFiles)
pages.push(...dynamicRoutes.routes.map((r) => r.path)) pages.push(...dynamicRoutes.routes.map((r) => r.path))
const rewrites = resolveRewrites(pages, userConfig.rewrites) const rewrites = resolveRewrites(pages, userConfig.rewrites)

@ -28,13 +28,17 @@ interface RouteModule {
export type ResolvedRouteConfig = UserRouteConfig & { export type ResolvedRouteConfig = UserRouteConfig & {
/** /**
* the raw route, e.g. foo/[bar].md * the raw route (relative to src root), e.g. foo/[bar].md
*/ */
route: string route: string
/** /**
* the actual path with params resolved, e.g. foo/1.md * the actual path with params resolved (relative to src root), e.g. foo/1.md
*/ */
path: string path: string
/**
* absolute fs path
*/
fullPath: string
} }
export const dynamicRoutesPlugin = async ( export const dynamicRoutesPlugin = async (
@ -52,10 +56,10 @@ export const dynamicRoutesPlugin = async (
resolveId(id) { resolveId(id) {
if (!id.endsWith('.md')) return if (!id.endsWith('.md')) return
const normalizedId = id.startsWith(config.root) const normalizedId = id.startsWith(config.root)
? normalizePath(path.relative(config.root, id)) ? id
: id.replace(/^\//, '') : normalizePath(path.resolve(config.srcDir, id.replace(/^\//, '')))
const matched = config.dynamicRoutes.routes.find( const matched = config.dynamicRoutes.routes.find(
(r) => r.path === normalizedId (r) => r.fullPath === normalizedId
) )
if (matched) { if (matched) {
return normalizedId return normalizedId
@ -63,10 +67,10 @@ export const dynamicRoutesPlugin = async (
}, },
load(id) { load(id) {
const matched = config.dynamicRoutes.routes.find((r) => r.path === id) const matched = config.dynamicRoutes.routes.find((r) => r.fullPath === id)
if (matched) { if (matched) {
const { route, params, content } = matched const { route, params, content } = matched
const routeFile = normalizePath(path.resolve(config.root, route)) const routeFile = normalizePath(path.resolve(config.srcDir, route))
config.dynamicRoutes.fileToModulesMap[routeFile].add(id) config.dynamicRoutes.fileToModulesMap[routeFile].add(id)
let baseContent = fs.readFileSync(routeFile, 'utf-8') let baseContent = fs.readFileSync(routeFile, 'utf-8')
@ -106,6 +110,7 @@ export const dynamicRoutesPlugin = async (
} }
export async function resolveDynamicRoutes( export async function resolveDynamicRoutes(
srcDir: string,
routes: string[] routes: string[]
): Promise<SiteConfig['dynamicRoutes']> { ): Promise<SiteConfig['dynamicRoutes']> {
const pendingResolveRoutes: Promise<ResolvedRouteConfig[]>[] = [] const pendingResolveRoutes: Promise<ResolvedRouteConfig[]>[] = []
@ -113,10 +118,11 @@ 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 jsPathsFile = route.replace(/\.md$/, '.paths.js') const fullPath = path.resolve(srcDir, route)
const jsPathsFile = fullPath.replace(/\.md$/, '.paths.js')
let pathsFile = jsPathsFile let pathsFile = jsPathsFile
if (!fs.existsSync(jsPathsFile)) { if (!fs.existsSync(jsPathsFile)) {
pathsFile = route.replace(/\.md$/, '.paths.ts') pathsFile = fullPath.replace(/\.md$/, '.paths.ts')
if (!fs.existsSync(pathsFile)) { if (!fs.existsSync(pathsFile)) {
console.warn( console.warn(
c.yellow( c.yellow(
@ -131,10 +137,7 @@ export async function resolveDynamicRoutes(
// load the paths loader module // load the paths loader module
let mod: RouteModule let mod: RouteModule
try { try {
mod = (await loadConfigFromFile( mod = (await loadConfigFromFile({} as any, pathsFile)) as RouteModule
{} as any,
path.resolve(pathsFile)
)) as RouteModule
} catch (e) { } catch (e) {
console.warn(`invalid paths file export in ${pathsFile}.`) console.warn(`invalid paths file export in ${pathsFile}.`)
continue continue
@ -143,12 +146,13 @@ export async function resolveDynamicRoutes(
if (mod) { 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(route)) normalizePath(path.resolve(srcDir, route))
] = new Set()) ] = new Set())
// each dependency (including the loader module itself) also point to the // each dependency (including the loader module itself) also point to the
// same array // same array
for (const dep of mod.dependencies) { for (const dep of mod.dependencies) {
// deps are resolved relative to cwd
routeFileToModulesMap[normalizePath(path.resolve(dep))] = routeFileToModulesMap[normalizePath(path.resolve(dep))] =
matchedModuleIds matchedModuleIds
} }
@ -157,11 +161,13 @@ export async function resolveDynamicRoutes(
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) => {
return { const resolvedPath = route.replace(
path: route.replace(
dynamicRouteRE, dynamicRouteRE,
(_, key) => userConfig.params[key] (_, key) => userConfig.params[key]
), )
return {
path: resolvedPath,
fullPath: normalizePath(path.resolve(srcDir, resolvedPath)),
route, route,
...userConfig ...userConfig
} }

Loading…
Cancel
Save