mirror of
https://github.com/pengzhanbo/vuepress-theme-plume.git
synced 2026-04-23 10:58:13 +08:00
perf(theme): 优化主题配置文件监听流
This commit is contained in:
parent
332d284415
commit
8438cd546b
@ -27,7 +27,6 @@ export interface Loader {
|
||||
dependencies: string[]
|
||||
load: () => Promise<{ config: ThemeConfig, dependencies: string[] }>
|
||||
loaded: boolean
|
||||
watcher: FSWatcher | null
|
||||
changeEvents: ChangeEvent[]
|
||||
whenLoaded: ChangeEvent[]
|
||||
defaultConfig: ThemeConfig
|
||||
@ -47,7 +46,6 @@ export async function initConfigLoader(
|
||||
dependencies: [],
|
||||
load: () => compiler(loader!.configFile),
|
||||
loaded: false,
|
||||
watcher: null,
|
||||
changeEvents: [],
|
||||
whenLoaded: [],
|
||||
defaultConfig,
|
||||
@ -64,7 +62,7 @@ export async function initConfigLoader(
|
||||
|
||||
const { config, dependencies = [] } = await loader.load()
|
||||
loader.loaded = true
|
||||
addDependencies(dependencies)
|
||||
loader.dependencies = [...dependencies]
|
||||
updateResolvedConfig(app, config)
|
||||
runChangeEvents()
|
||||
|
||||
@ -81,14 +79,14 @@ export function watchConfigFile(app: App, watchers: any[]) {
|
||||
cwd: path.join(path.dirname(loader.configFile), '../'),
|
||||
})
|
||||
|
||||
addDependencies()
|
||||
addDependencies(watcher)
|
||||
|
||||
watcher.on('change', async () => {
|
||||
if (loader) {
|
||||
loader.loaded = false
|
||||
const { config, dependencies = [] } = await loader.load()
|
||||
loader.loaded = true
|
||||
addDependencies(dependencies)
|
||||
addDependencies(watcher, dependencies)
|
||||
updateResolvedConfig(app, config)
|
||||
runChangeEvents()
|
||||
}
|
||||
@ -99,8 +97,6 @@ export function watchConfigFile(app: App, watchers: any[]) {
|
||||
runChangeEvents()
|
||||
})
|
||||
|
||||
loader.watcher = watcher
|
||||
|
||||
watchers.push(watcher)
|
||||
}
|
||||
|
||||
@ -147,7 +143,7 @@ function runChangeEvents() {
|
||||
}
|
||||
}
|
||||
|
||||
function addDependencies(dependencies?: string[]) {
|
||||
function addDependencies(watcher: FSWatcher, dependencies?: string[]) {
|
||||
if (!loader)
|
||||
return
|
||||
|
||||
@ -155,9 +151,9 @@ function addDependencies(dependencies?: string[]) {
|
||||
const deps = dependencies
|
||||
.filter(dep => !loader!.dependencies.includes(dep) && dep[0] === '.')
|
||||
loader.dependencies.push(...deps)
|
||||
deps.length && loader.watcher?.add(deps)
|
||||
watcher.add(deps)
|
||||
}
|
||||
else {
|
||||
loader.watcher?.add(loader.dependencies)
|
||||
watcher.add(loader.dependencies)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user