fix: print errors when importing an invalid dynamic route (#3201)

Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
pull/3137/merge
arianrhodsandlot 11 months ago committed by GitHub
parent 9c20e3b5f8
commit 6d89a08cb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -104,7 +104,8 @@ export async function resolveConfig(
const { pages, dynamicRoutes, rewrites } = await resolvePages( const { pages, dynamicRoutes, rewrites } = await resolvePages(
srcDir, srcDir,
userConfig userConfig,
logger
) )
const config: SiteConfig = { const config: SiteConfig = {

@ -268,7 +268,11 @@ export async function createVitePressPlugin(
if (file.endsWith('.md')) { if (file.endsWith('.md')) {
Object.assign( Object.assign(
siteConfig, siteConfig,
await resolvePages(siteConfig.srcDir, siteConfig.userConfig) await resolvePages(
siteConfig.srcDir,
siteConfig.userConfig,
siteConfig.logger
)
) )
} }

@ -1,6 +1,7 @@
import { import {
loadConfigFromFile, loadConfigFromFile,
normalizePath, normalizePath,
type Logger,
type Plugin, type Plugin,
type ViteDevServer type ViteDevServer
} from 'vite' } from 'vite'
@ -13,7 +14,11 @@ import { resolveRewrites } from './rewritesPlugin'
export const dynamicRouteRE = /\[(\w+?)\]/g export const dynamicRouteRE = /\[(\w+?)\]/g
export async function resolvePages(srcDir: string, userConfig: UserConfig) { export async function resolvePages(
srcDir: string,
userConfig: UserConfig,
logger: Logger
) {
// Important: fast-glob doesn't guarantee order of the returned files. // Important: fast-glob doesn't guarantee order of the returned files.
// We must sort the pages so the input list to rollup is stable across // We must sort the pages so the input list to rollup is stable across
// builds - otherwise different input order could result in different exports // builds - otherwise different input order could result in different exports
@ -39,7 +44,11 @@ export async function resolvePages(srcDir: string, userConfig: UserConfig) {
;(dynamicRouteRE.test(file) ? dynamicRouteFiles : pages).push(file) ;(dynamicRouteRE.test(file) ? dynamicRouteFiles : pages).push(file)
}) })
const dynamicRoutes = await resolveDynamicRoutes(srcDir, dynamicRouteFiles) const dynamicRoutes = await resolveDynamicRoutes(
srcDir,
dynamicRouteFiles,
logger
)
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)
@ -141,7 +150,7 @@ export const dynamicRoutesPlugin = async (
if (!/\.md$/.test(ctx.file)) { if (!/\.md$/.test(ctx.file)) {
Object.assign( Object.assign(
config, config,
await resolvePages(config.srcDir, config.userConfig) await resolvePages(config.srcDir, config.userConfig, config.logger)
) )
} }
for (const id of mods) { for (const id of mods) {
@ -154,7 +163,8 @@ export const dynamicRoutesPlugin = async (
export async function resolveDynamicRoutes( export async function resolveDynamicRoutes(
srcDir: string, srcDir: string,
routes: string[] routes: string[],
logger: Logger
): Promise<SiteConfig['dynamicRoutes']> { ): Promise<SiteConfig['dynamicRoutes']> {
const pendingResolveRoutes: Promise<ResolvedRouteConfig[]>[] = [] const pendingResolveRoutes: Promise<ResolvedRouteConfig[]>[] = []
const routeFileToModulesMap: Record<string, Set<string>> = {} const routeFileToModulesMap: Record<string, Set<string>> = {}
@ -170,7 +180,7 @@ export async function resolveDynamicRoutes(
const pathsFile = paths.find((p) => fs.existsSync(p)) const pathsFile = paths.find((p) => fs.existsSync(p))
if (pathsFile == null) { if (pathsFile == null) {
console.warn( logger.warn(
c.yellow( c.yellow(
`Missing paths file for dynamic route ${route}: ` + `Missing paths file for dynamic route ${route}: ` +
`a corresponding ${paths[0]} (or .ts/.mjs/.mts) file is needed.` `a corresponding ${paths[0]} (or .ts/.mjs/.mts) file is needed.`
@ -183,15 +193,15 @@ export async function resolveDynamicRoutes(
let mod = routeModuleCache.get(pathsFile) let mod = routeModuleCache.get(pathsFile)
if (!mod) { if (!mod) {
try { try {
mod = (await loadConfigFromFile({} as any, pathsFile)) as RouteModule mod = (await loadConfigFromFile(
{} as any,
pathsFile,
undefined,
'silent'
)) as RouteModule
routeModuleCache.set(pathsFile, mod) routeModuleCache.set(pathsFile, mod)
} catch (e) { } catch (e: any) {
console.warn( logger.warn(`${c.yellow(`Failed to load ${pathsFile}:`)}\n${e.stack}`)
c.yellow(
`Invalid paths file export in ${pathsFile}. ` +
`Expects default export of an object with a "paths" property.`
)
)
continue continue
} }
} }
@ -210,7 +220,7 @@ export async function resolveDynamicRoutes(
const loader = mod!.config.paths const loader = mod!.config.paths
if (!loader) { if (!loader) {
console.warn( logger.warn(
c.yellow( c.yellow(
`Invalid paths file export in ${pathsFile}. ` + `Invalid paths file export in ${pathsFile}. ` +
`Missing "paths" property from default export.` `Missing "paths" property from default export.`

Loading…
Cancel
Save