Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export interface SiteConfig<ThemeConfig = any>
srcDir: string
site: SiteData<ThemeConfig>
configPath: string | undefined
configDeps: string[]
themeDir: string
outDir: string
tempDir: string
Expand Down Expand Up @@ -131,7 +132,11 @@ export async function resolveConfig(
command: 'serve' | 'build' = 'serve',
mode = 'development'
): 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 srcDir = path.resolve(root, userConfig.srcDir || '.')
const outDir = userConfig.outDir
Expand Down Expand Up @@ -164,6 +169,7 @@ export async function resolveConfig(
themeDir,
pages,
configPath,
configDeps,
outDir,
tempDir: resolve(root, '.temp'),
markdown: userConfig.markdown,
Expand All @@ -186,32 +192,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(
Expand Down
4 changes: 3 additions & 1 deletion src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function createVitePressPlugin(
const {
srcDir,
configPath,
configDeps,
alias,
markdown,
site,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down