(languages: Language[], wasmBytes?: Record<string, Uint8Array>)
| 402 | * language's bytes are present they're loaded from memory instead of disk. |
| 403 | */ |
| 404 | export async function loadGrammarsForLanguages(languages: Language[], wasmBytes?: Record<string, Uint8Array>): Promise<void> { |
| 405 | if (!parserInitialized) { |
| 406 | await initGrammars(); |
| 407 | } |
| 408 | |
| 409 | languages = expandGrammarLanguages(languages); |
| 410 | |
| 411 | // Deduplicate and filter to languages that have WASM grammars and aren't already loaded |
| 412 | const toLoad = [...new Set(languages)].filter( |
| 413 | (lang): lang is GrammarLanguage => |
| 414 | lang in WASM_GRAMMAR_FILES && |
| 415 | !languageCache.has(lang) && |
| 416 | !unavailableGrammarErrors.has(lang) |
| 417 | ); |
| 418 | |
| 419 | // Load grammars sequentially to avoid web-tree-sitter WASM race condition on Node 20+ |
| 420 | // See: https://github.com/tree-sitter/tree-sitter/issues/2338 |
| 421 | for (const lang of toLoad) { |
| 422 | try { |
| 423 | const bytes = wasmBytes?.[lang]; |
| 424 | const language = await WasmLanguage.load(bytes ?? resolveWasmPath(lang)); |
| 425 | languageCache.set(lang, language); |
| 426 | } catch (error) { |
| 427 | const message = error instanceof Error ? error.message : String(error); |
| 428 | console.warn(`[CodeGraph] Failed to load ${lang} grammar — parsing will be unavailable: ${message}`); |
| 429 | unavailableGrammarErrors.set(lang, message); |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Load ALL grammar WASM files. Convenience function for tests and |
no test coverage detected