feat: support function config

pull/363/head
Evan You 3 years ago
parent 71b6845bc5
commit e74c5f06d1

@ -111,20 +111,22 @@ export async function resolveConfig(
return config return config
} }
export async function resolveUserConfig(root: string) { export async function resolveUserConfig(root: string): Promise<UserConfig> {
// load user config // load user config
const configPath = resolve(root, 'config.js') const configPath = resolve(root, 'config.js')
const hasUserConfig = await fs.pathExists(configPath) const hasUserConfig = await fs.pathExists(configPath)
// always delete cache first before loading config // always delete cache first before loading config
delete require.cache[configPath] delete require.cache[configPath]
const userConfig: UserConfig = hasUserConfig ? require(configPath) : {} const userConfig: UserConfig | (() => UserConfig) = hasUserConfig
? require(configPath)
: {}
if (hasUserConfig) { if (hasUserConfig) {
debug(`loaded config at ${chalk.yellow(configPath)}`) debug(`loaded config at ${chalk.yellow(configPath)}`)
} else { } else {
debug(`no config file found.`) debug(`no config file found.`)
} }
return userConfig return typeof userConfig === 'function' ? userConfig() : userConfig
} }
export async function resolveSiteData( export async function resolveSiteData(

Loading…
Cancel
Save