Skip to content

fix: decouple core from lowlight to reduce bundle size#65

Open
voglster wants to merge 2 commits intoMrWangJustToDo:mainfrom
voglster:fix/bundle-size-lowlight
Open

fix: decouple core from lowlight to reduce bundle size#65
voglster wants to merge 2 commits intoMrWangJustToDo:mainfrom
voglster:fix/bundle-size-lowlight

Conversation

@voglster
Copy link

Summary

  • Removes the hard dependency of @git-diff-view/core on @git-diff-view/lowlight, eliminating ~870KB of unused highlight.js language grammars from the bundle when users provide their own highlighter
  • Adds setDefaultHighlighter() API to core for injectable highlighter registration
  • @git-diff-view/lowlight auto-registers itself as the default when imported

Changes

Package Change
core/src/file.ts Replace import { highlighter } with injectable _defaultHighlighter via setDefaultHighlighter()
core/src/index.ts Replace export * from "@git-diff-view/lowlight" with type-only re-exports
core/package.json Move @git-diff-view/lowlight from dependencies to optional peerDependencies
lowlight/src/index.ts Auto-call setDefaultHighlighter(highlighter) on import
lowlight/package.json Add @git-diff-view/core as peer dependency
React, Vue, Svelte, Solid Remove unused buildInHighlighter imports

Breaking Change

highlighter and processAST are no longer re-exported from @git-diff-view/core. Users should import them from @git-diff-view/lowlight directly.

How it works

  1. @git-diff-view/core no longer imports any value from @git-diff-view/lowlight
  2. When @git-diff-view/lowlight is imported (e.g., by framework packages or user code), it calls setDefaultHighlighter() to register itself with core
  3. Users who provide a custom registerHighlighter prop can skip installing @git-diff-view/lowlight entirely, and the highlight.js bundle is never included

Fixes #58

Syntax highlighting is lost when DiffView is unmounted and remounted
with the same file content. The global File cache causes initRaw() →
#syncSyntax() to copy highlighter metadata from cached File objects
into a fresh DiffFile, making the syntax effect's guard condition
evaluate to false and skip initSyntax() entirely.

initSyntax() is already idempotent — when syntax is initialized with
a matching highlighter, it efficiently re-syncs syntax line references
without recomputing. Remove the guard condition and always call
initSyntax() when highlighting is enabled.

Fixes MrWangJustToDo#63
Remove the hard dependency on @git-diff-view/lowlight from core,
which was causing all 130+ highlight.js language grammars (~870KB
minified) to be bundled even when users provide their own highlighter.

Changes:
- core/file.ts: Replace direct highlighter import with injectable
  default via setDefaultHighlighter()
- core/index.ts: Replace `export * from "@git-diff-view/lowlight"`
  with type-only re-exports
- lowlight/index.ts: Auto-register as the default highlighter when
  imported via setDefaultHighlighter()
- core/package.json: Move @git-diff-view/lowlight from dependencies
  to optional peerDependencies
- Framework packages: Remove unused buildInHighlighter imports

Users who provide a custom registerHighlighter can now avoid
bundling lowlight entirely. Users who rely on the built-in lowlight
highlighter just need to ensure @git-diff-view/lowlight is installed
(which it is by default via the framework packages).

BREAKING CHANGE: `highlighter` and `processAST` are no longer
re-exported from @git-diff-view/core. Import them from
@git-diff-view/lowlight instead.

Fixes MrWangJustToDo#58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@git-diff-view/lowlight eagerly imports all highlight.js languages, adding ~870 KB to the bundle

1 participant