nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Feature] Lazy-load `cli-highlight` / `highlight.js` (193 modules) until a code block is rendered

Open addyCooks opened this issue 1 month ago • 1 comments

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).
  • --plain chain: 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). Also source/components/file-explorer/index.tsx:2.
  • Blocking cli-highlight/highlight.js/parse5 in 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 registerLanguage among the top self-time functions. Standalone import of cli-highlight took 683 ms (min) on a loaded test host.

Steps to Reproduce

  1. Count loaded modules with a module.registerHooks load hook while importing dist/app/index.js or dist/plain/shell.js.
  2. List the files under node_modules/highlight.js.

Proposed Change

  • Wrap highlight() in a helper that loads cli-highlight on first use. It is CommonJS, so createRequire(import.meta.url)('cli-highlight') keeps the call synchronous, which the render path needs.
  • Alternative (changes behaviour): highlight.js/lib/core with about 20 common languages registered and plain text as the fallback.

Expected Behavior

  • highlight.js and parse5 are absent from the module list for nanocoder --version, the idle TUI and --plain run when no code is shown.
  • Code blocks in assistant messages and the write_file preview 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.ts and the write-file formatter specs. Watch for tests that import the highlighter synchronously.

addyCooks avatar Oct 15 '26 15:10 addyCooks

I'd love to work on this!

Proposed plan:

  1. Introduce a lazy highlighter helper (wrapping createRequire(import.meta.url)('cli-highlight')) so cli-highlight, highlight.js (193 modules), and parse5 are not imported synchronously at startup or during --plain/no-code sessions.
  2. Update source/markdown-parser/index.ts, source/tools/file-ops/write-file.tsx, and source/components/file-explorer/index.tsx to use the lazy helper on first code-block / file preview render.
  3. Add unit tests confirming lazy initialization and syntax highlighting correctness.

Could you please assign this to me? Thanks!

dhruvil-codes avatar Oct 16 '26 08:10 dhruvil-codes