fix(react-email): export Tailwind templates to an outDir outside the project

TH
10h ago
+74-1

Related issue or discussion

Closes #3912

What this changes

email export bundles each template into --outDir as a .cjs file and then requires it from there. css-tree is kept external, so the bundle has require("css-tree/dist/csstree.esm") (the specifier the react-email ESM build uses since #3643). Node resolves that from the --outDir location, so a <Tailwind> template fails with Cannot find module 'css-tree/dist/csstree.esm' when --outDir is outside the project, for example --outDir ../backend/templates.

This replaces external: ['css-tree'] with a small esbuild plugin that keeps css-tree external but points the import at the absolute path of the css-tree that the template resolves. The rendered output does not change, and the .cjs files are still deleted after rendering.

Tests:

  • New test in export.spec.ts exports a <Tailwind> template that imports from react-email (as an installed project does) into a temporary directory outside the project. It stubs NODE_PATH, because pnpm puts its store there and that hides the failure. It fails on canary with the error from the issue and passes with this change.
  • tree.spec.ts snapshot updated for the new file in utils/esbuild.
  • The new fixture folder is excluded in tsconfig.json, like the other fixtures that import react-email.
  • vitest run src passes in packages/react-email, except export-esbuild-plugins.spec.ts > reports a plugins module that fails to load, which spawns pnpm and fails the same way on canary on my machine. tsc --noEmit and biome check pass.

Checklist

  • I understand the code in this PR and the problem it solves.
  • I read this description, and it accurately describes the change.
  • I added a changeset, or this change doesn't affect a published package.

Summary by cubic

Fixes email export failing with "Cannot find module 'css-tree/dist/csstree.esm'" for <Tailwind> templates when --outDir is outside the project.

The bundles keep css-tree external, but the bare specifier only resolved when the output lived inside the project. A small esbuild plugin now resolves css-tree against the template's own directory and stamps the absolute path into the bundle, so exports work from any --outDir. Rendered output is unchanged, and the temporary .cjs files are still cleaned up.