Skip to content

Commit

Permalink
fix: print errors when importing an invalid dynamic route (#3201)
Browse files Browse the repository at this point in the history
Co-authored-by: Divyansh Singh <[email protected]>
  • Loading branch information
arianrhodsandlot and brc-dd authored Dec 30, 2023
1 parent 9c20e3b commit 6d89a08
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ export async function resolveConfig(

const { pages, dynamicRoutes, rewrites } = await resolvePages(
srcDir,
userConfig
userConfig,
logger
)

const config: SiteConfig = {
Expand Down
6 changes: 5 additions & 1 deletion src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,11 @@ export async function createVitePressPlugin(
if (file.endsWith('.md')) {
Object.assign(
siteConfig,
await resolvePages(siteConfig.srcDir, siteConfig.userConfig)
await resolvePages(
siteConfig.srcDir,
siteConfig.userConfig,
siteConfig.logger
)
)
}

Expand Down
38 changes: 24 additions & 14 deletions src/node/plugins/dynamicRoutesPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
loadConfigFromFile,
normalizePath,
type Logger,
type Plugin,
type ViteDevServer
} from 'vite'
Expand All @@ -13,7 +14,11 @@ import { resolveRewrites } from './rewritesPlugin'

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.
// We must sort the pages so the input list to rollup is stable across
// builds - otherwise different input order could result in different exports
Expand All @@ -39,7 +44,11 @@ export async function resolvePages(srcDir: string, userConfig: UserConfig) {
;(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))

const rewrites = resolveRewrites(pages, userConfig.rewrites)
Expand Down Expand Up @@ -141,7 +150,7 @@ export const dynamicRoutesPlugin = async (
if (!/\.md$/.test(ctx.file)) {
Object.assign(
config,
await resolvePages(config.srcDir, config.userConfig)
await resolvePages(config.srcDir, config.userConfig, config.logger)
)
}
for (const id of mods) {
Expand All @@ -154,7 +163,8 @@ export const dynamicRoutesPlugin = async (

export async function resolveDynamicRoutes(
srcDir: string,
routes: string[]
routes: string[],
logger: Logger
): Promise<SiteConfig['dynamicRoutes']> {
const pendingResolveRoutes: Promise<ResolvedRouteConfig[]>[] = []
const routeFileToModulesMap: Record<string, Set<string>> = {}
Expand All @@ -170,7 +180,7 @@ export async function resolveDynamicRoutes(
const pathsFile = paths.find((p) => fs.existsSync(p))

if (pathsFile == null) {
console.warn(
logger.warn(
c.yellow(
`Missing paths file for dynamic route ${route}: ` +
`a corresponding ${paths[0]} (or .ts/.mjs/.mts) file is needed.`
Expand All @@ -183,15 +193,15 @@ export async function resolveDynamicRoutes(
let mod = routeModuleCache.get(pathsFile)
if (!mod) {
try {
mod = (await loadConfigFromFile({} as any, pathsFile)) as RouteModule
mod = (await loadConfigFromFile(
{} as any,
pathsFile,
undefined,
'silent'
)) as RouteModule
routeModuleCache.set(pathsFile, mod)
} catch (e) {
console.warn(
c.yellow(
`Invalid paths file export in ${pathsFile}. ` +
`Expects default export of an object with a "paths" property.`
)
)
} catch (e: any) {
logger.warn(`${c.yellow(`Failed to load ${pathsFile}:`)}\n${e.stack}`)
continue
}
}
Expand All @@ -210,7 +220,7 @@ export async function resolveDynamicRoutes(

const loader = mod!.config.paths
if (!loader) {
console.warn(
logger.warn(
c.yellow(
`Invalid paths file export in ${pathsFile}. ` +
`Missing "paths" property from default export.`
Expand Down

0 comments on commit 6d89a08

Please sign in to comment.