Skip to content

perf(js): externalize deps and split entry points for tree shaking#550

Open
adambrudzynski wants to merge 1 commit into
storyblok:mainfrom
adambrudzynski:perf/js-externalize-deps-tree-shaking
Open

perf(js): externalize deps and split entry points for tree shaking#550
adambrudzynski wants to merge 1 commit into
storyblok:mainfrom
adambrudzynski:perf/js-externalize-deps-tree-shaking

Conversation

@adambrudzynski

Copy link
Copy Markdown

Summary

Externalizes @storyblok/richtext and storyblok-js-client from the @storyblok/js bundle and splits the package into individual entry points — enabling proper tree shaking and code splitting for downstream bundlers.

Problem

@storyblok/js was shipping as a single monolithic bundle (storyblok-js.mjs) with all dependencies inlined. This meant:

  • storyblok-js-client and @storyblok/richtext were bundled twice — once inside @storyblok/js and again as standalone packages
  • Importing any single export (e.g. storyblokEditable) pulled the entire bundle into the consumer's app
  • No tree shaking was possible for downstream bundlers (Next.js, Vite, etc.)

Solution

Mirrors the pattern already established in @storyblok/react, which externalizes react, react-dom, and @storyblok/js itself.

vite.config.ts changes:

  • @storyblok/richtext and storyblok-js-client marked as external
  • preserveModules: true — each source file gets its own output file
  • Multiple named entry points: index, api, bridge, editable

package.json changes:

  • exports map updated with sub-path exports for each entry point
  • main/module updated from storyblok-js.jsindex.js/index.mjs

Before / After

Before After
index.mjs large inlined bundle 2.18 kB
api.mjs inlined 0.39 kB
bridge.mjs inlined 0.81 kB
editable.mjs inlined 0.37 kB

Bundle analysis — Next.js application

Before
Huge bundle of @storyblok/js when using only @storyblok/react and @storyblok/richtext packages

image

After

image

Test plan

  • pnpm build — clean output with externalized imports verified
  • pnpm test:unit — 19/19 passing
  • pnpm run test:types — no type errors
  • pnpm test:e2e — 4/4 passing
  • Tested in real Next.js app via yalc — build succeeds, externals confirmed as import statements in output

- Externalize @storyblok/richtext and storyblok-js-client so they are
  not inlined into the bundle, allowing downstream bundlers to tree shake
- Add preserveModules and multiple named entry points (index, api, bridge,
  editable) mirroring the pattern used in @storyblok/react
- Update package.json exports map with sub-path exports for each entry

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

1 participant