import lunr from "lunr" ; import "~/polyfills" ; import { Search , SearchIndexConfig } from "../../_" ; import { SearchMessage , SearchMessageType } from "../message" ; see https declare global { function importScripts ( ... urls ) ; } /**
 * Search index
 */ let index ; param config returns async function setupSearchLanguages ( config ) { let base = "../lunr" ; if ( typeof parent !== "undefined" && "IFrameWorker" in parent ) { const worker = document . querySelector < HTMLScriptElement > ( "script[src]" ) ! ; const [ path ] = worker . src . split ( "/worker" ) ; base = base . replace ( ".." , path ) ; } const scripts = [ ] ; for ( const lang of config . lang ) { switch ( lang ) { case "ja" : scripts . push ( `${ base }/tinyseg.js` ) ; break ; case "hi" : case "th" : scripts . push ( `${ base }/wordcut.js` ) ; break ; } if ( lang !== "en" ) scripts . push ( `${ base }/min/lunr.${ lang }.min.js` ) ; } if ( config . lang . length > 1 ) scripts . push ( `${ base }/min/lunr.multi.min.js` ) ; if ( scripts . length ) await importScripts ( `${ base }/min/lunr.stemmer.support.min.js` , ... scripts ) ; } param message returns export async function handler ( message ) { switch ( message . type ) { case SearchMessageType . SETUP : await setupSearchLanguages ( message . data . config ) ; index = new Search ( message . data ) ; return { type : SearchMessageType . READY } ; case SearchMessageType . QUERY : return { type : SearchMessageType . RESULT , data : index ? index . search ( message . data ) : { items : [ ] } } ; default : throw new TypeError ( "Invalid message type" ) ; } } self . lunr = lunr ; addEventListener ( "message" , async ( ev ) => { postMessage ( await handler ( ev . data ) ) ; } ) ;