feat(build): handle change of config file dependencies (#1009)

Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
pull/1101/head
CHOYSEN 2 years ago committed by GitHub
parent f4f1a6ccd6
commit 8e6665bd8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -119,6 +119,7 @@ export interface SiteConfig<ThemeConfig = any>
srcDir: string srcDir: string
site: SiteData<ThemeConfig> site: SiteData<ThemeConfig>
configPath: string | undefined configPath: string | undefined
configDeps: string[]
themeDir: string themeDir: string
outDir: string outDir: string
tempDir: string tempDir: string
@ -150,7 +151,11 @@ export async function resolveConfig(
command: 'serve' | 'build' = 'serve', command: 'serve' | 'build' = 'serve',
mode = 'development' mode = 'development'
): Promise<SiteConfig> { ): Promise<SiteConfig> {
const [userConfig, configPath] = await resolveUserConfig(root, command, mode) const [userConfig, configPath, configDeps] = await resolveUserConfig(
root,
command,
mode
)
const site = await resolveSiteData(root, userConfig) const site = await resolveSiteData(root, userConfig)
const srcDir = path.resolve(root, userConfig.srcDir || '.') const srcDir = path.resolve(root, userConfig.srcDir || '.')
const outDir = userConfig.outDir const outDir = userConfig.outDir
@ -183,6 +188,7 @@ export async function resolveConfig(
themeDir, themeDir,
pages, pages,
configPath, configPath,
configDeps,
outDir, outDir,
tempDir: resolve(root, '.temp'), tempDir: resolve(root, '.temp'),
markdown: userConfig.markdown, markdown: userConfig.markdown,
@ -206,32 +212,32 @@ async function resolveUserConfig(
root: string, root: string,
command: 'serve' | 'build', command: 'serve' | 'build',
mode: string mode: string
): Promise<[UserConfig, string | undefined]> { ): Promise<[UserConfig, string | undefined, string[]]> {
// load user config // load user config
const configPath = supportedConfigExtensions const configPath = supportedConfigExtensions
.map((ext) => resolve(root, `config.${ext}`)) .map((ext) => resolve(root, `config.${ext}`))
.find(fs.pathExistsSync) .find(fs.pathExistsSync)
const userConfig: RawConfigExports = configPath let userConfig: RawConfigExports = {}
? (( let configDeps: string[] = []
await loadConfigFromFile( if (!configPath) {
{ debug(`no config file found.`)
command, } else {
mode const configExports = await loadConfigFromFile(
}, { command, mode },
configPath, configPath,
root root
) )
)?.config as any) if (configExports) {
: {} userConfig = configExports.config
configDeps = configExports.dependencies.map((file) =>
if (configPath) { normalizePath(path.resolve(file))
)
}
debug(`loaded config at ${c.yellow(configPath)}`) debug(`loaded config at ${c.yellow(configPath)}`)
} else {
debug(`no config file found.`)
} }
return [await resolveConfigExtends(userConfig), configPath] return [await resolveConfigExtends(userConfig), configPath, configDeps]
} }
async function resolveConfigExtends( async function resolveConfigExtends(

@ -39,6 +39,7 @@ export async function createVitePressPlugin(
const { const {
srcDir, srcDir,
configPath, configPath,
configDeps,
alias, alias,
markdown, markdown,
site, site,
@ -162,6 +163,7 @@ export async function createVitePressPlugin(
configureServer(server) { configureServer(server) {
if (configPath) { if (configPath) {
server.watcher.add(configPath) server.watcher.add(configPath)
configDeps.forEach((file) => server.watcher.add(file))
} }
// serve our index.html after vite history fallback // serve our index.html after vite history fallback
@ -244,7 +246,7 @@ export async function createVitePressPlugin(
async handleHotUpdate(ctx) { async handleHotUpdate(ctx) {
// handle config hmr // handle config hmr
const { file, read, server } = ctx const { file, read, server } = ctx
if (file === configPath) { if (file === configPath || configDeps.includes(file)) {
const newData = await resolveSiteData(root) const newData = await resolveSiteData(root)
if (newData.base !== siteData.base) { if (newData.base !== siteData.base) {
console.warn( console.warn(

Loading…
Cancel
Save