diff --git a/plugins/plugin-search/src/node/searchPlugin.ts b/plugins/plugin-search/src/node/searchPlugin.ts index 24684796..1c9c71ba 100644 --- a/plugins/plugin-search/src/node/searchPlugin.ts +++ b/plugins/plugin-search/src/node/searchPlugin.ts @@ -1,7 +1,6 @@ import type { Plugin } from 'vuepress/core' import type { SearchPluginOptions } from '../shared/index.js' import { addViteOptimizeDepsInclude, getFullLocaleConfig } from '@vuepress/helper' -import chokidar from 'chokidar' import { getDirname, path } from 'vuepress/utils' import { SEARCH_LOCALES } from './locales/index.js' import { onSearchIndexRemoved, onSearchIndexUpdated, prepareSearchIndex, prepareSearchIndexPlaceholder } from './prepareSearchIndex.js' @@ -72,22 +71,34 @@ export function searchPlugin({ } }, - 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) + onPageUpdated: (app, type, page) => { + if (!page?.filePathRelative) + return + + if (type === 'create' || type === 'update') { + onSearchIndexUpdated(page?.filePathRelative, { app, isSearchable, searchOptions }) + } + else if (type === 'delete') { + onSearchIndexRemoved(page?.filePathRelative, { app, isSearchable, searchOptions }) + } }, + + // 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) + // }, }) } diff --git a/theme/src/node/prepare/index.ts b/theme/src/node/prepare/index.ts index e55a2108..1a0cffc8 100644 --- a/theme/src/node/prepare/index.ts +++ b/theme/src/node/prepare/index.ts @@ -1,5 +1,4 @@ import type { App } from 'vuepress' -import { watch } from 'chokidar' import { perf } from '../utils/index.js' import { prepareArticleTagColors } from './prepareArticleTagColor.js' import { prepareCollections } from './prepareCollections.js' @@ -29,24 +28,3 @@ export async function prepareData(app: App): Promise { 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)) -}