Skip to content

Commit 6d89a08

Browse files
fix: print errors when importing an invalid dynamic route (#3201)
Co-authored-by: Divyansh Singh <[email protected]>
1 parent 9c20e3b commit 6d89a08

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

Diff for: src/node/config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ export async function resolveConfig(
104104

105105
const { pages, dynamicRoutes, rewrites } = await resolvePages(
106106
srcDir,
107-
userConfig
107+
userConfig,
108+
logger
108109
)
109110

110111
const config: SiteConfig = {

Diff for: src/node/plugin.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,11 @@ export async function createVitePressPlugin(
268268
if (file.endsWith('.md')) {
269269
Object.assign(
270270
siteConfig,
271-
await resolvePages(siteConfig.srcDir, siteConfig.userConfig)
271+
await resolvePages(
272+
siteConfig.srcDir,
273+
siteConfig.userConfig,
274+
siteConfig.logger
275+
)
272276
)
273277
}
274278

Diff for: src/node/plugins/dynamicRoutesPlugin.ts

+24-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
loadConfigFromFile,
33
normalizePath,
4+
type Logger,
45
type Plugin,
56
type ViteDevServer
67
} from 'vite'
@@ -13,7 +14,11 @@ import { resolveRewrites } from './rewritesPlugin'
1314

1415
export const dynamicRouteRE = /\[(\w+?)\]/g
1516

16-
export async function resolvePages(srcDir: string, userConfig: UserConfig) {
17+
export async function resolvePages(
18+
srcDir: string,
19+
userConfig: UserConfig,
20+
logger: Logger
21+
) {
1722
// Important: fast-glob doesn't guarantee order of the returned files.
1823
// We must sort the pages so the input list to rollup is stable across
1924
// builds - otherwise different input order could result in different exports
@@ -39,7 +44,11 @@ export async function resolvePages(srcDir: string, userConfig: UserConfig) {
3944
;(dynamicRouteRE.test(file) ? dynamicRouteFiles : pages).push(file)
4045
})
4146

42-
const dynamicRoutes = await resolveDynamicRoutes(srcDir, dynamicRouteFiles)
47+
const dynamicRoutes = await resolveDynamicRoutes(
48+
srcDir,
49+
dynamicRouteFiles,
50+
logger
51+
)
4352
pages.push(...dynamicRoutes.routes.map((r) => r.path))
4453

4554
const rewrites = resolveRewrites(pages, userConfig.rewrites)
@@ -141,7 +150,7 @@ export const dynamicRoutesPlugin = async (
141150
if (!/\.md$/.test(ctx.file)) {
142151
Object.assign(
143152
config,
144-
await resolvePages(config.srcDir, config.userConfig)
153+
await resolvePages(config.srcDir, config.userConfig, config.logger)
145154
)
146155
}
147156
for (const id of mods) {
@@ -154,7 +163,8 @@ export const dynamicRoutesPlugin = async (
154163

155164
export async function resolveDynamicRoutes(
156165
srcDir: string,
157-
routes: string[]
166+
routes: string[],
167+
logger: Logger
158168
): Promise<SiteConfig['dynamicRoutes']> {
159169
const pendingResolveRoutes: Promise<ResolvedRouteConfig[]>[] = []
160170
const routeFileToModulesMap: Record<string, Set<string>> = {}
@@ -170,7 +180,7 @@ export async function resolveDynamicRoutes(
170180
const pathsFile = paths.find((p) => fs.existsSync(p))
171181

172182
if (pathsFile == null) {
173-
console.warn(
183+
logger.warn(
174184
c.yellow(
175185
`Missing paths file for dynamic route ${route}: ` +
176186
`a corresponding ${paths[0]} (or .ts/.mjs/.mts) file is needed.`
@@ -183,15 +193,15 @@ export async function resolveDynamicRoutes(
183193
let mod = routeModuleCache.get(pathsFile)
184194
if (!mod) {
185195
try {
186-
mod = (await loadConfigFromFile({} as any, pathsFile)) as RouteModule
196+
mod = (await loadConfigFromFile(
197+
{} as any,
198+
pathsFile,
199+
undefined,
200+
'silent'
201+
)) as RouteModule
187202
routeModuleCache.set(pathsFile, mod)
188-
} catch (e) {
189-
console.warn(
190-
c.yellow(
191-
`Invalid paths file export in ${pathsFile}. ` +
192-
`Expects default export of an object with a "paths" property.`
193-
)
194-
)
203+
} catch (e: any) {
204+
logger.warn(`${c.yellow(`Failed to load ${pathsFile}:`)}\n${e.stack}`)
195205
continue
196206
}
197207
}
@@ -210,7 +220,7 @@ export async function resolveDynamicRoutes(
210220

211221
const loader = mod!.config.paths
212222
if (!loader) {
213-
console.warn(
223+
logger.warn(
214224
c.yellow(
215225
`Invalid paths file export in ${pathsFile}. ` +
216226
`Missing "paths" property from default export.`

0 commit comments

Comments
 (0)