1
1
import {
2
2
loadConfigFromFile ,
3
3
normalizePath ,
4
+ type Logger ,
4
5
type Plugin ,
5
6
type ViteDevServer
6
7
} from 'vite'
@@ -13,7 +14,11 @@ import { resolveRewrites } from './rewritesPlugin'
13
14
14
15
export const dynamicRouteRE = / \[ ( \w + ?) \] / g
15
16
16
- export async function resolvePages ( srcDir : string , userConfig : UserConfig ) {
17
+ export async function resolvePages (
18
+ srcDir : string ,
19
+ userConfig : UserConfig ,
20
+ logger : Logger
21
+ ) {
17
22
// Important: fast-glob doesn't guarantee order of the returned files.
18
23
// We must sort the pages so the input list to rollup is stable across
19
24
// builds - otherwise different input order could result in different exports
@@ -39,7 +44,11 @@ export async function resolvePages(srcDir: string, userConfig: UserConfig) {
39
44
; ( dynamicRouteRE . test ( file ) ? dynamicRouteFiles : pages ) . push ( file )
40
45
} )
41
46
42
- const dynamicRoutes = await resolveDynamicRoutes ( srcDir , dynamicRouteFiles )
47
+ const dynamicRoutes = await resolveDynamicRoutes (
48
+ srcDir ,
49
+ dynamicRouteFiles ,
50
+ logger
51
+ )
43
52
pages . push ( ...dynamicRoutes . routes . map ( ( r ) => r . path ) )
44
53
45
54
const rewrites = resolveRewrites ( pages , userConfig . rewrites )
@@ -141,7 +150,7 @@ export const dynamicRoutesPlugin = async (
141
150
if ( ! / \. m d $ / . test ( ctx . file ) ) {
142
151
Object . assign (
143
152
config ,
144
- await resolvePages ( config . srcDir , config . userConfig )
153
+ await resolvePages ( config . srcDir , config . userConfig , config . logger )
145
154
)
146
155
}
147
156
for ( const id of mods ) {
@@ -154,7 +163,8 @@ export const dynamicRoutesPlugin = async (
154
163
155
164
export async function resolveDynamicRoutes (
156
165
srcDir : string ,
157
- routes : string [ ]
166
+ routes : string [ ] ,
167
+ logger : Logger
158
168
) : Promise < SiteConfig [ 'dynamicRoutes' ] > {
159
169
const pendingResolveRoutes : Promise < ResolvedRouteConfig [ ] > [ ] = [ ]
160
170
const routeFileToModulesMap : Record < string , Set < string > > = { }
@@ -170,7 +180,7 @@ export async function resolveDynamicRoutes(
170
180
const pathsFile = paths . find ( ( p ) => fs . existsSync ( p ) )
171
181
172
182
if ( pathsFile == null ) {
173
- console . warn (
183
+ logger . warn (
174
184
c . yellow (
175
185
`Missing paths file for dynamic route ${ route } : ` +
176
186
`a corresponding ${ paths [ 0 ] } (or .ts/.mjs/.mts) file is needed.`
@@ -183,15 +193,15 @@ export async function resolveDynamicRoutes(
183
193
let mod = routeModuleCache . get ( pathsFile )
184
194
if ( ! mod ) {
185
195
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
187
202
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 } ` )
195
205
continue
196
206
}
197
207
}
@@ -210,7 +220,7 @@ export async function resolveDynamicRoutes(
210
220
211
221
const loader = mod ! . config . paths
212
222
if ( ! loader ) {
213
- console . warn (
223
+ logger . warn (
214
224
c . yellow (
215
225
`Invalid paths file export in ${ pathsFile } . ` +
216
226
`Missing "paths" property from default export.`
0 commit comments