From 48970dd559f3d09e54382cf5e6051cddfb25ebc5 Mon Sep 17 00:00:00 2001 From: pengzhanbo Date: Mon, 19 Jan 2026 21:40:23 +0800 Subject: [PATCH] fix(theme): fix theme config hmr fail (#820) --- theme/src/node/loadConfig/ConfigLoader.ts | 8 ++++++-- theme/src/node/loadConfig/compiler.ts | 6 ++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/theme/src/node/loadConfig/ConfigLoader.ts b/theme/src/node/loadConfig/ConfigLoader.ts index ca8e4e7f..afc763f2 100644 --- a/theme/src/node/loadConfig/ConfigLoader.ts +++ b/theme/src/node/loadConfig/ConfigLoader.ts @@ -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) diff --git a/theme/src/node/loadConfig/compiler.ts b/theme/src/node/loadConfig/compiler.ts index 6d16a65f..80b52f77 100644 --- a/theme/src/node/loadConfig/compiler.ts +++ b/theme/src/node/loadConfig/compiler.ts @@ -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), } }