|
17 | 17 | * getSyntaxTheme always returns the default for the given Claude theme. |
18 | 18 | */ |
19 | 19 |
|
20 | | -import { createRequire } from 'node:module' |
21 | 20 | import { diffArrays } from 'diff' |
22 | | -import type * as hljsNamespace from 'highlight.js' |
| 21 | +import hljs from 'highlight.js' |
23 | 22 | import { basename, extname } from 'path' |
24 | 23 |
|
25 | | -// createRequire works in both Bun and Node.js ESM contexts. |
26 | | -// Needed because this package is "type": "module" but uses require() for |
27 | | -// lazy loading — bare require is not available in Node.js ESM. |
28 | | -const nodeRequire = createRequire(import.meta.url) |
29 | | - |
30 | | -// Lazy: defers loading highlight.js until first render. The full bundle |
31 | | -// registers 190+ language grammars at require time (~50MB, 100-200ms on |
32 | | -// macOS, several× that on Windows). With a top-level import, any caller |
33 | | -// chunk that reaches this module — including test/preload.ts via |
34 | | -// StructuredDiff.tsx → colorDiff.ts — pays that cost at module-eval time |
35 | | -// and carries the heap for the rest of the process. On Windows CI this |
36 | | -// pushed later tests in the same shard into GC-pause territory and a |
37 | | -// beforeEach/afterEach hook timeout (officialRegistry.test.ts, PR #24150). |
38 | | -// Same lazy pattern the NAPI wrapper used for dlopen. |
39 | | -type HLJSApi = typeof hljsNamespace.default |
| 24 | +// Static import — createRequire(import.meta.url) fails in Bun --compile mode |
| 25 | +// because the resolved path points to the internal bunfs binary path where |
| 26 | +// node_modules cannot be found. A top-level import ensures the module is |
| 27 | +// bundled and accessible at runtime. |
| 28 | +type HLJSApi = typeof hljs |
40 | 29 | let cachedHljs: HLJSApi | null = null |
41 | | -function hljs(): HLJSApi { |
| 30 | +function hljsApi(): HLJSApi { |
42 | 31 | if (cachedHljs) return cachedHljs |
43 | | - const mod = nodeRequire('highlight.js') |
44 | 32 | // highlight.js uses `export =` (CJS). Under bun/ESM the interop wraps it |
45 | 33 | // in .default; under node CJS the module IS the API. Check at runtime. |
| 34 | + const mod = hljs as HLJSApi & { default?: HLJSApi } |
46 | 35 | cachedHljs = 'default' in mod && mod.default ? mod.default : mod |
47 | 36 | return cachedHljs! |
48 | 37 | } |
@@ -441,9 +430,9 @@ function detectLanguage( |
441 | 430 | // Filename-based lookup (handles Dockerfile, Makefile, CMakeLists.txt, etc.) |
442 | 431 | const stem = base.split('.')[0] ?? '' |
443 | 432 | const byName = FILENAME_LANGS[base] ?? FILENAME_LANGS[stem] |
444 | | - if (byName && hljs().getLanguage(byName)) return byName |
| 433 | + if (byName && hljsApi().getLanguage(byName)) return byName |
445 | 434 | if (ext) { |
446 | | - const lang = hljs().getLanguage(ext) |
| 435 | + const lang = hljsApi().getLanguage(ext) |
447 | 436 | if (lang) return ext |
448 | 437 | } |
449 | 438 | // Shebang / first-line detection (strip UTF-8 BOM) |
@@ -525,7 +514,7 @@ function highlightLine( |
525 | 514 | } |
526 | 515 | let result |
527 | 516 | try { |
528 | | - result = hljs().highlight(code, { |
| 517 | + result = hljsApi().highlight(code, { |
529 | 518 | language: state.lang, |
530 | 519 | ignoreIllegals: true, |
531 | 520 | }) |
|
0 commit comments