fix(react-email): ship per-module output so bundlers can tree-shake unused components
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:
- Per-module build output +
sideEffects: false— the dist now mirrors the source tree (tsdownunbundlemode) 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 theexportsmap, so the dist layout is not observable. - css-tree without the bundling hack — the ESM output now resolves
css-treeto its self-containeddist/csstree.esmbundle (nocreateRequireJSON loading, safe for consumer bundlers), replacing the build-time JSON-inlining plugin. The CJS output keeps plaincss-tree, whose CJS build only uses staticrequire()calls. - Decoupled
is-componentfrom 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 everyTailwinduser's bundle. Components now carry a shared symbol marker instead.
Measured with esbuild bundle tracing (react/react-dom external), before → after:
| import pattern | before | after |
|---|---|---|
import { Text } | 1,510 KB | 2.6 KB |
| 14 layout components | 1,511 KB | 17.7 KB |
import * as RE (namespace) | 1,510 KB | 9.4 KB |
uses Markdown | 1,510 KB | 81 KB (marked only) |
uses Tailwind | 1,912 KB | 694 KB (tailwindcss only) |
uses CodeBlock | 1,513 KB | 727 KB (prismjs only) |
import { render } | 2,090 KB | 575 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
tsdownunbundle; barrel entry; ESM/CJS exports unchanged. - Remap
css-treetocss-tree/dist/csstree.esmin .mjs only; keep barecss-treein type declarations; drop thecreateRequirehack. CJS stays oncss-tree. - Use a shared symbol to mark "element" components; Tailwind no longer imports all components.
- Per-file build with