Skip to content

feat: Separate preview server from react-email #2182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 51 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
879951a
feat(react-email): added a theme switcher to the dev preview (#1749)
KayleeWilliams Feb 17, 2025
4a65e54
chore(root): Lint
gabrielmfern Mar 31, 2025
1b59627
fix(react-email): Rebase artifact with default active view
gabrielmfern Mar 31, 2025
ff3cdf1
chore(root): Version packages (canary) (#2064)
github-actions[bot] Apr 8, 2025
85e1c4f
chore(root): Version packages (canary) (#2217)
github-actions[bot] May 7, 2025
f286a23
chore(root): Version packages (canary) (#2233)
github-actions[bot] May 12, 2025
0a81d9a
chore(root): Version packages (canary) (#2217)
github-actions[bot] May 7, 2025
9bf4a2f
fix(root): lockfile
gabrielmfern May 9, 2025
3806c15
chore(root): Version packages (canary) (#2184)
github-actions[bot] May 5, 2025
7a7fac5
initial separated preview server
gabrielmfern Apr 22, 2025
e655073
fix traverse
gabrielmfern Apr 22, 2025
cca48a5
update get-emails-directory code on preview-server
gabrielmfern Apr 23, 2025
3cbd6e0
remove import = require
gabrielmfern May 5, 2025
0a0c2b1
lint
gabrielmfern May 5, 2025
e6180e0
add code to install the package automatically
gabrielmfern May 5, 2025
3673a22
quit process after installing preview server
gabrielmfern May 5, 2025
f2a2895
fix build error
gabrielmfern May 5, 2025
7f12199
lint
gabrielmfern May 5, 2025
653abe1
update lock
gabrielmfern May 12, 2025
f0f8a3d
implement `email build` and `email start`
gabrielmfern May 5, 2025
c9b6eae
lint
gabrielmfern May 5, 2025
6c2520f
fix majority of tests
gabrielmfern May 5, 2025
4d5c651
fix cache for preview-server
gabrielmfern May 5, 2025
137c52f
install on demo build
gabrielmfern May 5, 2025
a0b6c65
delete .react-email after build
gabrielmfern May 6, 2025
d4de7b9
fix filter for build
gabrielmfern May 6, 2025
9418de7
lint
gabrielmfern May 6, 2025
22077e1
use rm instead of rmdir
gabrielmfern May 6, 2025
55c2784
fix tests and some type issues
gabrielmfern May 6, 2025
7e8cee6
lint
gabrielmfern May 6, 2025
751be63
fix support for building with node 18
gabrielmfern May 6, 2025
87cdb4a
move preview-server to packages/preview-server
gabrielmfern May 6, 2025
d6b12d5
move jiti to a dependency
gabrielmfern May 6, 2025
9ffc231
undo changes to email build to use same mechanism as before, but copy…
gabrielmfern May 6, 2025
3179052
remove warning
gabrielmfern May 6, 2025
c95d43a
keep update package json as it was originally
gabrielmfern May 6, 2025
ee4f94b
juse getPreviewServerLocation on startDevServer
gabrielmfern May 12, 2025
b2c74ba
lint
gabrielmfern May 12, 2025
831ae20
update lock
gabrielmfern May 12, 2025
1a65a2b
fix `--watch` command
gabrielmfern May 13, 2025
675b89a
remove now unecessary expect error
gabrielmfern May 16, 2025
1c205fb
use unpinned next for demo
gabrielmfern May 20, 2025
1ad4d4e
remove pnpm install from react-email
gabrielmfern May 20, 2025
484c3a9
remove rebase artifact
gabrielmfern May 20, 2025
8d25e45
remove empty dev script
gabrielmfern May 20, 2025
fe9de00
add changeset
gabrielmfern May 20, 2025
a641a22
use 1.0.0-canary.0 for preview-server
gabrielmfern May 20, 2025
21337a1
remove theme switcher
gabrielmfern May 20, 2025
aea73f9
remove changeset for theme switcher
gabrielmfern May 20, 2025
0533ebd
set private to false
gabrielmfern May 20, 2025
e652bc1
fix publish config and repository config
gabrielmfern May 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
},
"changesets": [
"crazy-seas-eat",
"dirty-needles-chew",
"great-parrots-yell",
"mighty-pigs-add",
"stupid-ghosts-decide",
Expand Down
5 changes: 5 additions & 0 deletions .changeset/puny-chicken-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-email": minor
---

use a separate package for storing the preview server (@react-email/preview-server)
5 changes: 3 additions & 2 deletions apps/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"private": true,
"scripts": {
"build": "email build",
"build": "pnpm install --frozen-lockfile && email build",
"dev": "email dev",
"start": "email start",
"export": "email export"
Expand All @@ -15,7 +15,8 @@
"react-email": "workspace:*"
},
"devDependencies": {
"next": "15.3.1",
"@react-email/preview-server": "workspace:*",
"next": "^15.2.4",
"@types/react": "^19",
"@types/react-dom": "^19",
"tsx": "4.19.3"
Expand Down
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"!**/pnpm-lock.yaml",
"!**/.next",
"!**/public",
"!packages/react-email/src/actions/email-validation/caniemail-data.ts",
"!packages/preview-server/src/actions/email-validation/caniemail-data.ts",
"!**/.react-email/**/*",
"!**/node_modules/**/*",
"!**/*.d.ts",
Expand Down
5 changes: 5 additions & 0 deletions packages/preview-server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.next

# for testing
static
5 changes: 5 additions & 0 deletions packages/preview-server/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.react-email
./emails
./emails/static
node_modules
.turbo
4 changes: 4 additions & 0 deletions packages/preview-server/_index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* this file is just a placeholder file so that import.meta.resolve and require.resolve can properly
* find out the path to this module. This file does not do anything nor does it need to export anything of value.
*/
7 changes: 7 additions & 0 deletions packages/preview-server/license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2024 Plus Five Five, Inc

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
82 changes: 82 additions & 0 deletions packages/preview-server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"name": "@react-email/preview-server",
"version": "1.0.0-canary.0",
"description": "A live preview of your emails right in your browser.",
"scripts": {
"caniemail:fetch": "node ./scripts/fill-caniemail-data.mjs",
"clean": "rm -rf dist",
"build": "node ./scripts/build-preview-server.mjs",
"test": "vitest run",
"test:watch": "vitest"
},
"main": "./_index.js",
"dependencies": {
"@babel/core": "7.26.10",
"@babel/parser": "^7.27.0",
"@babel/traverse": "^7.27.0",
"@lottiefiles/dotlottie-react": "0.13.3",
"@radix-ui/colors": "3.0.0",
"@radix-ui/react-collapsible": "1.1.7",
"@radix-ui/react-dropdown-menu": "2.1.10",
"@radix-ui/react-popover": "1.1.10",
"@radix-ui/react-slot": "1.2.0",
"@radix-ui/react-tabs": "1.1.7",
"@radix-ui/react-toggle-group": "1.1.6",
"@radix-ui/react-tooltip": "1.2.3",
"@types/node": "22.14.1",
"@types/normalize-path": "3.0.2",
"@types/react": "19.0.10",
"@types/react-dom": "19.0.4",
"@types/webpack": "5.28.5",
"autoprefixer": "10.4.21",
"chalk": "^4.1.2",
"clsx": "2.1.1",
"esbuild": "^0.25.0",
"framer-motion": "12.7.5",
"json5": "2.2.3",
"log-symbols": "^4.1.0",
"module-punycode": "npm:[email protected]",
"next": "^15.2.4",
"node-html-parser": "7.0.1",
"ora": "^5.4.1",
"pretty-bytes": "6.1.1",
"prism-react-renderer": "2.4.1",
"react": "19.0.0",
"react-dom": "19.0.0",
"sharp": "0.34.1",
"socket.io-client": "4.8.1",
"sonner": "2.0.3",
"source-map-js": "1.2.1",
"spamc": "0.0.5",
"stacktrace-parser": "0.1.11",
"tailwind-merge": "3.2.0",
"tailwindcss": "3.4.0",
"use-debounce": "10.0.4",
"zod": "3.24.3"
},
"devDependencies": {
"@react-email/components": "workspace:*",
"@types/babel__core": "7.20.5",
"@types/babel__traverse": "7.20.7",
"@types/fs-extra": "11.0.1",
"@types/mime-types": "2.1.4",
"@types/node": "22.10.2",
"@types/normalize-path": "3.0.2",
"@types/react": "19.0.10",
"@types/react-dom": "19.0.4",
"@types/webpack": "5.28.5",
"autoprefixer": "10.4.21",
"postcss": "8.5.3",
"tailwindcss": "3.4.0",
"typescript": "5.8.3"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/resend/react-email.git",
"directory": "packages/preview-server"
},
"publishConfig": {
"access": "public"
}
}
29 changes: 29 additions & 0 deletions packages/preview-server/scripts/build-preview-server.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { spawn } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import url from 'node:url';

const filename = url.fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);

const nextBuildProcess = spawn('pnpm', ['next', 'build'], {
detached: true,
shell: true,
stdio: 'inherit',
cwd: path.resolve(dirname, '../'),
});

process.on('SIGINT', () => {
nextBuildProcess.kill('SIGINT');
});

nextBuildProcess.on('exit', (code) => {
if (code !== 0) {
console.error(`next build failed with exit code ${code}`);
process.exit(code);
}

fs.rmSync(path.resolve(dirname, '../.next/cache'), {
recursive: true,
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function escapeStringForRegex(string: string) {
return string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { promises as fs } from 'node:fs';
import path from 'node:path';
import type { Loader, PluginBuild, ResolveOptions } from 'esbuild';
import { escapeStringForRegex } from './escape-string-for-regex';

/**
* Made to export the `render` function out of the user's email template
* so that issues like https://github.com/resend/react-email/issues/649 don't
* happen.
*
* This also exports the `createElement` from the user's React version as well
* to avoid mismatches.
*
* This avoids multiple versions of React being involved, i.e., the version
* in the CLI vs. the version the user has on their emails.
*/
export const renderingUtilitiesExporter = (emailTemplates: string[]) => ({
name: 'rendering-utilities-exporter',
setup: (b: PluginBuild) => {
b.onLoad(
{
filter: new RegExp(
emailTemplates
.map((emailPath) => escapeStringForRegex(emailPath))
.join('|'),
),
},
async ({ path: pathToFile }) => {
return {
contents: `${await fs.readFile(pathToFile, 'utf8')};
export { render } from 'react-email-module-that-will-export-render'
export { createElement as reactEmailCreateReactElement } from 'react';
`,
loader: path.extname(pathToFile).slice(1) as Loader,
};
},
);

b.onResolve(
{ filter: /^react-email-module-that-will-export-render$/ },
async (args) => {
const options: ResolveOptions = {
kind: 'import-statement',
importer: args.importer,
resolveDir: args.resolveDir,
namespace: args.namespace,
};
let result = await b.resolve('@react-email/render', options);
if (result.errors.length === 0) {
return result;
}

// If @react-email/render does not exist, resolve to @react-email/components
result = await b.resolve('@react-email/components', options);
if (result.errors.length > 0 && result.errors[0]) {
result.errors[0].text =
"Failed trying to import `render` from either `@react-email/render` or `@react-email/components` to be able to render your email template.\n Maybe you don't have either of them installed?";
}
return result;
},
);
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import path from 'node:path';
import { getEmailsDirectoryMetadata } from './get-emails-directory-metadata';

test('getEmailsDirectoryMetadata on demo emails', async () => {
const emailsDirectoryPath = path.resolve(
__dirname,
'../../../../apps/demo/emails',
);
expect(await getEmailsDirectoryMetadata(emailsDirectoryPath)).toEqual({
absolutePath: emailsDirectoryPath,
directoryName: 'emails',
relativePath: '',
emailFilenames: [],
subDirectories: [
{
absolutePath: `${emailsDirectoryPath}/magic-links`,
directoryName: 'magic-links',
relativePath: 'magic-links',
emailFilenames: [
'aws-verify-email',
'linear-login-code',
'notion-magic-link',
'plaid-verify-identity',
'raycast-magic-link',
'slack-confirm',
],
subDirectories: [],
},
{
absolutePath: `${emailsDirectoryPath}/newsletters`,
directoryName: 'newsletters',
relativePath: 'newsletters',
emailFilenames: [
'codepen-challengers',
'google-play-policy-update',
'stack-overflow-tips',
],
subDirectories: [],
},
{
absolutePath: `${emailsDirectoryPath}/notifications`,
directoryName: 'notifications',
relativePath: 'notifications',
emailFilenames: [
'github-access-token',
'papermark-year-in-review',
'vercel-invite-user',
'yelp-recent-login',
],
subDirectories: [],
},
{
absolutePath: `${emailsDirectoryPath}/receipts`,
directoryName: 'receipts',
relativePath: 'receipts',
emailFilenames: ['apple-receipt', 'nike-receipt'],
subDirectories: [],
},
{
absolutePath: `${emailsDirectoryPath}/reset-password`,
directoryName: 'reset-password',
relativePath: 'reset-password',
emailFilenames: ['dropbox-reset-password', 'twitch-reset-password'],
subDirectories: [],
},
{
absolutePath: `${emailsDirectoryPath}/reviews`,
directoryName: 'reviews',
relativePath: 'reviews',
emailFilenames: ['airbnb-review', 'amazon-review'],
subDirectories: [],
},
{
absolutePath: `${emailsDirectoryPath}/welcome`,
directoryName: 'welcome',
relativePath: 'welcome',
emailFilenames: ['koala-welcome', 'netlify-welcome', 'stripe-welcome'],
subDirectories: [],
},
],
});
});
Loading
Loading