fix(theme): fix theme config hmr fail (#820)

This commit is contained in:
pengzhanbo 2026-01-19 21:40:23 +08:00 committed by GitHub
parent 1dfbb872f7
commit 48970dd559
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 4 deletions

View File

@ -3,7 +3,7 @@ import type { App } from 'vuepress'
import type { ThemeOptions } from '../../shared/index.js'
import EventEmitter from 'node:events'
import process from 'node:process'
import { deepMerge } from '@pengzhanbo/utils'
import { deepMerge, difference } from '@pengzhanbo/utils'
import { watch } from 'chokidar'
import { colors } from 'vuepress/utils'
import { initThemeOptions } from '../config/index.js'
@ -50,11 +50,15 @@ export class ConfigLoader extends EventEmitter {
const watcher = watch([this.configFile, ...this.dependencies], {
ignoreInitial: true,
cwd,
ignored: (filepath, stats) => {
const isFile = Boolean(stats?.isFile())
return isFile && filepath.includes('node_modules')
},
})
watcher.on('change', async (filepath) => {
const dependencies = await this.load()
watcher.add(dependencies.filter(dep => !this.dependencies.includes(dep)))
watcher.add(difference(dependencies, this.dependencies))
this.dependencies = [...dependencies]
this.emit('change', this.config)

View File

@ -5,7 +5,7 @@ import process from 'node:process'
import { pathToFileURL } from 'node:url'
import { build } from 'esbuild'
import { importFileDefault } from 'vuepress/utils'
import { hash } from '../utils/index.js'
import { hash, normalizePath } from '../utils/index.js'
export async function compiler(configPath?: string,
): Promise<{
@ -88,6 +88,8 @@ export async function compiler(configPath?: string,
return {
config,
// local deps
dependencies: Object.keys(result.metafile?.inputs ?? {}).filter(dep => dep[0] === '.'),
dependencies: Object.keys(result.metafile?.inputs ?? {})
.filter(dep => dep[0] === '.')
.map(normalizePath),
}
}