Skip to content

UI bundle ships two full syntax-highlighting language registries (~2.3 MB) via react-syntax-highlighter's root entry #10633

Closed

Description

@GiladShoham

Summary

highlight.js (1.34 MB, 192 modules — every language) and refractor (0.85 MB, 279 modules — every
Prism language) are bundled eagerly into both the UI browser bundle and the scope SSR bundle. That is
~2.3 MB parsed, in each of two bundles, and it is the largest single remaining item in the shipped
@teambit/ui artifact after #10628 and #10629 took it from 58 MB to 16 MB.

Neither package is imported directly anywhere in the bit repo.

Cause

They arrive through react-syntax-highlighter, whose package root re-exports every build:

// react-syntax-highlighter/dist/esm/index.js
export { default } from './default-highlight';   // highlight.js, all languages, via lowlight
export { default as Prism } from './prism';      // refractor, all Prism languages
export { default as PrismLight } from './prism-light';
export { default as createElement } from './create-element';

Importing anything from the root — even just createElement — pulls both full registries. The
light builds (prism-light, light) exist for this and take explicit registerLanguage calls.

Why it can't be fixed in the bit repo alone

The remaining root imports are in published @teambit components resolved from node_modules,
not in this repo's source:

  • @teambit/api-reference.renderers.schema-node-member-summary → dist/function-node-summary.js
  • @teambit/documenter.ui.code-snippet

scopes/api-reference/renderers/schema-node-member-summary/ contains only a node_modules
directory; there is no source for it here.

Verified: changing the in-repo root imports to deep paths changes the emitted artifact by
0 bytes, because those two keep the root entry in the module graph.

Options

  1. Fix the published components upstream to import react-syntax-highlighter/dist/esm/prism-light
    and register the languages they need. Correct, and the only option with no behaviour cost — but it
    lives outside this repo.
  2. Alias the registries to their language-free cores in scopes/ui-foundation/ui/rspack/rspack.common.ts:
    lowlight → lowlight/lib/core, refractor → refractor/core. Attempted; two problems:
    • bit fails the build with a missing-dependency issue (rspack/rspack.common.ts -> lowlight, refractor),
      since both are transitive and would have to become declared dependencies of @teambit/ui;
    • any consumer relying on the auto-registered languages silently degrades to unhighlighted text.
      That is a product decision.
  3. NormalModuleReplacementPlugin on the root entry — same behaviour trade-off as 2, without the
    dependency declaration.

Option 1 is the right fix if someone owns those components. Option 2/3 is a pragmatic fallback if the
degraded highlighting is acceptable in API-reference summaries and code snippets.

Reproduce

BIT_UI_BUNDLE_STATS=1 bit build "teambit.ui-foundation/ui" --tasks BundleUI --reuse-capsules --unmodified
npm run analyze-bundle bundle-stats/browser.stats.json

(rm -rf the capsule's artifacts/ between measured builds — rspack does not clean its output dir
and --reuse-capsules will otherwise accumulate both runs.)

Context

Full breakdown of this and the other remaining levers — lodash not being tree-shakeable, graphql
shipping whole into the preview bundle, sucrase arriving via react-live, and the single 6.23 MB
eager chunk — is in docs/ui-bundle-size-analysis.md (PR #10632).

Related: #10596.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions