Repository navigation
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #9212 +/- ##
==========================================
+ Coverage 86.07% 86.15% +0.08%
==========================================
Files 86 86
Lines 6060 6075 +15
Branches 359 360 +1
==========================================
+ Hits 5216 5234 +18
+ Misses 840 837 -3
Partials 4 4 ☔ View full report in Codecov by Harness. |
There was a problem hiding this comment.
🟡 Changes recommended
The supplied-highlighter path needs an automated regression test proving bundled grammars remain unloaded.
1 open finding
What changed in this PR
Defers loading bundled Shiki grammars when callers provide their own highlighter, reducing import overhead for consumers such as nodejs/doc-kit.
Changes:
- Dynamically imports the default highlighter only when needed.
- Adds a patch changeset.
| File | Description |
|---|---|
packages/rehype-shiki/src/plugin.mjs |
Lazily loads the grammar-heavy default highlighter. |
.changeset/lazy-rehype-shiki-highlighter.md |
Records the patch release. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
The Shiki plugin registered every bundled language (~250 grammars) in each thread that highlighted code, which cost ~2s and ~100MB per thread and made every highlight several times slower, as each code block was matched against grammars it never uses. Importing it also imported all of them, through `@node-core/rehype-shiki`'s `LANGS` and its plugin, on every thread loading `jsx-ast`, the main thread included. The highlighter now registers a bundled language the first time code in it is highlighted, along with the bundled languages a configured one embeds, and lists the bundled ones from their metadata alone, without importing `LANGS`. The themes are given to Shiki by name, which it keeps parsed instead of parsing them for every highlight. Importing `@node-core/rehype-shiki`'s plugin still imports every grammar until nodejs/nodejs.org#9212 is released. The grammars now come from doc-kit's own `shiki` dependency (4.4.3) rather than the copy `@node-core/rehype-shiki` pins (4.3.1). Its C++ grammar highlights types and template arguments differently, which shows on the Node.js docs' C++ examples. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
📦 Build Size ComparisonSummary
|
The Shiki plugin registered every bundled language (~250 grammars) in each thread that highlighted code, which cost ~2s and ~100MB per thread and made every highlight several times slower, as each code block was matched against grammars it never uses. Importing it also imported all of them, through `@node-core/rehype-shiki`'s `LANGS` and its plugin, on every thread loading `jsx-ast`, the main thread included. The highlighter now registers a bundled language the first time code in it is highlighted, along with the bundled languages a configured one embeds, and lists the bundled ones from their metadata alone, without importing `LANGS`. The themes are given to Shiki by name, which it keeps parsed instead of parsing them for every highlight. Importing `@node-core/rehype-shiki`'s plugin still imports every grammar until nodejs/nodejs.org#9212 is released. The grammars now come from doc-kit's own `shiki` dependency (4.4.3) rather than the copy `@node-core/rehype-shiki` pins (4.3.1). Its C++ grammar highlights types and template arguments differently, which shows on the Node.js docs' C++ examples. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
The plugin imported `index.mjs` statically, and that module imports every grammar Shiki bundles (`LANGS`) as soon as it's loaded, so a caller passing its own highlighter still paid for all of them: 260 grammar modules on each thread that loads the plugin. It's now imported only when the plugin has to create a highlighter itself. This lets doc-kit, which highlights with a highlighter that loads grammars on demand, use the plugin rather than a copy of it. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
36ac193 to
e1c50ce
Compare

Description
This PR makes
@node-core/rehype-shiki's plugin importindex.mjsonly when it has to create a highlighter itself.plugin.mjsimportedindex.mjsstatically, and that module imports every grammar Shiki bundles (LANGS) as soon as it's loaded. So a caller passing its ownhighlighterstill paid for all of them: importing the plugin loaded 260 grammar modules on every thread that used it. It's now a dynamic import, inside the branch that creates the default highlighter, and a patch changeset releases it.Validation
lint:jsand Prettier.plugin.mjsloads 260 grammar modules before this change and none after it, counted with a moduleloadhook.Related Issues
Refs: nodejs/doc-kit#1156, which highlights with its own highlighter that loads grammars on demand, and can then use this plugin instead of a copy of it.
Check List
pnpm formatto ensure the code follows the style guide.pnpm testto check if all tests are passing.pnpm buildto check if the website builds without errors.