diff --git a/src/node/plugins/localSearchPlugin.ts b/src/node/plugins/localSearchPlugin.ts index 536a3222..0af42dac 100644 --- a/src/node/plugins/localSearchPlugin.ts +++ b/src/node/plugins/localSearchPlugin.ts @@ -2,7 +2,6 @@ import { createDebug } from 'obug' import fs from 'fs-extra' import MiniSearch from 'minisearch' import path from 'node:path' -import pMap from 'p-map' import type { Plugin, ViteDevServer } from 'vite' import type { SiteConfig } from '../config' import type { DefaultTheme } from '../defaultTheme' @@ -81,27 +80,27 @@ export async function localSearchPlugin( } let server: ViteDevServer | undefined + let pending: Promise function onIndexUpdated() { - if (server) { - server.moduleGraph.onFileChange(LOCAL_SEARCH_INDEX_REQUEST_PATH) - // HMR - const mod = server.moduleGraph.getModuleById( - LOCAL_SEARCH_INDEX_REQUEST_PATH - ) - if (!mod) return - server.ws.send({ - type: 'update', - updates: [ - { - acceptedPath: mod.url, - path: mod.url, - timestamp: Date.now(), - type: 'js-update' - } - ] - }) - } + if (!server) return + server.moduleGraph.onFileChange(LOCAL_SEARCH_INDEX_REQUEST_PATH) + // HMR + const mod = server.moduleGraph.getModuleById( + LOCAL_SEARCH_INDEX_REQUEST_PATH + ) + if (!mod) return + server.ws.send({ + type: 'update', + updates: [ + { + acceptedPath: mod.url, + path: mod.url, + timestamp: Date.now(), + type: 'js-update' + } + ] + }) } function getDocId(file: string) { @@ -142,9 +141,9 @@ export async function localSearchPlugin( async function scanForBuild() { debug('🔍️ Indexing files for search...') - await pMap(siteConfig.pages, indexFile, { - concurrency: siteConfig.buildConcurrency - }) + for (const page of siteConfig.pages) { + await indexFile(page) + } debug('✅ Indexing finished...') } @@ -161,10 +160,9 @@ export async function localSearchPlugin( } }), - async configureServer(_server) { + configureServer(_server) { server = _server - await scanForBuild() - onIndexUpdated() + pending = scanForBuild().then(onIndexUpdated) }, resolveId(id) { @@ -175,6 +173,7 @@ export async function localSearchPlugin( async load(id) { if (id === LOCAL_SEARCH_INDEX_REQUEST_PATH) { + await pending if (process.env.NODE_ENV === 'production') { await scanForBuild() } @@ -188,6 +187,7 @@ export async function localSearchPlugin( } return `export default {${records.join(',')}}` } else if (id.startsWith(LOCAL_SEARCH_INDEX_REQUEST_PATH)) { + await pending return `export default ${JSON.stringify( JSON.stringify( indexByLocales.get(