nanocoder
nanocoder copied to clipboard
[Feature] Lazy-load `cli-highlight` / `highlight.js` (193 modules) until a code block is rendered
Description
highlight.js registers every language at import: 193 modules and about 1.35 MB of source, plus parse5 (26 modules) for cli-highlight. The markdown parser and the write_file formatter import it at startup, so --plain runs and sessions that never show a code block still pay for it.
Environment
- OS: Windows 11 (10.0.26300)
- Node version: 22.22.3
- Nanocoder version: 1.31.0 (main at 5c2e6935)
Evidence
- TUI chain:
App.js -> components/assistant-reasoning.js -> markdown-parser/index.js -> cli-highlight -> highlight.js(source/markdown-parser/index.ts:2). --plainchain:plain/shell.js -> plain/initialize.js -> tools/tool-manager.js -> tools/index.js -> tools/file-ops/write-file.js -> cli-highlight(source/tools/file-ops/write-file.tsx:4). Alsosource/components/file-explorer/index.tsx:2.- Blocking
cli-highlight/highlight.js/parse5in the import graph removes 226 of 1,459 modules in the TUI and 227 of 981 (23%) in--plain. - A CPU profile of the app import shows
highlight.js registerLanguageamong the top self-time functions. Standalone import ofcli-highlighttook 683 ms (min) on a loaded test host.
Steps to Reproduce
- Count loaded modules with a
module.registerHooksloadhook while importingdist/app/index.jsordist/plain/shell.js. - List the files under
node_modules/highlight.js.
Proposed Change
- Wrap
highlight()in a helper that loadscli-highlighton first use. It is CommonJS, socreateRequire(import.meta.url)('cli-highlight')keeps the call synchronous, which the render path needs. - Alternative (changes behaviour):
highlight.js/lib/corewith about 20 common languages registered and plain text as the fallback.
Expected Behavior
highlight.jsandparse5are absent from the module list fornanocoder --version, the idle TUI and--plain runwhen no code is shown.- Code blocks in assistant messages and the
write_filepreview look exactly as before.
Additional Context
- Effort: M. Start with
source/markdown-parser/index.ts,source/tools/file-ops/write-file.tsx,source/components/file-explorer/index.tsx. - Specs:
source/markdown-parser/*.spec.tsand thewrite-fileformatter specs. Watch for tests that import the highlighter synchronously.
I'd love to work on this!
Proposed plan:
- Introduce a lazy highlighter helper (wrapping
createRequire(import.meta.url)('cli-highlight')) socli-highlight,highlight.js(193 modules), andparse5are not imported synchronously at startup or during--plain/no-code sessions. - Update
source/markdown-parser/index.ts,source/tools/file-ops/write-file.tsx, andsource/components/file-explorer/index.tsxto use the lazy helper on first code-block / file preview render. - Add unit tests confirming lazy initialization and syntax highlighting correctness.
Could you please assign this to me? Thanks!