@@ -18,7 +18,6 @@ import {
18
18
VP_SOURCE_KEY ,
19
19
slash ,
20
20
type AdditionalConfig ,
21
- type AdditionalConfigDict ,
22
21
type Awaitable ,
23
22
type HeadConfig ,
24
23
type SiteData
@@ -170,15 +169,11 @@ export async function resolveConfig(
170
169
}
171
170
172
171
const supportedConfigExtensions = [ 'js' , 'ts' , 'mjs' , 'mts' ]
172
+ const additionalConfigRE = / (?: ^ | \/ | \\ ) c o n f i g \. m ? [ j t ] s $ /
173
+ const additionalConfigGlob = `**/config.{js,mjs,ts,mts}`
173
174
174
175
export function isAdditionalConfigFile ( path : string ) {
175
- const filename_to_check = path . split ( '/' ) . pop ( ) ?? ''
176
- for ( const filename of supportedConfigExtensions . map ( ( e ) => `config.${ e } ` ) ) {
177
- if ( filename_to_check === filename ) {
178
- return true
179
- }
180
- }
181
- return false
176
+ return additionalConfigRE . test ( path )
182
177
}
183
178
184
179
/**
@@ -197,38 +192,50 @@ async function gatherAdditionalConfig(
197
192
root : string ,
198
193
command : 'serve' | 'build' ,
199
194
mode : string ,
200
- srcDir : string = '.'
201
- ) : Promise < [ AdditionalConfigDict , string [ ] [ ] ] > {
202
- const pattern = `**/config.{${ supportedConfigExtensions . join ( ',' ) } }`
203
- const candidates = await glob ( pattern , {
195
+ srcDir : string = '.' ,
196
+ srcExclude : string [ ] = [ ]
197
+ ) {
198
+ //
199
+
200
+ const candidates = await glob ( additionalConfigGlob , {
204
201
cwd : path . resolve ( root , srcDir ) ,
205
202
dot : false , // conveniently ignores .vitepress/*
206
- ignore : [ '**/node_modules/**' , '**/.git/**' ]
203
+ ignore : [ '**/node_modules/**' , ...srcExclude ] ,
204
+ expandDirectories : false
207
205
} )
206
+
208
207
const deps : string [ ] [ ] = [ ]
208
+
209
209
const exports = await Promise . all (
210
210
candidates . map ( async ( file ) => {
211
211
const id = '/' + dirname ( slash ( file ) )
212
+
212
213
const configExports = await loadConfigFromFile (
213
214
{ command, mode } ,
214
215
normalizePath ( path . resolve ( root , srcDir , file ) ) ,
215
216
root
216
217
) . catch ( console . error ) // Skip additionalConfig file if it fails to load
218
+
217
219
if ( ! configExports ) {
218
220
debug ( `Failed to load additional config from ${ file } ` )
219
- return [ id , undefined ]
221
+ return
220
222
}
223
+
221
224
deps . push (
222
225
configExports . dependencies . map ( ( file ) =>
223
226
normalizePath ( path . resolve ( file ) )
224
227
)
225
228
)
226
- if ( mode === 'development' )
227
- ( configExports . config as any ) [ VP_SOURCE_KEY ] = '/' + slash ( file )
228
- return [ id , configExports . config as AdditionalConfig ]
229
+
230
+ if ( mode === 'development' ) {
231
+ ; ( configExports . config as any ) [ VP_SOURCE_KEY ] = '/' + slash ( file )
232
+ }
233
+
234
+ return [ id , configExports . config as AdditionalConfig ] as const
229
235
} )
230
236
)
231
- return [ Object . fromEntries ( exports . filter ( ( [ id , config ] ) => config ) ) , deps ]
237
+
238
+ return [ Object . fromEntries ( exports . filter ( ( e ) => e != null ) ) , deps ] as const
232
239
}
233
240
234
241
export async function resolveUserConfig (
@@ -265,7 +272,8 @@ export async function resolveUserConfig(
265
272
root ,
266
273
command ,
267
274
mode ,
268
- userConfig . srcDir
275
+ userConfig . srcDir ,
276
+ userConfig . srcExclude
269
277
)
270
278
userConfig . additionalConfig = additionalConfig
271
279
configDeps = configDeps . concat ( ...additionalDeps )
0 commit comments