From a8cb8f2d9f79296d71b96616fe92802e2199e666 Mon Sep 17 00:00:00 2001 From: CHOYSEN Date: Mon, 18 Jul 2022 13:22:09 +0800 Subject: [PATCH 1/2] feat: handle change of config file dependencies --- src/node/config.ts | 48 ++++++++++++++++++++++++++++------------------ src/node/plugin.ts | 4 +++- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/node/config.ts b/src/node/config.ts index 1fac1d1b083a..c8658b5f0606 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -100,6 +100,7 @@ export interface SiteConfig srcDir: string site: SiteData configPath: string | undefined + configDependencies: string[] themeDir: string outDir: string tempDir: string @@ -131,7 +132,11 @@ export async function resolveConfig( command: 'serve' | 'build' = 'serve', mode = 'development' ): Promise { - const [userConfig, configPath] = await resolveUserConfig(root, command, mode) + const [userConfig, configPath, configDependencies] = await resolveUserConfig( + root, + command, + mode + ) const site = await resolveSiteData(root, userConfig) const srcDir = path.resolve(root, userConfig.srcDir || '.') const outDir = userConfig.outDir @@ -164,6 +169,7 @@ export async function resolveConfig( themeDir, pages, configPath, + configDependencies, outDir, tempDir: resolve(root, '.temp'), markdown: userConfig.markdown, @@ -186,32 +192,36 @@ 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 configDependencies: string[] = [] + if (!configPath) { debug(`no config file found.`) + } else { + const configExports = await loadConfigFromFile( + { command, mode }, + configPath, + root + ) + if (configExports) { + userConfig = configExports.config + configDependencies = 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, + configDependencies + ] } async function resolveConfigExtends( diff --git a/src/node/plugin.ts b/src/node/plugin.ts index a5fb7cceecc7..0e174e1a9811 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -39,6 +39,7 @@ export async function createVitePressPlugin( const { srcDir, configPath, + configDependencies, alias, markdown, site, @@ -162,6 +163,7 @@ export async function createVitePressPlugin( configureServer(server) { if (configPath) { server.watcher.add(configPath) + configDependencies.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 || configDependencies.includes(file)) { const newData = await resolveSiteData(root) if (newData.base !== siteData.base) { console.warn( From 430a3c83fbf9b5b63f0644a1b7cf8c81e47ac370 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Mon, 1 Aug 2022 23:20:50 +0530 Subject: [PATCH 2/2] rename `configDependencies` to `configDeps` --- src/node/config.ts | 16 ++++++---------- src/node/plugin.ts | 6 +++--- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/node/config.ts b/src/node/config.ts index c8658b5f0606..be16e656cb40 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -100,7 +100,7 @@ export interface SiteConfig srcDir: string site: SiteData configPath: string | undefined - configDependencies: string[] + configDeps: string[] themeDir: string outDir: string tempDir: string @@ -132,7 +132,7 @@ export async function resolveConfig( command: 'serve' | 'build' = 'serve', mode = 'development' ): Promise { - const [userConfig, configPath, configDependencies] = await resolveUserConfig( + const [userConfig, configPath, configDeps] = await resolveUserConfig( root, command, mode @@ -169,7 +169,7 @@ export async function resolveConfig( themeDir, pages, configPath, - configDependencies, + configDeps, outDir, tempDir: resolve(root, '.temp'), markdown: userConfig.markdown, @@ -199,7 +199,7 @@ async function resolveUserConfig( .find(fs.pathExistsSync) let userConfig: RawConfigExports = {} - let configDependencies: string[] = [] + let configDeps: string[] = [] if (!configPath) { debug(`no config file found.`) } else { @@ -210,18 +210,14 @@ async function resolveUserConfig( ) if (configExports) { userConfig = configExports.config - configDependencies = configExports.dependencies.map((file) => + configDeps = configExports.dependencies.map((file) => normalizePath(path.resolve(file)) ) } debug(`loaded config at ${c.yellow(configPath)}`) } - return [ - await resolveConfigExtends(userConfig), - configPath, - configDependencies - ] + return [await resolveConfigExtends(userConfig), configPath, configDeps] } async function resolveConfigExtends( diff --git a/src/node/plugin.ts b/src/node/plugin.ts index 0e174e1a9811..78a4dc026cee 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -39,7 +39,7 @@ export async function createVitePressPlugin( const { srcDir, configPath, - configDependencies, + configDeps, alias, markdown, site, @@ -163,7 +163,7 @@ export async function createVitePressPlugin( configureServer(server) { if (configPath) { server.watcher.add(configPath) - configDependencies.forEach((file) => server.watcher.add(file)) + configDeps.forEach((file) => server.watcher.add(file)) } // serve our index.html after vite history fallback @@ -246,7 +246,7 @@ export async function createVitePressPlugin( async handleHotUpdate(ctx) { // handle config hmr const { file, read, server } = ctx - if (file === configPath || configDependencies.includes(file)) { + if (file === configPath || configDeps.includes(file)) { const newData = await resolveSiteData(root) if (newData.base !== siteData.base) { console.warn(