From 8e6665bd8de66a8249fca92fbb1b9a4f6d76a041 Mon Sep 17 00:00:00 2001 From: CHOYSEN Date: Tue, 2 Aug 2022 01:57:14 +0800 Subject: [PATCH] feat(build): handle change of config file dependencies (#1009) Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> --- src/node/config.ts | 44 +++++++++++++++++++++++++------------------- src/node/plugin.ts | 4 +++- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/node/config.ts b/src/node/config.ts index 28ecdec87af8..a938f15302c2 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -119,6 +119,7 @@ export interface SiteConfig srcDir: string site: SiteData configPath: string | undefined + configDeps: string[] themeDir: string outDir: string tempDir: string @@ -150,7 +151,11 @@ export async function resolveConfig( command: 'serve' | 'build' = 'serve', mode = 'development' ): Promise { - const [userConfig, configPath] = await resolveUserConfig(root, command, mode) + const [userConfig, configPath, configDeps] = await resolveUserConfig( + root, + command, + mode + ) const site = await resolveSiteData(root, userConfig) const srcDir = path.resolve(root, userConfig.srcDir || '.') const outDir = userConfig.outDir @@ -183,6 +188,7 @@ export async function resolveConfig( themeDir, pages, configPath, + configDeps, outDir, tempDir: resolve(root, '.temp'), markdown: userConfig.markdown, @@ -206,32 +212,32 @@ async function resolveUserConfig( root: string, command: 'serve' | 'build', mode: string -): Promise<[UserConfig, string | undefined]> { +): Promise<[UserConfig, string | undefined, string[]]> { // load user config const configPath = supportedConfigExtensions .map((ext) => resolve(root, `config.${ext}`)) .find(fs.pathExistsSync) - const userConfig: RawConfigExports = configPath - ? (( - await loadConfigFromFile( - { - command, - mode - }, - configPath, - root - ) - )?.config as any) - : {} - - if (configPath) { - debug(`loaded config at ${c.yellow(configPath)}`) - } else { + let userConfig: RawConfigExports = {} + let configDeps: string[] = [] + if (!configPath) { debug(`no config file found.`) + } else { + const configExports = await loadConfigFromFile( + { command, mode }, + configPath, + root + ) + if (configExports) { + userConfig = configExports.config + configDeps = configExports.dependencies.map((file) => + normalizePath(path.resolve(file)) + ) + } + debug(`loaded config at ${c.yellow(configPath)}`) } - return [await resolveConfigExtends(userConfig), configPath] + return [await resolveConfigExtends(userConfig), configPath, configDeps] } async function resolveConfigExtends( diff --git a/src/node/plugin.ts b/src/node/plugin.ts index a5fb7cceecc7..78a4dc026cee 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -39,6 +39,7 @@ export async function createVitePressPlugin( const { srcDir, configPath, + configDeps, alias, markdown, site, @@ -162,6 +163,7 @@ export async function createVitePressPlugin( configureServer(server) { if (configPath) { server.watcher.add(configPath) + configDeps.forEach((file) => server.watcher.add(file)) } // serve our index.html after vite history fallback @@ -244,7 +246,7 @@ export async function createVitePressPlugin( async handleHotUpdate(ctx) { // handle config hmr const { file, read, server } = ctx - if (file === configPath) { + if (file === configPath || configDeps.includes(file)) { const newData = await resolveSiteData(root) if (newData.base !== siteData.base) { console.warn(