fix(build): deterministic local search indexing and non-blocking initial scan (closes #4081)

pull/4522/merge
Divyansh Singh 6 months ago
parent cf9994d3c2
commit fc0f203f0a

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

Loading…
Cancel
Save