Skip to content

Commit

Permalink
feat: 配置文件依赖变更进行热更新
Browse files Browse the repository at this point in the history
  • Loading branch information
c0dedance committed Oct 15, 2023
1 parent 8af907f commit 4958ce6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
28 changes: 17 additions & 11 deletions src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ export async function resolveConfig(
command: 'serve' | 'build',
mode: 'development' | 'production'
): Promise<SiteConfig> {
const [configPath, userConfig] = await resolveUserConfig(root, command, mode)
const [configPath, userConfig, userConfigDeps] = await resolveUserConfig(
root,
command,
mode
)
const siteConfig: SiteConfig = {
root,
configPath: configPath,
configDeps: userConfigDeps,
siteData: resolveSiteData(userConfig as UserConfig),
}
console.log(siteConfig, 'siteConfig')
Expand All @@ -31,20 +36,21 @@ export async function resolveUserConfig(
// 1. 获取配置路径 支持js 和 ts 格式
const configPath = getUserConfigPath(root)
// 2.解析配置文件
const { config: rawConfig = {} as RawUserConfig } = await loadConfigFromFile(
{
command,
mode,
},
configPath,
root
)
const { config: rawConfig = {} as RawUserConfig, dependencies = [] } =
await loadConfigFromFile(
{
command,
mode,
},
configPath,
root
)
if (rawConfig) {
const config =
typeof rawConfig === 'function' ? await rawConfig() : rawConfig
return [configPath, config] as const
return [configPath, config, dependencies] as const
}
return [configPath, {} as UserConfig] as const
return [configPath, {} as UserConfig, dependencies] as const
}

export function resolveSiteData(userConfig: UserConfig): UserConfig {
Expand Down
2 changes: 1 addition & 1 deletion src/node/plugin-r-press/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function pluginConfig(
}
},
async handleHotUpdate(ctx) {
const customWatchedFiles = [config.configPath]
const customWatchedFiles = [config.configPath, ...config.configDeps]
const include = (id: string) =>
customWatchedFiles.some((file) => id.includes(file))

Expand Down
1 change: 1 addition & 0 deletions src/shared/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { UserConfig as ViteUserConfig } from 'vite'
export interface SiteConfig {
root: string
configPath: string
configDeps: string[]
siteData: UserConfig
}

Expand Down

0 comments on commit 4958ce6

Please sign in to comment.