Skip to content

Commit 36ac193

Browse files
committed
fix(rehype-shiki): only import every grammar without a highlighter
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>
1 parent bbbdde7 commit 36ac193

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@node-core/rehype-shiki': patch
3+
---
4+
5+
Only import every bundled grammar when the plugin isn't given a highlighter

‎packages/rehype-shiki/src/plugin.mjs‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import classNames from 'classnames';
44
import { toString } from 'hast-util-to-string';
55
import { SKIP, visit } from 'unist-util-visit';
66

7-
import createHighlighter from '#rs/index.mjs';
8-
97
// This is what Remark will use as prefix within a <pre> className
108
// to attribute the current language of the <pre> element
119
const languagePrefix = 'language-';
@@ -57,8 +55,15 @@ function isCodeBlock(node) {
5755
* @param {import('#rs/index.mjs').HighlighterOptions & { highlighter: import('#rs/highlighter.mjs').SyntaxHighlighter }} options
5856
*/
5957
export default async function rehypeShikiji(options) {
60-
const highlighter =
61-
options?.highlighter ?? (await createHighlighter(options));
58+
let highlighter = options?.highlighter;
59+
60+
// Loaded only when no highlighter is given, as it imports every grammar
61+
// Shiki bundles as soon as it's loaded
62+
if (!highlighter) {
63+
const { default: createHighlighter } = await import('#rs/index.mjs');
64+
65+
highlighter = await createHighlighter(options);
66+
}
6267

6368
return function (tree) {
6469
visit(tree, 'element', (_, index, parent) => {

0 commit comments

Comments
 (0)