Skip to content

Bundle size regression: @copilotkitnext/react barrel file pulls in heavy transitive deps (mermaid, shiki, streamdown) #3653

@gsig123

Description

@gsig123

Problem

Upgrading @copilotkit/react-core + @copilotkit/react-ui from v1.10.x to v1.54.x causes a +21.95 MB bundle size increase due to heavy transitive dependencies pulled in through @copilotkitnext/react.

This is a follow-up to #3225, which was closed after PR #3310 fixed the @copilotkit/runtime leak from runtime-client-gql. However, a separate and larger bundle size problem remains: the @copilotkitnext/react barrel file.

Root Cause

@copilotkitnext/react/dist/index.mjs statically imports all components at the top level:

import CopilotChat from "./components/chat/CopilotChat.mjs";
import CopilotChatAssistantMessage from "./components/chat/CopilotChatAssistantMessage.mjs";
// ... every component

The chat components import heavy dependencies:

  • CopilotChatAssistantMessage.mjsstreamdownmermaid (~3 MB minified), shiki (~1 MB)
  • Multiple components → lucide-react (~530 KB minified)
  • streamdownkatex, cytoscape, langium

Since @copilotkitnext/react has:

  1. No sideEffects field in package.json — bundlers assume all imports have side effects
  2. No subpath exports — consumers can't import just hooks without triggering the barrel
  3. Barrel file with static imports — even unused components are pulled into the module graph

Any consumer importing from @copilotkit/react-core (which re-exports hooks from @copilotkitnext/react) gets the entire dependency tree.

Impact

Metric v1.10.6 v1.54.1 Delta
Top-level packages in node_modules 178 321 +143 (+80%)
node_modules disk size 111 MB 416 MB +305 MB
Bundle size (Codecov) baseline +21.95 MB +12.16%

The build also OOMs with 8 GB heap due to the bundler trying to process mermaid's massive module graph.

Proposed Fixes

Immediate: Add sideEffects declaration (PR #3652)

Adding "sideEffects": ["**/*.css"] to @copilotkitnext/react's package.json allows bundlers to tree-shake unused components. This completely eliminates mermaid/shiki/streamdown for hook-only consumers.

Follow-up suggestions

  1. Subpath exports — e.g., @copilotkitnext/react/hooks so consumers can bypass the barrel entirely
  2. Make heavy renderers opt-in — mermaid, shiki, and katex could be peer/optional dependencies or loaded via a plugin pattern, so consumers who don't need diagram/code rendering don't pay the cost
  3. Add sideEffects: false to streamdown — this package also lacks the field, preventing tree-shaking even when the consumer package is properly configured

Reproduction

mkdir test && cd test
echo '{"dependencies":{"@copilotkit/react-core":"1.54.1","@copilotkit/react-ui":"1.54.1"}}' > package.json
npm install
du -sh node_modules  # 416 MB

Environment

  • Bundler: rspack 1.3.x (also affects webpack 5)
  • CopilotKit version: 1.54.1
  • Previous working version: 1.10.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions