refactor: migrate onWatched to onPageUpdated

This commit is contained in:
pengzhanbo 2026-04-02 21:14:16 +08:00
parent a6cb3820b1
commit 86cb872ce6
2 changed files with 28 additions and 39 deletions

View File

@ -1,7 +1,6 @@
import type { Plugin } from 'vuepress/core' import type { Plugin } from 'vuepress/core'
import type { SearchPluginOptions } from '../shared/index.js' import type { SearchPluginOptions } from '../shared/index.js'
import { addViteOptimizeDepsInclude, getFullLocaleConfig } from '@vuepress/helper' import { addViteOptimizeDepsInclude, getFullLocaleConfig } from '@vuepress/helper'
import chokidar from 'chokidar'
import { getDirname, path } from 'vuepress/utils' import { getDirname, path } from 'vuepress/utils'
import { SEARCH_LOCALES } from './locales/index.js' import { SEARCH_LOCALES } from './locales/index.js'
import { onSearchIndexRemoved, onSearchIndexUpdated, prepareSearchIndex, prepareSearchIndexPlaceholder } from './prepareSearchIndex.js' import { onSearchIndexRemoved, onSearchIndexUpdated, prepareSearchIndex, prepareSearchIndexPlaceholder } from './prepareSearchIndex.js'
@ -72,22 +71,34 @@ export function searchPlugin({
} }
}, },
onWatched: (app, watchers) => { onPageUpdated: (app, type, page) => {
const searchIndexWatcher = chokidar.watch('pages', { if (!page?.filePathRelative)
cwd: app.dir.temp(), return
ignoreInitial: true,
ignored: (filepath, stats) => Boolean(stats?.isFile()) && !filepath.endsWith('.js'), if (type === 'create' || type === 'update') {
}) onSearchIndexUpdated(page?.filePathRelative, { app, isSearchable, searchOptions })
searchIndexWatcher.on('add', (filepath) => { }
onSearchIndexUpdated(filepath, { app, isSearchable, searchOptions }) else if (type === 'delete') {
}) onSearchIndexRemoved(page?.filePathRelative, { app, isSearchable, searchOptions })
searchIndexWatcher.on('change', (filepath) => { }
onSearchIndexUpdated(filepath, { app, isSearchable, searchOptions })
})
searchIndexWatcher.on('unlink', (filepath) => {
onSearchIndexRemoved(filepath, { app, isSearchable, searchOptions })
})
watchers.push(searchIndexWatcher)
}, },
// onWatched: (app, watchers) => {
// const searchIndexWatcher = chokidar.watch('pages', {
// cwd: app.dir.temp(),
// ignoreInitial: true,
// ignored: (filepath, stats) => Boolean(stats?.isFile()) && !filepath.endsWith('.js'),
// })
// searchIndexWatcher.on('add', (filepath) => {
// onSearchIndexUpdated(filepath, { app, isSearchable, searchOptions })
// })
// searchIndexWatcher.on('change', (filepath) => {
// onSearchIndexUpdated(filepath, { app, isSearchable, searchOptions })
// })
// searchIndexWatcher.on('unlink', (filepath) => {
// onSearchIndexRemoved(filepath, { app, isSearchable, searchOptions })
// })
// watchers.push(searchIndexWatcher)
// },
}) })
} }

View File

@ -1,5 +1,4 @@
import type { App } from 'vuepress' import type { App } from 'vuepress'
import { watch } from 'chokidar'
import { perf } from '../utils/index.js' import { perf } from '../utils/index.js'
import { prepareArticleTagColors } from './prepareArticleTagColor.js' import { prepareArticleTagColors } from './prepareArticleTagColor.js'
import { prepareCollections } from './prepareCollections.js' import { prepareCollections } from './prepareCollections.js'
@ -29,24 +28,3 @@ export async function prepareData(app: App): Promise<void> {
perf.log('prepare:data') perf.log('prepare:data')
} }
/**
* Watch for changes in prepared data and re-prepare when needed
*
*
*/
export function watchPrepare(
app: App,
watchers: any[],
): void {
const pagesWatcher = watch('pages', {
cwd: app.dir.temp(),
ignoreInitial: true,
ignored: (filepath, stats) => Boolean(stats?.isFile()) && !filepath.endsWith('.js'),
})
watchers.push(pagesWatcher)
pagesWatcher.on('change', () => prepareData(app))
pagesWatcher.on('add', () => prepareData(app))
pagesWatcher.on('unlink', () => prepareData(app))
}