fix(react-email): ship per-module output so bundlers can tree-shake unused components

Merged
FE
4mo ago
+72-66

Summary

Fixes #3556.

The unified react-email package compiled its entire source into a single flat dist/index.mjs, with eager top-level imports of prismjs, marked, and the tailwindcss compiler. Since tree-shaking is module-granular, importing even one component (import { Text } from 'react-email') pulled all of it into consumer bundles — ~1.5 MB minimum per esbuild trace, reported as ~80 MB serverless functions on Vercel.

Three changes:

  1. Per-module build output + sideEffects: false — the dist now mirrors the source tree (tsdown unbundle mode) and the entry is a pure re-export barrel, so bundlers include only the modules a project actually imports. The public API and the dual ESM/CJS exports are unchanged; deep imports were already blocked by the exports map, so the dist layout is not observable.
  2. css-tree without the bundling hack — the ESM output now resolves css-tree to its self-contained dist/csstree.esm bundle (no createRequire JSON loading, safe for consumer bundlers), replacing the build-time JSON-inlining plugin. The CJS output keeps plain css-tree, whose CJS build only uses static require() calls.
  3. Decoupled is-component from every component — the Tailwind traversal helper imported all 14 "treat as element" components to build an identity list, which alone dragged the 662 KB prism module into every Tailwind user's bundle. Components now carry a shared symbol marker instead.

Measured with esbuild bundle tracing (react/react-dom external), before → after:

import patternbeforeafter
import { Text }1,510 KB2.6 KB
14 layout components1,511 KB17.7 KB
import * as RE (namespace)1,510 KB9.4 KB
uses Markdown1,510 KB81 KB (marked only)
uses Tailwind1,912 KB694 KB (tailwindcss only)
uses CodeBlock1,513 KB727 KB (prismjs only)
import { render }2,090 KB575 KB

Users of Markdown/Tailwind/CodeBlock keep only their component's own heavy dependency; everyone else drops all of them. Rendered HTML is byte-identical before vs after for every pattern above, plus CJS require() (partial, namespace, and Tailwind) and unbundled plain-Node ESM/CJS runtimes. CJS consumed through a bundler still includes the full graph (inherent to require() of a barrel, same as before).


Summary by cubic

Make react-email tree-shakeable by shipping per-module output and setting sideEffects: false. Importing one component no longer drags prismjs, marked, or tailwindcss; import { Text } drops from ~1.5 MB to ~2.6 KB.

  • Refactors
    • Per-file build with tsdown unbundle; barrel entry; ESM/CJS exports unchanged.
    • Remap css-tree to css-tree/dist/csstree.esm in .mjs only; keep bare css-tree in type declarations; drop the createRequire hack. CJS stays on css-tree.
    • Use a shared symbol to mark "element" components; Tailwind no longer imports all components.