Skip to content

Commit e73dce3

Browse files
committed
minor tweaks
1 parent 27de822 commit e73dce3

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/node/config.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
VP_SOURCE_KEY,
1919
slash,
2020
type AdditionalConfig,
21-
type AdditionalConfigDict,
2221
type Awaitable,
2322
type HeadConfig,
2423
type SiteData
@@ -170,15 +169,11 @@ export async function resolveConfig(
170169
}
171170

172171
const supportedConfigExtensions = ['js', 'ts', 'mjs', 'mts']
172+
const additionalConfigRE = /(?:^|\/|\\)config\.m?[jt]s$/
173+
const additionalConfigGlob = `**/config.{js,mjs,ts,mts}`
173174

174175
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)
182177
}
183178

184179
/**
@@ -197,38 +192,50 @@ async function gatherAdditionalConfig(
197192
root: string,
198193
command: 'serve' | 'build',
199194
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, {
204201
cwd: path.resolve(root, srcDir),
205202
dot: false, // conveniently ignores .vitepress/*
206-
ignore: ['**/node_modules/**', '**/.git/**']
203+
ignore: ['**/node_modules/**', ...srcExclude],
204+
expandDirectories: false
207205
})
206+
208207
const deps: string[][] = []
208+
209209
const exports = await Promise.all(
210210
candidates.map(async (file) => {
211211
const id = '/' + dirname(slash(file))
212+
212213
const configExports = await loadConfigFromFile(
213214
{ command, mode },
214215
normalizePath(path.resolve(root, srcDir, file)),
215216
root
216217
).catch(console.error) // Skip additionalConfig file if it fails to load
218+
217219
if (!configExports) {
218220
debug(`Failed to load additional config from ${file}`)
219-
return [id, undefined]
221+
return
220222
}
223+
221224
deps.push(
222225
configExports.dependencies.map((file) =>
223226
normalizePath(path.resolve(file))
224227
)
225228
)
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
229235
})
230236
)
231-
return [Object.fromEntries(exports.filter(([id, config]) => config)), deps]
237+
238+
return [Object.fromEntries(exports.filter((e) => e != null)), deps] as const
232239
}
233240

234241
export async function resolveUserConfig(
@@ -265,7 +272,8 @@ export async function resolveUserConfig(
265272
root,
266273
command,
267274
mode,
268-
userConfig.srcDir
275+
userConfig.srcDir,
276+
userConfig.srcExclude
269277
)
270278
userConfig.additionalConfig = additionalConfig
271279
configDeps = configDeps.concat(...additionalDeps)

0 commit comments

Comments
 (0)