perf(catalog): trim highlight.js bundle from ~190 to 35 grammars#4883
Draft
fiskus wants to merge 3 commits into
Draft
perf(catalog): trim highlight.js bundle from ~190 to 35 grammars#4883fiskus wants to merge 3 commits into
fiskus wants to merge 3 commits into
Conversation
Replace the convenience `highlight.js` default import with a single shared module (`utils/hljs`) that uses `highlight.js/lib/core` plus explicit per-language registrations. Runtime API (`hljs.highlight`, `hljs.getLanguage`) is unchanged at every call site. The registered set is bounded by the keys of `Text.js`'s `LANGS` map (the only enumerated runtime consumer); `python` / `bash` used by `Code.tsx` callers are already in that set. Behavioral change: drop `hljs.highlightAuto` fallback in `Markdown.tsx`. With ~35 registered grammars instead of ~190, auto-detection accuracy degrades sharply, so unlabeled / unsupported fences now render as plain monospace via Remarkable's default escaping — same path as the existing `lang === 'none'` short-circuit. `Code.tsx`'s `hl` prop is narrowed from `string` to `RegisteredLanguage`, so passing an unregistered language is a compile error. Tests: `app/utils/hljs.spec.ts` adds a scope tripwire (registered set matches an explicit expected list) and a contract test (every key of `Text.js`'s `LANGS` map resolves via `hljs.getLanguage`). A TODO at the top of `utils/hljs.ts` notes that the 35 grammars still ship in one chunk; per-grammar lazy loading is left as a follow-up because every call site invokes `hljs.highlight` synchronously.
# Conflicts: # catalog/CHANGELOG.md
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #4883 +/- ##
==========================================
+ Coverage 46.55% 46.64% +0.09%
==========================================
Files 832 833 +1
Lines 34131 34162 +31
Branches 5833 5831 -2
==========================================
+ Hits 15890 15936 +46
+ Misses 16239 16227 -12
+ Partials 2002 1999 -3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…tered Catches the failure mode where a developer extends the array (and thus the `RegisteredLanguage` type) but forgets the matching `registerLanguage` call — the type would claim support while `hljs.getLanguage` returns `undefined` at runtime, silently degrading to plain text in Code.tsx. Suggested by Greptile on #4883.
Member
Author
|
Wait for #4884 and then maybe implement async parsing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
highlight.jsdefault import with a sharedapp/utils/hljs.tsthat importshighlight.js/lib/coreand registers a bounded set of 35 languages explicitly. Runtime API (hljs.highlight/hljs.getLanguage) is unchanged at every call site, so 7 files only need the import path swapped.Text.js'sLANGSmap — the only enumerated runtime consumer of hljs in the catalog.python/bashused byCode.tsxcallers are already in that set.Code.tsx'shl: stringprop tohl: RegisteredLanguageso passing an unregistered language is a compile error.Behavioral change
Drop the
hljs.highlightAutofallback inMarkdown.tsx. With ~35 registered grammars instead of ~190, auto-detection accuracy degrades sharply, so unlabeled / unsupported fences now render as plain monospace via Remarkable's default escaping — same path as the existinglang === 'none'short-circuit. A code comment at the deletion site explains the trade-off, and there's a CHANGELOG entry.What's NOT in this PR
The 35 grammars still ship in one chunk — they're statically imported, not lazily loaded. The chunk topology is the same as before; the chunk is just smaller (~250 KB vs the prior full set). A
TODOat the top ofapp/utils/hljs.tsdocuments the path to per-grammar lazy loading and the blocker (Remarkable'shighlightcallback and the preview loaders all callhljs.highlightsynchronously).Tests
app/utils/hljs.spec.tsadds two assertions:REGISTERED_LANGUAGESmust equal an explicit expected list, so any change to the supported set has to surface in code review.Text.js'sLANGSresolves viahljs.getLanguage(...)from the configured module, catching the case where someone adds an extension toLANGSwithout registering its language.Test plan
npm run lintnpm run test:only— all 99 files / 893 tests pass (incl. the 2 new hljs assertions)npm run build— succeeds; verifiedkotlin/haxe/ unregistered grammars no longer appear in build output, whilepython/bash/json/yaml/typescriptare present in the hljs language chunkjson/yaml/python/bashkotlin) — should render as plain monospace.json/.yaml/.pyfileGreptile Summary
Replaces the full
highlight.jsdefault bundle (~190 grammars) with a curatedutils/hljs.tssingleton that registers exactly 35 languages, reducing the hljs chunk size significantly. Seven files are updated only to swap the import path;Markdown.tsxadditionally drops thehighlightAutofallback (intentional and well-documented), andCode.tsx'shlprop is narrowed fromstringto the newRegisteredLanguageunion type.Text.jsLANGS keys resolve viahljs.getLanguage— but there is no assertion that each entry inREGISTERED_LANGUAGESitself is actually registered.lang === 'none'special case removal inMarkdown.tsxis safe:'none'is not registered, sogetLanguage('none')already returnedundefinedand fell through toreturn ''in the old else-branch.Confidence Score: 4/5
Safe to merge; the behavioral change (dropping highlightAuto) is intentional and well-documented, and all existing call sites use languages that are in the registered set.
The implementation is clean and test coverage is good for the primary consumer path (Text.js LANGS). The one gap is that the spec does not assert every entry in REGISTERED_LANGUAGES resolves via hljs.getLanguage, so the exported TypeScript type and the actual runtime registrations can drift silently in future PRs.
catalog/app/utils/hljs.spec.ts — missing assertion that every REGISTERED_LANGUAGES entry is actually registered in the hljs instance.
Important Files Changed
Reviews (1): Last reviewed commit: "Merge remote-tracking branch 'origin/mas..." | Re-trigger Greptile