react-email 4.0.15 → 4.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/dist/cli/index.d.mts +1 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +2785 -0
- package/dist/cli/index.mjs +77 -47
- package/dist/index.d.mts +20 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +1410 -0
- package/dist/index.mjs +21 -0
- package/dist/package/index.d.mts +33 -0
- package/dist/package/index.d.ts +33 -0
- package/dist/package/index.js +62 -0
- package/dist/package/index.mjs +7 -0
- package/dist/preview/.next/BUILD_ID +1 -1
- package/dist/preview/.next/app-build-manifest.json +10 -10
- package/dist/preview/.next/app-path-routes-manifest.json +1 -1
- package/dist/preview/.next/build-manifest.json +3 -3
- package/dist/preview/.next/next-minimal-server.js.nft.json +1 -1
- package/dist/preview/.next/next-server.js.nft.json +1 -1
- package/dist/preview/.next/prerender-manifest.json +3 -3
- package/dist/preview/.next/required-server-files.json +4 -4
- package/dist/preview/.next/server/app/_not-found/page.js +1 -1
- package/dist/preview/.next/server/app/_not-found/page_client-reference-manifest.js +1 -1
- package/dist/preview/.next/server/app/favicon.ico/route.js +1 -1
- package/dist/preview/.next/server/app/page.js +1 -1
- package/dist/preview/.next/server/app/page.js.nft.json +1 -1
- package/dist/preview/.next/server/app/page_client-reference-manifest.js +1 -1
- package/dist/preview/.next/server/app/preview/[...slug]/page.js +17 -17
- package/dist/preview/.next/server/app/preview/[...slug]/page.js.nft.json +1 -1
- package/dist/preview/.next/server/app/preview/[...slug]/page_client-reference-manifest.js +1 -1
- package/dist/preview/.next/server/app-paths-manifest.json +1 -1
- package/dist/preview/.next/server/chunks/147.js +1 -0
- package/dist/preview/.next/server/chunks/235.js +4 -4
- package/dist/preview/.next/server/middleware-build-manifest.js +1 -1
- package/dist/preview/.next/server/next-font-manifest.js +1 -1
- package/dist/preview/.next/server/next-font-manifest.json +1 -1
- package/dist/preview/.next/server/pages/500.html +1 -1
- package/dist/preview/.next/server/pages-manifest.json +1 -1
- package/dist/preview/.next/server/server-reference-manifest.js +1 -1
- package/dist/preview/.next/server/server-reference-manifest.json +1 -1
- package/dist/preview/.next/static/chunks/484-3d4d9735a701a476.js +1 -0
- package/dist/preview/.next/static/chunks/app/layout-95b74654735388bd.js +1 -0
- package/dist/preview/.next/static/chunks/app/page-2dbfb5b2dc4b1191.js +1 -0
- package/dist/preview/.next/static/chunks/app/preview/[...slug]/page-42afb14d97d633e3.js +1 -0
- package/dist/preview/.next/static/chunks/main-app-976577a424e11c75.js +1 -0
- package/dist/preview/.next/static/css/{35e8811589f0962b.css → 78c81281aa95270f.css} +1 -1
- package/dist/preview/.next/static/media/8e9860b6e62d6359-s.woff2 +0 -0
- package/dist/preview/.next/static/media/e4af272ccee01ff0-s.p.woff2 +0 -0
- package/dist/preview/.next/trace +27 -27
- package/dist/preview/.next/types/app/layout.ts +1 -1
- package/dist/preview/.next/types/app/page.ts +1 -1
- package/dist/preview/.next/types/app/preview/[...slug]/page.ts +1 -1
- package/package.json +4 -3
- package/src/app/layout.tsx +2 -2
- package/src/app/preview/[...slug]/preview.tsx +1 -1
- package/src/commands/testing/out/notifications/yelp-recent-login.html +1 -1
- package/src/commands/testing/out/reset-password/twitch-reset-password.html +1 -1
- package/src/components/shell.tsx +4 -1
- package/src/components/sidebar/file-tree-directory-children.tsx +7 -2
- package/src/package/body/dist/index.d.mts +6 -0
- package/src/package/body/dist/index.d.ts +6 -0
- package/src/package/body/dist/index.js +79 -0
- package/src/package/body/dist/index.mjs +45 -0
- package/src/package/button/dist/index.d.mts +6 -0
- package/src/package/button/dist/index.d.ts +6 -0
- package/src/package/button/dist/index.js +252 -0
- package/src/package/button/dist/index.mjs +218 -0
- package/src/package/code-block/dist/index.d.mts +4906 -0
- package/src/package/code-block/dist/index.d.ts +4906 -0
- package/src/package/code-block/dist/index.js +18205 -0
- package/src/package/code-block/dist/index.mjs +18133 -0
- package/src/package/code-inline/dist/index.d.mts +11 -0
- package/src/package/code-inline/dist/index.d.ts +11 -0
- package/src/package/code-inline/dist/index.js +106 -0
- package/src/package/code-inline/dist/index.mjs +72 -0
- package/src/package/column/dist/index.d.mts +6 -0
- package/src/package/column/dist/index.d.ts +6 -0
- package/src/package/column/dist/index.js +79 -0
- package/src/package/column/dist/index.mjs +45 -0
- package/src/package/components/dist/index.d.mts +20 -0
- package/src/package/components/dist/index.d.ts +20 -0
- package/src/package/components/dist/index.js +62 -0
- package/src/package/components/dist/index.mjs +21 -0
- package/src/package/container/dist/index.d.mts +6 -0
- package/src/package/container/dist/index.d.ts +6 -0
- package/src/package/container/dist/index.js +93 -0
- package/src/package/container/dist/index.mjs +59 -0
- package/src/package/font/dist/index.d.mts +25 -0
- package/src/package/font/dist/index.d.ts +25 -0
- package/src/package/font/dist/index.js +55 -0
- package/src/package/font/dist/index.mjs +28 -0
- package/src/package/head/dist/index.d.mts +6 -0
- package/src/package/head/dist/index.d.ts +6 -0
- package/src/package/head/dist/index.js +83 -0
- package/src/package/head/dist/index.mjs +49 -0
- package/src/package/heading/dist/index.d.mts +43 -0
- package/src/package/heading/dist/index.d.ts +43 -0
- package/src/package/heading/dist/index.js +113 -0
- package/src/package/heading/dist/index.mjs +79 -0
- package/src/package/hr/dist/index.d.mts +6 -0
- package/src/package/hr/dist/index.d.ts +6 -0
- package/src/package/hr/dist/index.js +89 -0
- package/src/package/hr/dist/index.mjs +55 -0
- package/src/package/html/dist/index.d.mts +6 -0
- package/src/package/html/dist/index.d.ts +6 -0
- package/src/package/html/dist/index.js +79 -0
- package/src/package/html/dist/index.mjs +45 -0
- package/src/package/img/dist/index.d.mts +6 -0
- package/src/package/img/dist/index.d.ts +6 -0
- package/src/package/img/dist/index.js +94 -0
- package/src/package/img/dist/index.mjs +60 -0
- package/src/package/link/dist/index.d.mts +6 -0
- package/src/package/link/dist/index.d.ts +6 -0
- package/src/package/link/dist/index.js +90 -0
- package/src/package/link/dist/index.mjs +56 -0
- package/src/package/markdown/dist/index.d.mts +15 -0
- package/src/package/markdown/dist/index.d.ts +15 -0
- package/src/package/markdown/dist/index.js +92 -0
- package/src/package/markdown/dist/index.mjs +58 -0
- package/src/package/preview/dist/index.d.mts +12 -0
- package/src/package/preview/dist/index.d.ts +12 -0
- package/src/package/preview/dist/index.js +108 -0
- package/src/package/preview/dist/index.mjs +73 -0
- package/src/package/render/dist/browser/index.d.mts +24 -0
- package/src/package/render/dist/browser/index.d.ts +24 -0
- package/src/package/render/dist/browser/index.js +250 -0
- package/src/package/render/dist/browser/index.mjs +214 -0
- package/src/package/render/dist/index.d.mts +23 -0
- package/src/package/render/dist/index.d.ts +23 -0
- package/src/package/render/dist/index.js +768 -0
- package/src/package/render/dist/index.mjs +733 -0
- package/src/package/render/dist/node/index.d.mts +27 -0
- package/src/package/render/dist/node/index.d.ts +27 -0
- package/src/package/render/dist/node/index.js +212 -0
- package/src/package/render/dist/node/index.mjs +176 -0
- package/src/package/row/dist/index.d.mts +10 -0
- package/src/package/row/dist/index.d.ts +10 -0
- package/src/package/row/dist/index.js +93 -0
- package/src/package/row/dist/index.mjs +59 -0
- package/src/package/section/dist/index.d.mts +6 -0
- package/src/package/section/dist/index.d.ts +6 -0
- package/src/package/section/dist/index.js +93 -0
- package/src/package/section/dist/index.mjs +59 -0
- package/src/package/tailwind/dist/index.d.ts +19 -0
- package/src/package/tailwind/dist/index.js +48 -0
- package/src/package/tailwind/dist/index.mjs +17167 -0
- package/src/package/tailwind/dist/tailwindcss/config.d.ts +376 -0
- package/src/package/tailwind/dist/tailwindcss/generated/.gitkeep +0 -0
- package/src/package/tailwind/dist/tailwindcss/generated/colors.d.ts +298 -0
- package/src/package/tailwind/dist/tailwindcss/generated/corePluginList.d.ts +1 -0
- package/src/package/tailwind/dist/tailwindcss/generated/default-theme.d.ts +397 -0
- package/src/package/tailwind/dist/tailwindcss/index.d.ts +11 -0
- package/src/package/text/dist/index.d.mts +6 -0
- package/src/package/text/dist/index.d.ts +6 -0
- package/src/package/text/dist/index.js +89 -0
- package/src/package/text/dist/index.mjs +55 -0
- package/src/utils/__snapshots__/get-email-component.spec.ts.snap +1 -1
- package/src/utils/caniemail/get-element-names.ts +9 -3
- package/src/utils/contains-email-template.spec.ts +12 -0
- package/src/utils/contains-email-template.ts +26 -9
- package/src/utils/improve-error-with-sourcemap.ts +0 -1
- package/src/utils/types/error-object.ts +1 -1
- package/dist/preview/.next/server/chunks/488.js +0 -1
- package/dist/preview/.next/static/chunks/484-21e30f98c67e8d58.js +0 -1
- package/dist/preview/.next/static/chunks/app/layout-3044a159bf29302b.js +0 -1
- package/dist/preview/.next/static/chunks/app/page-ed77f0af99f8ff62.js +0 -1
- package/dist/preview/.next/static/chunks/app/preview/[...slug]/page-994a4d501e46f3f8.js +0 -1
- package/dist/preview/.next/static/chunks/main-app-5bc2d814f500db60.js +0 -1
- package/dist/preview/.next/static/media/6d93bde91c0c2823-s.woff2 +0 -0
- package/dist/preview/.next/static/media/a34f9d1faa5f3315-s.p.woff2 +0 -0
- /package/dist/preview/.next/static/{0LFN6rP55keVoTJYuP-4s → 9A_Lez8sdD6nDBcSehk5k}/_buildManifest.js +0 -0
- /package/dist/preview/.next/static/{0LFN6rP55keVoTJYuP-4s → 9A_Lez8sdD6nDBcSehk5k}/_ssgManifest.js +0 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1410 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { program } from "commander";
|
|
5
|
+
|
|
6
|
+
// src/commands/build.ts
|
|
7
|
+
import { spawn } from "node:child_process";
|
|
8
|
+
import fs2 from "node:fs";
|
|
9
|
+
import path3 from "node:path";
|
|
10
|
+
import logSymbols2 from "log-symbols";
|
|
11
|
+
import ora from "ora";
|
|
12
|
+
|
|
13
|
+
// src/utils/get-emails-directory-metadata.ts
|
|
14
|
+
import fs from "node:fs";
|
|
15
|
+
import path from "node:path";
|
|
16
|
+
var isFileAnEmail = async (fullPath) => {
|
|
17
|
+
let fileHandle;
|
|
18
|
+
try {
|
|
19
|
+
fileHandle = await fs.promises.open(fullPath, "r");
|
|
20
|
+
} catch (exception) {
|
|
21
|
+
console.warn(exception);
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
const stat = await fileHandle.stat();
|
|
25
|
+
if (stat.isDirectory()) {
|
|
26
|
+
await fileHandle.close();
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
const { ext } = path.parse(fullPath);
|
|
30
|
+
if (![".js", ".tsx", ".jsx"].includes(ext)) {
|
|
31
|
+
await fileHandle.close();
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
const fileContents = await fileHandle.readFile("utf8");
|
|
35
|
+
await fileHandle.close();
|
|
36
|
+
const hasES6DefaultExport = /\bexport\s+default\b/gm.test(fileContents);
|
|
37
|
+
const hasCommonJSExport = /\bmodule\.exports\s*=/gm.test(fileContents);
|
|
38
|
+
const hasNamedExport = /\bexport\s+\{[^}]*\bdefault\b[^}]*\}/gm.test(
|
|
39
|
+
fileContents
|
|
40
|
+
);
|
|
41
|
+
return hasES6DefaultExport || hasCommonJSExport || hasNamedExport;
|
|
42
|
+
};
|
|
43
|
+
var mergeDirectoriesWithSubDirectories = (emailsDirectoryMetadata) => {
|
|
44
|
+
let currentResultingMergedDirectory = emailsDirectoryMetadata;
|
|
45
|
+
while (currentResultingMergedDirectory.emailFilenames.length === 0 && currentResultingMergedDirectory.subDirectories.length === 1) {
|
|
46
|
+
const onlySubDirectory = currentResultingMergedDirectory.subDirectories[0];
|
|
47
|
+
currentResultingMergedDirectory = {
|
|
48
|
+
...onlySubDirectory,
|
|
49
|
+
directoryName: path.join(
|
|
50
|
+
currentResultingMergedDirectory.directoryName,
|
|
51
|
+
onlySubDirectory.directoryName
|
|
52
|
+
)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return currentResultingMergedDirectory;
|
|
56
|
+
};
|
|
57
|
+
var getEmailsDirectoryMetadata = async (absolutePathToEmailsDirectory, keepFileExtensions = false, isSubDirectory = false, baseDirectoryPath = absolutePathToEmailsDirectory) => {
|
|
58
|
+
if (!fs.existsSync(absolutePathToEmailsDirectory)) return;
|
|
59
|
+
const dirents = await fs.promises.readdir(absolutePathToEmailsDirectory, {
|
|
60
|
+
withFileTypes: true
|
|
61
|
+
});
|
|
62
|
+
const isEmailPredicates = await Promise.all(
|
|
63
|
+
dirents.map(
|
|
64
|
+
(dirent) => isFileAnEmail(path.join(absolutePathToEmailsDirectory, dirent.name))
|
|
65
|
+
)
|
|
66
|
+
);
|
|
67
|
+
const emailFilenames = dirents.filter((_, i) => isEmailPredicates[i]).map(
|
|
68
|
+
(dirent) => keepFileExtensions ? dirent.name : dirent.name.replace(path.extname(dirent.name), "")
|
|
69
|
+
);
|
|
70
|
+
const subDirectories = await Promise.all(
|
|
71
|
+
dirents.filter(
|
|
72
|
+
(dirent) => dirent.isDirectory() && !dirent.name.startsWith("_") && dirent.name !== "static"
|
|
73
|
+
).map((dirent) => {
|
|
74
|
+
const direntAbsolutePath = path.join(
|
|
75
|
+
absolutePathToEmailsDirectory,
|
|
76
|
+
dirent.name
|
|
77
|
+
);
|
|
78
|
+
return getEmailsDirectoryMetadata(
|
|
79
|
+
direntAbsolutePath,
|
|
80
|
+
keepFileExtensions,
|
|
81
|
+
true,
|
|
82
|
+
baseDirectoryPath
|
|
83
|
+
);
|
|
84
|
+
})
|
|
85
|
+
);
|
|
86
|
+
const emailsMetadata = {
|
|
87
|
+
absolutePath: absolutePathToEmailsDirectory,
|
|
88
|
+
relativePath: path.relative(
|
|
89
|
+
baseDirectoryPath,
|
|
90
|
+
absolutePathToEmailsDirectory
|
|
91
|
+
),
|
|
92
|
+
directoryName: absolutePathToEmailsDirectory.split(path.sep).pop(),
|
|
93
|
+
emailFilenames,
|
|
94
|
+
subDirectories
|
|
95
|
+
};
|
|
96
|
+
return isSubDirectory ? mergeDirectoriesWithSubDirectories(emailsMetadata) : emailsMetadata;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// src/utils/get-preview-server-location.ts
|
|
100
|
+
import path2 from "node:path";
|
|
101
|
+
import url from "node:url";
|
|
102
|
+
import { createJiti } from "jiti";
|
|
103
|
+
import { addDevDependency } from "nypm";
|
|
104
|
+
import prompts from "prompts";
|
|
105
|
+
|
|
106
|
+
// package.json
|
|
107
|
+
var package_default = {
|
|
108
|
+
name: "react-email",
|
|
109
|
+
version: "4.1.0-canary.9",
|
|
110
|
+
description: "A live preview of your emails right in your browser.",
|
|
111
|
+
bin: {
|
|
112
|
+
email: "./dist/index.js"
|
|
113
|
+
},
|
|
114
|
+
type: "module",
|
|
115
|
+
scripts: {
|
|
116
|
+
build: "tsup-node",
|
|
117
|
+
clean: "rm -rf dist",
|
|
118
|
+
dev: "tsup-node --watch src",
|
|
119
|
+
test: "vitest run",
|
|
120
|
+
"test:watch": "vitest"
|
|
121
|
+
},
|
|
122
|
+
license: "MIT",
|
|
123
|
+
repository: {
|
|
124
|
+
type: "git",
|
|
125
|
+
url: "https://github.com/resend/react-email.git",
|
|
126
|
+
directory: "packages/react-email"
|
|
127
|
+
},
|
|
128
|
+
keywords: [
|
|
129
|
+
"react",
|
|
130
|
+
"email"
|
|
131
|
+
],
|
|
132
|
+
engines: {
|
|
133
|
+
node: ">=18.0.0"
|
|
134
|
+
},
|
|
135
|
+
dependencies: {
|
|
136
|
+
"@babel/parser": "^7.27.0",
|
|
137
|
+
"@babel/traverse": "^7.27.0",
|
|
138
|
+
chalk: "^5.0.0",
|
|
139
|
+
chokidar: "^4.0.3",
|
|
140
|
+
commander: "^13.0.0",
|
|
141
|
+
debounce: "^2.0.0",
|
|
142
|
+
esbuild: "^0.25.0",
|
|
143
|
+
glob: "^11.0.0",
|
|
144
|
+
jiti: "2.4.2",
|
|
145
|
+
"log-symbols": "^7.0.0",
|
|
146
|
+
"mime-types": "^3.0.0",
|
|
147
|
+
"normalize-path": "^3.0.0",
|
|
148
|
+
nypm: "0.6.0",
|
|
149
|
+
ora: "^8.0.0",
|
|
150
|
+
prompts: "2.4.2",
|
|
151
|
+
"socket.io": "^4.8.1",
|
|
152
|
+
"tsconfig-paths": "4.2.0"
|
|
153
|
+
},
|
|
154
|
+
devDependencies: {
|
|
155
|
+
"@react-email/components": "workspace:*",
|
|
156
|
+
"@types/babel__core": "7.20.5",
|
|
157
|
+
"@types/babel__traverse": "7.20.7",
|
|
158
|
+
"@types/mime-types": "2.1.4",
|
|
159
|
+
"@types/prompts": "2.4.9",
|
|
160
|
+
next: "^15.3.2",
|
|
161
|
+
react: "19.0.0",
|
|
162
|
+
"react-dom": "19.0.0",
|
|
163
|
+
tsup: "8.4.0",
|
|
164
|
+
tsx: "4.19.3",
|
|
165
|
+
typescript: "5.8.3"
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
// src/utils/get-preview-server-location.ts
|
|
170
|
+
var ensurePreviewServerInstalled = async (message) => {
|
|
171
|
+
const response = await prompts({
|
|
172
|
+
type: "confirm",
|
|
173
|
+
name: "installPreviewServer",
|
|
174
|
+
message,
|
|
175
|
+
initial: true
|
|
176
|
+
});
|
|
177
|
+
if (response.installPreviewServer) {
|
|
178
|
+
console.log('Installing "@react-email/preview-server"');
|
|
179
|
+
await addDevDependency(
|
|
180
|
+
`@react-email/preview-server@${package_default.version}`
|
|
181
|
+
);
|
|
182
|
+
process.exit(0);
|
|
183
|
+
} else {
|
|
184
|
+
process.exit(0);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
var getPreviewServerLocation = async () => {
|
|
188
|
+
const usersProject = createJiti(process.cwd());
|
|
189
|
+
let previewServerLocation;
|
|
190
|
+
try {
|
|
191
|
+
previewServerLocation = path2.dirname(
|
|
192
|
+
url.parse(usersProject.esmResolve("@react-email/preview-server"), true).path
|
|
193
|
+
);
|
|
194
|
+
} catch (_exception) {
|
|
195
|
+
await ensurePreviewServerInstalled(
|
|
196
|
+
'To run the preview server, the package "@react-email/preview-server" must be installed. Would you like to install it?'
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
const { version } = await usersProject.import("@react-email/preview-server");
|
|
200
|
+
if (version !== package_default.version) {
|
|
201
|
+
await ensurePreviewServerInstalled(
|
|
202
|
+
`To run the preview server, the version of "@react-email/preview-server" must match the version of "react-email" (${package_default.version}). Would you like to install it?`
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
return previewServerLocation;
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
// src/utils/register-spinner-autostopping.ts
|
|
209
|
+
import logSymbols from "log-symbols";
|
|
210
|
+
var spinners = /* @__PURE__ */ new Set();
|
|
211
|
+
process.on("SIGINT", () => {
|
|
212
|
+
spinners.forEach((spinner) => {
|
|
213
|
+
if (spinner.isSpinning) {
|
|
214
|
+
spinner.stop();
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
process.on("exit", (code) => {
|
|
219
|
+
if (code !== 0) {
|
|
220
|
+
spinners.forEach((spinner) => {
|
|
221
|
+
if (spinner.isSpinning) {
|
|
222
|
+
spinner.stopAndPersist({
|
|
223
|
+
symbol: logSymbols.error
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
var registerSpinnerAutostopping = (spinner) => {
|
|
230
|
+
spinners.add(spinner);
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
// src/commands/build.ts
|
|
234
|
+
var buildPreviewApp = (absoluteDirectory) => {
|
|
235
|
+
return new Promise((resolve, reject) => {
|
|
236
|
+
const nextBuild = spawn("npm", ["run", "build"], {
|
|
237
|
+
cwd: absoluteDirectory,
|
|
238
|
+
shell: true
|
|
239
|
+
});
|
|
240
|
+
nextBuild.stdout.pipe(process.stdout);
|
|
241
|
+
nextBuild.stderr.pipe(process.stderr);
|
|
242
|
+
nextBuild.on("close", (code) => {
|
|
243
|
+
if (code === 0) {
|
|
244
|
+
resolve();
|
|
245
|
+
} else {
|
|
246
|
+
reject(
|
|
247
|
+
new Error(
|
|
248
|
+
`Unable to build the Next app and it exited with code: ${code}`
|
|
249
|
+
)
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
};
|
|
255
|
+
var npmInstall = async (builtPreviewAppPath, packageManager) => {
|
|
256
|
+
return new Promise((resolve, reject) => {
|
|
257
|
+
const childProc = spawn(
|
|
258
|
+
packageManager,
|
|
259
|
+
[
|
|
260
|
+
"install",
|
|
261
|
+
packageManager === "deno" ? "" : "--include=dev",
|
|
262
|
+
packageManager === "deno" ? "--quiet" : "--silent"
|
|
263
|
+
],
|
|
264
|
+
{
|
|
265
|
+
cwd: builtPreviewAppPath,
|
|
266
|
+
shell: true
|
|
267
|
+
}
|
|
268
|
+
);
|
|
269
|
+
childProc.stdout.pipe(process.stdout);
|
|
270
|
+
childProc.stderr.pipe(process.stderr);
|
|
271
|
+
childProc.on("close", (code) => {
|
|
272
|
+
if (code === 0) {
|
|
273
|
+
resolve();
|
|
274
|
+
} else {
|
|
275
|
+
reject(
|
|
276
|
+
new Error(
|
|
277
|
+
`Unable to install the dependencies and it exited with code: ${code}`
|
|
278
|
+
)
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
};
|
|
284
|
+
var setNextEnvironmentVariablesForBuild = async (emailsDirRelativePath, builtPreviewAppPath) => {
|
|
285
|
+
const nextConfigContents = `
|
|
286
|
+
const path = require('path');
|
|
287
|
+
const emailsDirRelativePath = path.normalize('${emailsDirRelativePath}');
|
|
288
|
+
const userProjectLocation = '${process.cwd()}';
|
|
289
|
+
/** @type {import('next').NextConfig} */
|
|
290
|
+
module.exports = {
|
|
291
|
+
env: {
|
|
292
|
+
NEXT_PUBLIC_IS_BUILDING: 'true',
|
|
293
|
+
EMAILS_DIR_RELATIVE_PATH: emailsDirRelativePath,
|
|
294
|
+
EMAILS_DIR_ABSOLUTE_PATH: path.resolve(userProjectLocation, emailsDirRelativePath),
|
|
295
|
+
PREVIEW_SERVER_LOCATION: '${builtPreviewAppPath}',
|
|
296
|
+
USER_PROJECT_LOCATION: userProjectLocation
|
|
297
|
+
},
|
|
298
|
+
// this is needed so that the code for building emails works properly
|
|
299
|
+
webpack: (
|
|
300
|
+
/** @type {import('webpack').Configuration & { externals: string[] }} */
|
|
301
|
+
config,
|
|
302
|
+
{ isServer }
|
|
303
|
+
) => {
|
|
304
|
+
if (isServer) {
|
|
305
|
+
config.externals.push('esbuild');
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return config;
|
|
309
|
+
},
|
|
310
|
+
typescript: {
|
|
311
|
+
ignoreBuildErrors: true
|
|
312
|
+
},
|
|
313
|
+
eslint: {
|
|
314
|
+
ignoreDuringBuilds: true
|
|
315
|
+
},
|
|
316
|
+
experimental: {
|
|
317
|
+
webpackBuildWorker: true
|
|
318
|
+
},
|
|
319
|
+
}`;
|
|
320
|
+
await fs2.promises.writeFile(
|
|
321
|
+
path3.resolve(builtPreviewAppPath, "./next.config.js"),
|
|
322
|
+
nextConfigContents,
|
|
323
|
+
"utf8"
|
|
324
|
+
);
|
|
325
|
+
};
|
|
326
|
+
var getEmailSlugsFromEmailDirectory = (emailDirectory, emailsDirectoryAbsolutePath) => {
|
|
327
|
+
const directoryPathRelativeToEmailsDirectory = emailDirectory.absolutePath.replace(emailsDirectoryAbsolutePath, "").trim();
|
|
328
|
+
const slugs = [];
|
|
329
|
+
emailDirectory.emailFilenames.forEach(
|
|
330
|
+
(filename3) => slugs.push(
|
|
331
|
+
path3.join(directoryPathRelativeToEmailsDirectory, filename3).split(path3.sep).filter((segment) => segment.length > 0)
|
|
332
|
+
)
|
|
333
|
+
);
|
|
334
|
+
emailDirectory.subDirectories.forEach((directory) => {
|
|
335
|
+
slugs.push(
|
|
336
|
+
...getEmailSlugsFromEmailDirectory(
|
|
337
|
+
directory,
|
|
338
|
+
emailsDirectoryAbsolutePath
|
|
339
|
+
)
|
|
340
|
+
);
|
|
341
|
+
});
|
|
342
|
+
return slugs;
|
|
343
|
+
};
|
|
344
|
+
var forceSSGForEmailPreviews = async (emailsDirPath, builtPreviewAppPath) => {
|
|
345
|
+
const emailDirectoryMetadata = (
|
|
346
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
347
|
+
await getEmailsDirectoryMetadata(emailsDirPath)
|
|
348
|
+
);
|
|
349
|
+
const parameters = getEmailSlugsFromEmailDirectory(
|
|
350
|
+
emailDirectoryMetadata,
|
|
351
|
+
emailsDirPath
|
|
352
|
+
).map((slug) => ({ slug }));
|
|
353
|
+
const removeForceDynamic = async (filePath) => {
|
|
354
|
+
const contents = await fs2.promises.readFile(filePath, "utf8");
|
|
355
|
+
await fs2.promises.writeFile(
|
|
356
|
+
filePath,
|
|
357
|
+
contents.replace("export const dynamic = 'force-dynamic';", ""),
|
|
358
|
+
"utf8"
|
|
359
|
+
);
|
|
360
|
+
};
|
|
361
|
+
await removeForceDynamic(
|
|
362
|
+
path3.resolve(builtPreviewAppPath, "./src/app/layout.tsx")
|
|
363
|
+
);
|
|
364
|
+
await removeForceDynamic(
|
|
365
|
+
path3.resolve(builtPreviewAppPath, "./src/app/preview/[...slug]/page.tsx")
|
|
366
|
+
);
|
|
367
|
+
await fs2.promises.appendFile(
|
|
368
|
+
path3.resolve(builtPreviewAppPath, "./src/app/preview/[...slug]/page.tsx"),
|
|
369
|
+
`
|
|
370
|
+
|
|
371
|
+
export function generateStaticParams() {
|
|
372
|
+
return Promise.resolve(
|
|
373
|
+
${JSON.stringify(parameters)}
|
|
374
|
+
);
|
|
375
|
+
}`,
|
|
376
|
+
"utf8"
|
|
377
|
+
);
|
|
378
|
+
};
|
|
379
|
+
var updatePackageJson = async (builtPreviewAppPath) => {
|
|
380
|
+
const packageJsonPath = path3.resolve(builtPreviewAppPath, "./package.json");
|
|
381
|
+
const packageJson = JSON.parse(
|
|
382
|
+
await fs2.promises.readFile(packageJsonPath, "utf8")
|
|
383
|
+
);
|
|
384
|
+
packageJson.scripts.build = "next build";
|
|
385
|
+
packageJson.scripts.start = "next start";
|
|
386
|
+
delete packageJson.scripts.postbuild;
|
|
387
|
+
packageJson.name = "preview-server";
|
|
388
|
+
delete packageJson.devDependencies["@react-email/render"];
|
|
389
|
+
delete packageJson.devDependencies["@react-email/components"];
|
|
390
|
+
delete packageJson.scripts.prepare;
|
|
391
|
+
await fs2.promises.writeFile(
|
|
392
|
+
packageJsonPath,
|
|
393
|
+
JSON.stringify(packageJson),
|
|
394
|
+
"utf8"
|
|
395
|
+
);
|
|
396
|
+
};
|
|
397
|
+
var build = async ({
|
|
398
|
+
dir: emailsDirRelativePath,
|
|
399
|
+
packageManager
|
|
400
|
+
}) => {
|
|
401
|
+
try {
|
|
402
|
+
const previewServerLocation = await getPreviewServerLocation();
|
|
403
|
+
const spinner = ora({
|
|
404
|
+
text: "Starting build process...",
|
|
405
|
+
prefixText: " "
|
|
406
|
+
}).start();
|
|
407
|
+
registerSpinnerAutostopping(spinner);
|
|
408
|
+
spinner.text = `Checking if ${emailsDirRelativePath} folder exists`;
|
|
409
|
+
if (!fs2.existsSync(emailsDirRelativePath)) {
|
|
410
|
+
process.exit(1);
|
|
411
|
+
}
|
|
412
|
+
const emailsDirPath = path3.join(process.cwd(), emailsDirRelativePath);
|
|
413
|
+
const staticPath = path3.join(emailsDirPath, "static");
|
|
414
|
+
const builtPreviewAppPath = path3.join(process.cwd(), ".react-email");
|
|
415
|
+
if (fs2.existsSync(builtPreviewAppPath)) {
|
|
416
|
+
spinner.text = "Deleting pre-existing `.react-email` folder";
|
|
417
|
+
await fs2.promises.rm(builtPreviewAppPath, { recursive: true });
|
|
418
|
+
}
|
|
419
|
+
spinner.text = "Copying preview app from CLI to `.react-email`";
|
|
420
|
+
await fs2.promises.cp(previewServerLocation, builtPreviewAppPath, {
|
|
421
|
+
recursive: true,
|
|
422
|
+
filter: (source) => {
|
|
423
|
+
return !/(\/|\\)cli(\/|\\)?/.test(source) && !/(\/|\\)\.next(\/|\\)?/.test(source) && !/(\/|\\)\.turbo(\/|\\)?/.test(source) && !/(\/|\\)node_modules(\/|\\)?$/.test(source);
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
if (fs2.existsSync(staticPath)) {
|
|
427
|
+
spinner.text = "Copying `static` folder into `.react-email/public/static`";
|
|
428
|
+
const builtStaticDirectory = path3.resolve(
|
|
429
|
+
builtPreviewAppPath,
|
|
430
|
+
"./public/static"
|
|
431
|
+
);
|
|
432
|
+
await fs2.promises.cp(staticPath, builtStaticDirectory, {
|
|
433
|
+
recursive: true
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
spinner.text = "Setting Next environment variables for preview app to work properly";
|
|
437
|
+
await setNextEnvironmentVariablesForBuild(
|
|
438
|
+
emailsDirRelativePath,
|
|
439
|
+
builtPreviewAppPath
|
|
440
|
+
);
|
|
441
|
+
spinner.text = "Setting server side generation for the email preview pages";
|
|
442
|
+
await forceSSGForEmailPreviews(emailsDirPath, builtPreviewAppPath);
|
|
443
|
+
spinner.text = "Updating package.json's build and start scripts";
|
|
444
|
+
await updatePackageJson(builtPreviewAppPath);
|
|
445
|
+
spinner.text = "Installing dependencies on `.react-email`";
|
|
446
|
+
await npmInstall(builtPreviewAppPath, packageManager);
|
|
447
|
+
spinner.stopAndPersist({
|
|
448
|
+
text: "Successfully prepared `.react-email` for `next build`",
|
|
449
|
+
symbol: logSymbols2.success
|
|
450
|
+
});
|
|
451
|
+
await buildPreviewApp(builtPreviewAppPath);
|
|
452
|
+
} catch (error) {
|
|
453
|
+
console.log(error);
|
|
454
|
+
process.exit(1);
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
// src/commands/dev.ts
|
|
459
|
+
import fs6 from "node:fs";
|
|
460
|
+
|
|
461
|
+
// src/utils/preview/hot-reloading/setup-hot-reloading.ts
|
|
462
|
+
import path9 from "node:path";
|
|
463
|
+
import { watch } from "chokidar";
|
|
464
|
+
import debounce from "debounce";
|
|
465
|
+
import { Server as SocketServer } from "socket.io";
|
|
466
|
+
|
|
467
|
+
// src/utils/preview/hot-reloading/create-dependency-graph.ts
|
|
468
|
+
import { existsSync as existsSync2, promises as fs4, statSync } from "node:fs";
|
|
469
|
+
import path8 from "node:path";
|
|
470
|
+
|
|
471
|
+
// src/utils/preview/start-dev-server.ts
|
|
472
|
+
import http from "node:http";
|
|
473
|
+
import path6 from "node:path";
|
|
474
|
+
import url2 from "node:url";
|
|
475
|
+
import chalk from "chalk";
|
|
476
|
+
import { createJiti as createJiti2 } from "jiti";
|
|
477
|
+
import logSymbols3 from "log-symbols";
|
|
478
|
+
import ora2 from "ora";
|
|
479
|
+
|
|
480
|
+
// src/utils/preview/get-env-variables-for-preview-app.ts
|
|
481
|
+
import path4 from "node:path";
|
|
482
|
+
var getEnvVariablesForPreviewApp = (relativePathToEmailsDirectory, previewServerLocation, cwd) => {
|
|
483
|
+
return {
|
|
484
|
+
EMAILS_DIR_RELATIVE_PATH: relativePathToEmailsDirectory,
|
|
485
|
+
EMAILS_DIR_ABSOLUTE_PATH: path4.resolve(cwd, relativePathToEmailsDirectory),
|
|
486
|
+
PREVIEW_SERVER_LOCATION: previewServerLocation,
|
|
487
|
+
USER_PROJECT_LOCATION: cwd,
|
|
488
|
+
NEXT_PUBLIC_IS_PREVIEW_DEVELOPMENT: isDev ? "true" : "false"
|
|
489
|
+
};
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
// src/utils/preview/serve-static-file.ts
|
|
493
|
+
import { existsSync, promises as fs3 } from "node:fs";
|
|
494
|
+
import path5 from "node:path";
|
|
495
|
+
import { lookup } from "mime-types";
|
|
496
|
+
var serveStaticFile = async (res, parsedUrl, staticDirRelativePath) => {
|
|
497
|
+
const pathname = parsedUrl.pathname.replace("/static", "./static");
|
|
498
|
+
const ext = path5.parse(pathname).ext;
|
|
499
|
+
const staticBaseDir = path5.resolve(process.cwd(), staticDirRelativePath);
|
|
500
|
+
const fileAbsolutePath = path5.resolve(staticBaseDir, pathname);
|
|
501
|
+
if (!fileAbsolutePath.startsWith(staticBaseDir)) {
|
|
502
|
+
res.statusCode = 403;
|
|
503
|
+
res.end();
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
try {
|
|
507
|
+
const fileHandle = await fs3.open(fileAbsolutePath, "r");
|
|
508
|
+
const fileData = await fs3.readFile(fileHandle);
|
|
509
|
+
res.setHeader("Content-type", lookup(ext) || "text/plain");
|
|
510
|
+
res.end(fileData);
|
|
511
|
+
fileHandle.close();
|
|
512
|
+
} catch (exception) {
|
|
513
|
+
if (!existsSync(fileAbsolutePath)) {
|
|
514
|
+
res.statusCode = 404;
|
|
515
|
+
res.end();
|
|
516
|
+
} else {
|
|
517
|
+
const sanitizedFilePath = fileAbsolutePath.replace(/\n|\r/g, "");
|
|
518
|
+
console.error(
|
|
519
|
+
`Could not read file at ${sanitizedFilePath} to be served, here's the exception:`,
|
|
520
|
+
exception
|
|
521
|
+
);
|
|
522
|
+
res.statusCode = 500;
|
|
523
|
+
res.end(
|
|
524
|
+
"Could not read file to be served! Check your terminal for more information."
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
// src/utils/preview/start-dev-server.ts
|
|
531
|
+
var devServer;
|
|
532
|
+
var safeAsyncServerListen = (server, port) => {
|
|
533
|
+
return new Promise((resolve) => {
|
|
534
|
+
server.listen(port, () => {
|
|
535
|
+
resolve({ portAlreadyInUse: false });
|
|
536
|
+
});
|
|
537
|
+
server.on("error", (e) => {
|
|
538
|
+
if (e.code === "EADDRINUSE") {
|
|
539
|
+
resolve({ portAlreadyInUse: true });
|
|
540
|
+
}
|
|
541
|
+
});
|
|
542
|
+
});
|
|
543
|
+
};
|
|
544
|
+
var filename = url2.fileURLToPath(import.meta.url);
|
|
545
|
+
var dirname = path6.dirname(filename);
|
|
546
|
+
var isDev = !dirname.includes("dist");
|
|
547
|
+
var startDevServer = async (emailsDirRelativePath, staticBaseDirRelativePath, port) => {
|
|
548
|
+
const [majorNodeVersion] = process.versions.node.split(".");
|
|
549
|
+
if (majorNodeVersion && Number.parseInt(majorNodeVersion) < 18) {
|
|
550
|
+
console.error(
|
|
551
|
+
` ${logSymbols3.error} Node ${majorNodeVersion} is not supported. Please upgrade to Node 18 or higher.`
|
|
552
|
+
);
|
|
553
|
+
process.exit(1);
|
|
554
|
+
}
|
|
555
|
+
const previewServerLocation = await getPreviewServerLocation();
|
|
556
|
+
const previewServer = createJiti2(previewServerLocation);
|
|
557
|
+
const { default: next } = await previewServer.import("next");
|
|
558
|
+
devServer = http.createServer((req, res) => {
|
|
559
|
+
if (!req.url) {
|
|
560
|
+
res.end(404);
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
const parsedUrl = url2.parse(req.url, true);
|
|
564
|
+
res.setHeader(
|
|
565
|
+
"Cache-Control",
|
|
566
|
+
"no-cache, max-age=0, must-revalidate, no-store"
|
|
567
|
+
);
|
|
568
|
+
res.setHeader("Pragma", "no-cache");
|
|
569
|
+
res.setHeader("Expires", "-1");
|
|
570
|
+
try {
|
|
571
|
+
if (parsedUrl.path?.includes("static/") && !parsedUrl.path.includes("_next/static/")) {
|
|
572
|
+
void serveStaticFile(res, parsedUrl, staticBaseDirRelativePath);
|
|
573
|
+
} else if (!isNextReady) {
|
|
574
|
+
void nextReadyPromise.then(
|
|
575
|
+
() => nextHandleRequest?.(req, res, parsedUrl)
|
|
576
|
+
);
|
|
577
|
+
} else {
|
|
578
|
+
void nextHandleRequest?.(req, res, parsedUrl);
|
|
579
|
+
}
|
|
580
|
+
} catch (e) {
|
|
581
|
+
console.error("caught error", e);
|
|
582
|
+
res.writeHead(500);
|
|
583
|
+
res.end();
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
const { portAlreadyInUse } = await safeAsyncServerListen(devServer, port);
|
|
587
|
+
if (!portAlreadyInUse) {
|
|
588
|
+
console.log(chalk.greenBright(` React Email ${package_default.version}`));
|
|
589
|
+
console.log(` Running preview at: http://localhost:${port}
|
|
590
|
+
`);
|
|
591
|
+
} else {
|
|
592
|
+
const nextPortToTry = port + 1;
|
|
593
|
+
console.warn(
|
|
594
|
+
` ${logSymbols3.warning} Port ${port} is already in use, trying ${nextPortToTry}`
|
|
595
|
+
);
|
|
596
|
+
return startDevServer(
|
|
597
|
+
emailsDirRelativePath,
|
|
598
|
+
staticBaseDirRelativePath,
|
|
599
|
+
nextPortToTry
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
devServer.on("close", async () => {
|
|
603
|
+
await app.close();
|
|
604
|
+
});
|
|
605
|
+
devServer.on("error", (e) => {
|
|
606
|
+
spinner.stopAndPersist({
|
|
607
|
+
symbol: logSymbols3.error,
|
|
608
|
+
text: `Preview Server had an error: ${e}`
|
|
609
|
+
});
|
|
610
|
+
process.exit(1);
|
|
611
|
+
});
|
|
612
|
+
const spinner = ora2({
|
|
613
|
+
text: "Getting react-email preview server ready...\n",
|
|
614
|
+
prefixText: " "
|
|
615
|
+
}).start();
|
|
616
|
+
registerSpinnerAutostopping(spinner);
|
|
617
|
+
const timeBeforeNextReady = performance.now();
|
|
618
|
+
process.env = {
|
|
619
|
+
NODE_ENV: "development",
|
|
620
|
+
...process.env,
|
|
621
|
+
...getEnvVariablesForPreviewApp(
|
|
622
|
+
// If we don't do normalization here, stuff like https://github.com/resend/react-email/issues/1354 happens.
|
|
623
|
+
path6.normalize(emailsDirRelativePath),
|
|
624
|
+
previewServerLocation,
|
|
625
|
+
process.cwd()
|
|
626
|
+
)
|
|
627
|
+
};
|
|
628
|
+
const app = next({
|
|
629
|
+
// passing in env here does not get the environment variables there
|
|
630
|
+
dev: isDev,
|
|
631
|
+
conf: {
|
|
632
|
+
images: {
|
|
633
|
+
// This is to avoid the warning with sharp
|
|
634
|
+
unoptimized: true
|
|
635
|
+
}
|
|
636
|
+
},
|
|
637
|
+
hostname: "localhost",
|
|
638
|
+
port,
|
|
639
|
+
dir: previewServerLocation
|
|
640
|
+
});
|
|
641
|
+
let isNextReady = false;
|
|
642
|
+
const nextReadyPromise = app.prepare();
|
|
643
|
+
try {
|
|
644
|
+
await nextReadyPromise;
|
|
645
|
+
} catch (exception) {
|
|
646
|
+
spinner.stopAndPersist({
|
|
647
|
+
symbol: logSymbols3.error,
|
|
648
|
+
text: ` Preview Server had an error: ${exception}`
|
|
649
|
+
});
|
|
650
|
+
process.exit(1);
|
|
651
|
+
}
|
|
652
|
+
isNextReady = true;
|
|
653
|
+
const nextHandleRequest = app.getRequestHandler();
|
|
654
|
+
const secondsToNextReady = ((performance.now() - timeBeforeNextReady) / 1e3).toFixed(1);
|
|
655
|
+
spinner.stopAndPersist({
|
|
656
|
+
text: `Ready in ${secondsToNextReady}s
|
|
657
|
+
`,
|
|
658
|
+
symbol: logSymbols3.success
|
|
659
|
+
});
|
|
660
|
+
return devServer;
|
|
661
|
+
};
|
|
662
|
+
var makeExitHandler = (options) => (codeSignalOrError) => {
|
|
663
|
+
if (typeof devServer !== "undefined") {
|
|
664
|
+
console.log("\nshutting down dev server");
|
|
665
|
+
devServer.close();
|
|
666
|
+
devServer = void 0;
|
|
667
|
+
}
|
|
668
|
+
if (codeSignalOrError instanceof Error) {
|
|
669
|
+
console.error(codeSignalOrError);
|
|
670
|
+
}
|
|
671
|
+
if (options?.shouldKillProcess) {
|
|
672
|
+
process.exit(options.killWithErrorCode ? 1 : 0);
|
|
673
|
+
}
|
|
674
|
+
};
|
|
675
|
+
process.on("exit", makeExitHandler());
|
|
676
|
+
process.on(
|
|
677
|
+
"SIGINT",
|
|
678
|
+
makeExitHandler({ shouldKillProcess: true, killWithErrorCode: false })
|
|
679
|
+
);
|
|
680
|
+
process.on(
|
|
681
|
+
"SIGUSR1",
|
|
682
|
+
makeExitHandler({ shouldKillProcess: true, killWithErrorCode: false })
|
|
683
|
+
);
|
|
684
|
+
process.on(
|
|
685
|
+
"SIGUSR2",
|
|
686
|
+
makeExitHandler({ shouldKillProcess: true, killWithErrorCode: false })
|
|
687
|
+
);
|
|
688
|
+
process.on(
|
|
689
|
+
"uncaughtException",
|
|
690
|
+
makeExitHandler({ shouldKillProcess: true, killWithErrorCode: true })
|
|
691
|
+
);
|
|
692
|
+
|
|
693
|
+
// src/utils/preview/hot-reloading/get-imported-modules.ts
|
|
694
|
+
import { parse } from "@babel/parser";
|
|
695
|
+
import traverseModule from "@babel/traverse";
|
|
696
|
+
var traverse = (
|
|
697
|
+
// we keep this check here so that this still works with the dev:preview
|
|
698
|
+
// script's use of tsx
|
|
699
|
+
typeof traverseModule === "function" ? traverseModule : traverseModule.default
|
|
700
|
+
);
|
|
701
|
+
var getImportedModules = (contents) => {
|
|
702
|
+
const importedPaths = [];
|
|
703
|
+
const parsedContents = parse(contents, {
|
|
704
|
+
sourceType: "unambiguous",
|
|
705
|
+
strictMode: false,
|
|
706
|
+
errorRecovery: true,
|
|
707
|
+
plugins: ["jsx", "typescript", "decorators"]
|
|
708
|
+
});
|
|
709
|
+
traverse(parsedContents, {
|
|
710
|
+
ImportDeclaration({ node }) {
|
|
711
|
+
importedPaths.push(node.source.value);
|
|
712
|
+
},
|
|
713
|
+
ExportAllDeclaration({ node }) {
|
|
714
|
+
importedPaths.push(node.source.value);
|
|
715
|
+
},
|
|
716
|
+
ExportNamedDeclaration({ node }) {
|
|
717
|
+
if (node.source) {
|
|
718
|
+
importedPaths.push(node.source.value);
|
|
719
|
+
}
|
|
720
|
+
},
|
|
721
|
+
TSExternalModuleReference({ node }) {
|
|
722
|
+
importedPaths.push(node.expression.value);
|
|
723
|
+
},
|
|
724
|
+
CallExpression({ node }) {
|
|
725
|
+
if ("name" in node.callee && node.callee.name === "require") {
|
|
726
|
+
if (node.arguments.length === 1) {
|
|
727
|
+
const importPathNode = node.arguments[0];
|
|
728
|
+
if (importPathNode.type === "StringLiteral") {
|
|
729
|
+
importedPaths.push(importPathNode.value);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
});
|
|
735
|
+
return importedPaths;
|
|
736
|
+
};
|
|
737
|
+
|
|
738
|
+
// src/utils/preview/hot-reloading/resolve-path-aliases.ts
|
|
739
|
+
import path7 from "node:path";
|
|
740
|
+
import { createMatchPath, loadConfig } from "tsconfig-paths";
|
|
741
|
+
var resolvePathAliases = (importPaths, projectPath) => {
|
|
742
|
+
const configLoadResult = loadConfig(projectPath);
|
|
743
|
+
if (configLoadResult.resultType === "success") {
|
|
744
|
+
const matchPath = createMatchPath(
|
|
745
|
+
configLoadResult.absoluteBaseUrl,
|
|
746
|
+
configLoadResult.paths
|
|
747
|
+
);
|
|
748
|
+
return importPaths.map((importedPath) => {
|
|
749
|
+
const unaliasedPath = matchPath(importedPath, void 0, void 0, [
|
|
750
|
+
".tsx",
|
|
751
|
+
".ts",
|
|
752
|
+
".js",
|
|
753
|
+
".jsx",
|
|
754
|
+
".cjs",
|
|
755
|
+
".mjs"
|
|
756
|
+
]);
|
|
757
|
+
if (unaliasedPath) {
|
|
758
|
+
return `./${path7.relative(projectPath, unaliasedPath)}`;
|
|
759
|
+
}
|
|
760
|
+
return importedPath;
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
return importPaths;
|
|
764
|
+
};
|
|
765
|
+
|
|
766
|
+
// src/utils/preview/hot-reloading/create-dependency-graph.ts
|
|
767
|
+
var readAllFilesInsideDirectory = async (directory) => {
|
|
768
|
+
let allFilePaths = [];
|
|
769
|
+
const topLevelDirents = await fs4.readdir(directory, { withFileTypes: true });
|
|
770
|
+
for await (const dirent of topLevelDirents) {
|
|
771
|
+
const pathToDirent = path8.join(directory, dirent.name);
|
|
772
|
+
if (dirent.isDirectory()) {
|
|
773
|
+
allFilePaths = allFilePaths.concat(
|
|
774
|
+
await readAllFilesInsideDirectory(pathToDirent)
|
|
775
|
+
);
|
|
776
|
+
} else {
|
|
777
|
+
allFilePaths.push(pathToDirent);
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
return allFilePaths;
|
|
781
|
+
};
|
|
782
|
+
var isJavascriptModule = (filePath) => {
|
|
783
|
+
const extensionName = path8.extname(filePath);
|
|
784
|
+
return [".js", ".ts", ".jsx", ".tsx", ".mjs", ".cjs"].includes(extensionName);
|
|
785
|
+
};
|
|
786
|
+
var checkFileExtensionsUntilItExists = (pathWithoutExtension) => {
|
|
787
|
+
if (existsSync2(`${pathWithoutExtension}.ts`)) {
|
|
788
|
+
return `${pathWithoutExtension}.ts`;
|
|
789
|
+
}
|
|
790
|
+
if (existsSync2(`${pathWithoutExtension}.tsx`)) {
|
|
791
|
+
return `${pathWithoutExtension}.tsx`;
|
|
792
|
+
}
|
|
793
|
+
if (existsSync2(`${pathWithoutExtension}.js`)) {
|
|
794
|
+
return `${pathWithoutExtension}.js`;
|
|
795
|
+
}
|
|
796
|
+
if (existsSync2(`${pathWithoutExtension}.jsx`)) {
|
|
797
|
+
return `${pathWithoutExtension}.jsx`;
|
|
798
|
+
}
|
|
799
|
+
if (existsSync2(`${pathWithoutExtension}.mjs`)) {
|
|
800
|
+
return `${pathWithoutExtension}.mjs`;
|
|
801
|
+
}
|
|
802
|
+
if (existsSync2(`${pathWithoutExtension}.cjs`)) {
|
|
803
|
+
return `${pathWithoutExtension}.cjs`;
|
|
804
|
+
}
|
|
805
|
+
};
|
|
806
|
+
var createDependencyGraph = async (directory) => {
|
|
807
|
+
const filePaths = await readAllFilesInsideDirectory(directory);
|
|
808
|
+
const modulePaths = filePaths.filter(isJavascriptModule);
|
|
809
|
+
const graph = Object.fromEntries(
|
|
810
|
+
modulePaths.map((path14) => [
|
|
811
|
+
path14,
|
|
812
|
+
{
|
|
813
|
+
path: path14,
|
|
814
|
+
dependencyPaths: [],
|
|
815
|
+
dependentPaths: [],
|
|
816
|
+
moduleDependencies: []
|
|
817
|
+
}
|
|
818
|
+
])
|
|
819
|
+
);
|
|
820
|
+
const getDependencyPaths = async (filePath) => {
|
|
821
|
+
const contents = await fs4.readFile(filePath, "utf8");
|
|
822
|
+
const importedPaths = isJavascriptModule(filePath) ? resolvePathAliases(getImportedModules(contents), path8.dirname(filePath)) : [];
|
|
823
|
+
const importedPathsRelativeToDirectory = importedPaths.map(
|
|
824
|
+
(dependencyPath) => {
|
|
825
|
+
const isModulePath = !dependencyPath.startsWith(".");
|
|
826
|
+
if (isModulePath || path8.isAbsolute(dependencyPath)) {
|
|
827
|
+
return dependencyPath;
|
|
828
|
+
}
|
|
829
|
+
let pathToDependencyFromDirectory = path8.resolve(
|
|
830
|
+
/*
|
|
831
|
+
path.resolve resolves paths differently from what imports on javascript do.
|
|
832
|
+
|
|
833
|
+
So if we wouldn't do this, for an email at "/path/to/email.tsx" with a dependency path of "./other-email"
|
|
834
|
+
would end up going into /path/to/email.tsx/other-email instead of /path/to/other-email which is the
|
|
835
|
+
one the import is meant to go to
|
|
836
|
+
*/
|
|
837
|
+
path8.dirname(filePath),
|
|
838
|
+
dependencyPath
|
|
839
|
+
);
|
|
840
|
+
let isDirectory = false;
|
|
841
|
+
try {
|
|
842
|
+
isDirectory = statSync(pathToDependencyFromDirectory).isDirectory();
|
|
843
|
+
} catch (_) {
|
|
844
|
+
}
|
|
845
|
+
if (isDirectory) {
|
|
846
|
+
const pathToSubDirectory = pathToDependencyFromDirectory;
|
|
847
|
+
const pathWithExtension = checkFileExtensionsUntilItExists(
|
|
848
|
+
`${pathToSubDirectory}/index`
|
|
849
|
+
);
|
|
850
|
+
if (pathWithExtension) {
|
|
851
|
+
pathToDependencyFromDirectory = pathWithExtension;
|
|
852
|
+
} else if (isDev) {
|
|
853
|
+
console.warn(
|
|
854
|
+
`Could not find index file for directory at ${pathToDependencyFromDirectory}. This is probably going to cause issues with both hot reloading and your code.`
|
|
855
|
+
);
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
const extension = path8.extname(pathToDependencyFromDirectory);
|
|
859
|
+
const pathWithEnsuredExtension = (() => {
|
|
860
|
+
if (extension.length > 0 && existsSync2(pathToDependencyFromDirectory)) {
|
|
861
|
+
return pathToDependencyFromDirectory;
|
|
862
|
+
}
|
|
863
|
+
return checkFileExtensionsUntilItExists(
|
|
864
|
+
pathToDependencyFromDirectory.replace(extension, "")
|
|
865
|
+
);
|
|
866
|
+
})();
|
|
867
|
+
if (pathWithEnsuredExtension) {
|
|
868
|
+
pathToDependencyFromDirectory = pathWithEnsuredExtension;
|
|
869
|
+
} else if (isDev) {
|
|
870
|
+
console.warn(
|
|
871
|
+
`Could not find file at ${pathToDependencyFromDirectory}`
|
|
872
|
+
);
|
|
873
|
+
}
|
|
874
|
+
return pathToDependencyFromDirectory;
|
|
875
|
+
}
|
|
876
|
+
);
|
|
877
|
+
const moduleDependencies = importedPathsRelativeToDirectory.filter(
|
|
878
|
+
(dependencyPath) => !dependencyPath.startsWith(".") && !path8.isAbsolute(dependencyPath)
|
|
879
|
+
);
|
|
880
|
+
const nonNodeModuleImportPathsRelativeToDirectory = importedPathsRelativeToDirectory.filter(
|
|
881
|
+
(dependencyPath) => dependencyPath.startsWith(".") || path8.isAbsolute(dependencyPath)
|
|
882
|
+
);
|
|
883
|
+
return {
|
|
884
|
+
dependencyPaths: nonNodeModuleImportPathsRelativeToDirectory,
|
|
885
|
+
moduleDependencies
|
|
886
|
+
};
|
|
887
|
+
};
|
|
888
|
+
const updateModuleDependenciesInGraph = async (moduleFilePath) => {
|
|
889
|
+
if (graph[moduleFilePath] === void 0) {
|
|
890
|
+
graph[moduleFilePath] = {
|
|
891
|
+
path: moduleFilePath,
|
|
892
|
+
dependencyPaths: [],
|
|
893
|
+
dependentPaths: [],
|
|
894
|
+
moduleDependencies: []
|
|
895
|
+
};
|
|
896
|
+
}
|
|
897
|
+
const { moduleDependencies, dependencyPaths: newDependencyPaths } = await getDependencyPaths(moduleFilePath);
|
|
898
|
+
graph[moduleFilePath].moduleDependencies = moduleDependencies;
|
|
899
|
+
for (const dependencyPath of graph[moduleFilePath].dependencyPaths) {
|
|
900
|
+
if (newDependencyPaths.includes(dependencyPath)) continue;
|
|
901
|
+
const dependencyModule = graph[dependencyPath];
|
|
902
|
+
if (dependencyModule !== void 0) {
|
|
903
|
+
dependencyModule.dependentPaths = dependencyModule.dependentPaths.filter(
|
|
904
|
+
(dependentPath) => dependentPath !== moduleFilePath
|
|
905
|
+
);
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
graph[moduleFilePath].dependencyPaths = newDependencyPaths;
|
|
909
|
+
for await (const dependencyPath of newDependencyPaths) {
|
|
910
|
+
if (graph[dependencyPath] === void 0) {
|
|
911
|
+
await updateModuleDependenciesInGraph(dependencyPath);
|
|
912
|
+
}
|
|
913
|
+
const dependencyModule = graph[dependencyPath];
|
|
914
|
+
if (dependencyModule === void 0) {
|
|
915
|
+
throw new Error(
|
|
916
|
+
`Loading the dependency path ${dependencyPath} did not initialize it at all. This is a bug in React Email.`
|
|
917
|
+
);
|
|
918
|
+
}
|
|
919
|
+
if (!dependencyModule.dependentPaths.includes(moduleFilePath)) {
|
|
920
|
+
dependencyModule.dependentPaths.push(moduleFilePath);
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
};
|
|
924
|
+
for (const filePath of modulePaths) {
|
|
925
|
+
await updateModuleDependenciesInGraph(filePath);
|
|
926
|
+
}
|
|
927
|
+
const removeModuleFromGraph = (filePath) => {
|
|
928
|
+
const module = graph[filePath];
|
|
929
|
+
if (module) {
|
|
930
|
+
for (const dependencyPath of module.dependencyPaths) {
|
|
931
|
+
if (graph[dependencyPath]) {
|
|
932
|
+
graph[dependencyPath].dependentPaths = graph[dependencyPath].dependentPaths.filter(
|
|
933
|
+
(dependentPath) => dependentPath !== filePath
|
|
934
|
+
);
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
delete graph[filePath];
|
|
938
|
+
}
|
|
939
|
+
};
|
|
940
|
+
return [
|
|
941
|
+
graph,
|
|
942
|
+
async (event, pathToModified) => {
|
|
943
|
+
switch (event) {
|
|
944
|
+
case "change":
|
|
945
|
+
if (isJavascriptModule(pathToModified)) {
|
|
946
|
+
await updateModuleDependenciesInGraph(pathToModified);
|
|
947
|
+
}
|
|
948
|
+
break;
|
|
949
|
+
case "add":
|
|
950
|
+
if (isJavascriptModule(pathToModified)) {
|
|
951
|
+
await updateModuleDependenciesInGraph(pathToModified);
|
|
952
|
+
}
|
|
953
|
+
break;
|
|
954
|
+
case "addDir": {
|
|
955
|
+
const filesInsideAddedDirectory = await readAllFilesInsideDirectory(pathToModified);
|
|
956
|
+
const modulesInsideAddedDirectory = filesInsideAddedDirectory.filter(isJavascriptModule);
|
|
957
|
+
for await (const filePath of modulesInsideAddedDirectory) {
|
|
958
|
+
await updateModuleDependenciesInGraph(filePath);
|
|
959
|
+
}
|
|
960
|
+
break;
|
|
961
|
+
}
|
|
962
|
+
case "unlink":
|
|
963
|
+
if (isJavascriptModule(pathToModified)) {
|
|
964
|
+
removeModuleFromGraph(pathToModified);
|
|
965
|
+
}
|
|
966
|
+
break;
|
|
967
|
+
case "unlinkDir": {
|
|
968
|
+
const filesInsideDeletedDirectory = await readAllFilesInsideDirectory(pathToModified);
|
|
969
|
+
const modulesInsideDeletedDirectory = filesInsideDeletedDirectory.filter(isJavascriptModule);
|
|
970
|
+
for await (const filePath of modulesInsideDeletedDirectory) {
|
|
971
|
+
removeModuleFromGraph(filePath);
|
|
972
|
+
}
|
|
973
|
+
break;
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
},
|
|
977
|
+
{
|
|
978
|
+
resolveDependentsOf: function resolveDependentsOf(pathToModule) {
|
|
979
|
+
const moduleEntry = graph[pathToModule];
|
|
980
|
+
const dependentPaths = [];
|
|
981
|
+
if (moduleEntry) {
|
|
982
|
+
for (const dependentPath of moduleEntry.dependentPaths) {
|
|
983
|
+
const dependentsOfDependent = resolveDependentsOf(dependentPath);
|
|
984
|
+
dependentPaths.push(...dependentsOfDependent);
|
|
985
|
+
dependentPaths.push(dependentPath);
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
return dependentPaths;
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
];
|
|
992
|
+
};
|
|
993
|
+
|
|
994
|
+
// src/utils/preview/hot-reloading/setup-hot-reloading.ts
|
|
995
|
+
var setupHotreloading = async (devServer2, emailDirRelativePath) => {
|
|
996
|
+
let clients = [];
|
|
997
|
+
const io = new SocketServer(devServer2);
|
|
998
|
+
io.on("connection", (client) => {
|
|
999
|
+
clients.push(client);
|
|
1000
|
+
client.on("disconnect", () => {
|
|
1001
|
+
clients = clients.filter((item) => item !== client);
|
|
1002
|
+
});
|
|
1003
|
+
});
|
|
1004
|
+
let changes = [];
|
|
1005
|
+
const reload = debounce(() => {
|
|
1006
|
+
clients.forEach((client) => {
|
|
1007
|
+
client.emit(
|
|
1008
|
+
"reload",
|
|
1009
|
+
changes.filter(
|
|
1010
|
+
(change) => (
|
|
1011
|
+
// Ensures only changes inside the emails directory are emitted
|
|
1012
|
+
path9.resolve(absolutePathToEmailsDirectory, change.filename).startsWith(absolutePathToEmailsDirectory)
|
|
1013
|
+
)
|
|
1014
|
+
)
|
|
1015
|
+
);
|
|
1016
|
+
});
|
|
1017
|
+
changes = [];
|
|
1018
|
+
}, 150);
|
|
1019
|
+
const absolutePathToEmailsDirectory = path9.resolve(
|
|
1020
|
+
process.cwd(),
|
|
1021
|
+
emailDirRelativePath
|
|
1022
|
+
);
|
|
1023
|
+
const [dependencyGraph, updateDependencyGraph, { resolveDependentsOf }] = await createDependencyGraph(absolutePathToEmailsDirectory);
|
|
1024
|
+
const watcher = watch("", {
|
|
1025
|
+
ignoreInitial: true,
|
|
1026
|
+
cwd: absolutePathToEmailsDirectory
|
|
1027
|
+
});
|
|
1028
|
+
const getFilesOutsideEmailsDirectory = () => Object.keys(dependencyGraph).filter(
|
|
1029
|
+
(p) => path9.relative(absolutePathToEmailsDirectory, p).startsWith("..")
|
|
1030
|
+
);
|
|
1031
|
+
let filesOutsideEmailsDirectory = getFilesOutsideEmailsDirectory();
|
|
1032
|
+
for (const p of filesOutsideEmailsDirectory) {
|
|
1033
|
+
watcher.add(p);
|
|
1034
|
+
}
|
|
1035
|
+
const exit = async () => {
|
|
1036
|
+
await watcher.close();
|
|
1037
|
+
};
|
|
1038
|
+
process.on("SIGINT", exit);
|
|
1039
|
+
process.on("uncaughtException", exit);
|
|
1040
|
+
watcher.on("all", async (event, relativePathToChangeTarget) => {
|
|
1041
|
+
const file = relativePathToChangeTarget.split(path9.sep);
|
|
1042
|
+
if (file.length === 0) {
|
|
1043
|
+
return;
|
|
1044
|
+
}
|
|
1045
|
+
const pathToChangeTarget = path9.resolve(
|
|
1046
|
+
absolutePathToEmailsDirectory,
|
|
1047
|
+
relativePathToChangeTarget
|
|
1048
|
+
);
|
|
1049
|
+
await updateDependencyGraph(event, pathToChangeTarget);
|
|
1050
|
+
const newFilesOutsideEmailsDirectory = getFilesOutsideEmailsDirectory();
|
|
1051
|
+
for (const p of filesOutsideEmailsDirectory) {
|
|
1052
|
+
if (!newFilesOutsideEmailsDirectory.includes(p)) {
|
|
1053
|
+
watcher.unwatch(p);
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
for (const p of newFilesOutsideEmailsDirectory) {
|
|
1057
|
+
if (!filesOutsideEmailsDirectory.includes(p)) {
|
|
1058
|
+
watcher.add(p);
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
filesOutsideEmailsDirectory = newFilesOutsideEmailsDirectory;
|
|
1062
|
+
changes.push({
|
|
1063
|
+
event,
|
|
1064
|
+
filename: relativePathToChangeTarget
|
|
1065
|
+
});
|
|
1066
|
+
for (const dependentPath of resolveDependentsOf(pathToChangeTarget)) {
|
|
1067
|
+
changes.push({
|
|
1068
|
+
event: "change",
|
|
1069
|
+
filename: path9.relative(absolutePathToEmailsDirectory, dependentPath)
|
|
1070
|
+
});
|
|
1071
|
+
}
|
|
1072
|
+
reload();
|
|
1073
|
+
});
|
|
1074
|
+
return watcher;
|
|
1075
|
+
};
|
|
1076
|
+
|
|
1077
|
+
// src/utils/tree.ts
|
|
1078
|
+
import { promises as fs5 } from "node:fs";
|
|
1079
|
+
import os from "node:os";
|
|
1080
|
+
import path10 from "node:path";
|
|
1081
|
+
var SYMBOLS = {
|
|
1082
|
+
BRANCH: "\u251C\u2500\u2500 ",
|
|
1083
|
+
EMPTY: "",
|
|
1084
|
+
INDENT: " ",
|
|
1085
|
+
LAST_BRANCH: "\u2514\u2500\u2500 ",
|
|
1086
|
+
VERTICAL: "\u2502 "
|
|
1087
|
+
};
|
|
1088
|
+
var getTreeLines = async (dirPath, depth, currentDepth = 0) => {
|
|
1089
|
+
const base = process.cwd();
|
|
1090
|
+
const dirFullpath = path10.resolve(base, dirPath);
|
|
1091
|
+
const dirname2 = path10.basename(dirFullpath);
|
|
1092
|
+
let lines = [dirname2];
|
|
1093
|
+
const dirStat = await fs5.stat(dirFullpath);
|
|
1094
|
+
if (dirStat.isDirectory() && currentDepth < depth) {
|
|
1095
|
+
const childDirents = await fs5.readdir(dirFullpath, { withFileTypes: true });
|
|
1096
|
+
childDirents.sort((a, b) => {
|
|
1097
|
+
if (a.isDirectory() && b.isFile()) {
|
|
1098
|
+
return -1;
|
|
1099
|
+
}
|
|
1100
|
+
if (a.isFile() && b.isDirectory()) {
|
|
1101
|
+
return 1;
|
|
1102
|
+
}
|
|
1103
|
+
return b.name > a.name ? -1 : 1;
|
|
1104
|
+
});
|
|
1105
|
+
for (let i = 0; i < childDirents.length; i++) {
|
|
1106
|
+
const dirent = childDirents[i];
|
|
1107
|
+
const isLast = i === childDirents.length - 1;
|
|
1108
|
+
const branchingSymbol = isLast ? SYMBOLS.LAST_BRANCH : SYMBOLS.BRANCH;
|
|
1109
|
+
const verticalSymbol = isLast ? SYMBOLS.INDENT : SYMBOLS.VERTICAL;
|
|
1110
|
+
if (dirent.isFile()) {
|
|
1111
|
+
lines.push(`${branchingSymbol}${dirent.name}`);
|
|
1112
|
+
} else {
|
|
1113
|
+
const pathToDirectory = path10.join(dirFullpath, dirent.name);
|
|
1114
|
+
const treeLinesForSubDirectory = await getTreeLines(
|
|
1115
|
+
pathToDirectory,
|
|
1116
|
+
depth,
|
|
1117
|
+
currentDepth + 1
|
|
1118
|
+
);
|
|
1119
|
+
lines = lines.concat(
|
|
1120
|
+
treeLinesForSubDirectory.map(
|
|
1121
|
+
(line, index) => index === 0 ? `${branchingSymbol}${line}` : `${verticalSymbol}${line}`
|
|
1122
|
+
)
|
|
1123
|
+
);
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
return lines;
|
|
1128
|
+
};
|
|
1129
|
+
var tree = async (dirPath, depth) => {
|
|
1130
|
+
const lines = await getTreeLines(dirPath, depth);
|
|
1131
|
+
return lines.join(os.EOL);
|
|
1132
|
+
};
|
|
1133
|
+
|
|
1134
|
+
// src/commands/dev.ts
|
|
1135
|
+
var dev = async ({ dir: emailsDirRelativePath, port }) => {
|
|
1136
|
+
try {
|
|
1137
|
+
if (!fs6.existsSync(emailsDirRelativePath)) {
|
|
1138
|
+
console.error(`Missing ${emailsDirRelativePath} folder`);
|
|
1139
|
+
process.exit(1);
|
|
1140
|
+
}
|
|
1141
|
+
const devServer2 = await startDevServer(
|
|
1142
|
+
emailsDirRelativePath,
|
|
1143
|
+
emailsDirRelativePath,
|
|
1144
|
+
// defaults to ./emails/static for the static files that are served to the preview
|
|
1145
|
+
Number.parseInt(port)
|
|
1146
|
+
);
|
|
1147
|
+
await setupHotreloading(devServer2, emailsDirRelativePath);
|
|
1148
|
+
} catch (error) {
|
|
1149
|
+
console.log(error);
|
|
1150
|
+
process.exit(1);
|
|
1151
|
+
}
|
|
1152
|
+
};
|
|
1153
|
+
|
|
1154
|
+
// src/commands/export.ts
|
|
1155
|
+
import fs8, { unlinkSync, writeFileSync } from "node:fs";
|
|
1156
|
+
import { createRequire } from "node:module";
|
|
1157
|
+
import path12 from "node:path";
|
|
1158
|
+
import url3 from "node:url";
|
|
1159
|
+
import { build as build2 } from "esbuild";
|
|
1160
|
+
import { glob } from "glob";
|
|
1161
|
+
import logSymbols4 from "log-symbols";
|
|
1162
|
+
import normalize from "normalize-path";
|
|
1163
|
+
import ora3 from "ora";
|
|
1164
|
+
|
|
1165
|
+
// src/utils/esbuild/renderring-utilities-exporter.ts
|
|
1166
|
+
import { promises as fs7 } from "node:fs";
|
|
1167
|
+
import path11 from "node:path";
|
|
1168
|
+
|
|
1169
|
+
// src/utils/esbuild/escape-string-for-regex.ts
|
|
1170
|
+
function escapeStringForRegex(string) {
|
|
1171
|
+
return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
// src/utils/esbuild/renderring-utilities-exporter.ts
|
|
1175
|
+
var renderingUtilitiesExporter = (emailTemplates) => ({
|
|
1176
|
+
name: "rendering-utilities-exporter",
|
|
1177
|
+
setup: (b) => {
|
|
1178
|
+
b.onLoad(
|
|
1179
|
+
{
|
|
1180
|
+
filter: new RegExp(
|
|
1181
|
+
emailTemplates.map((emailPath) => escapeStringForRegex(emailPath)).join("|")
|
|
1182
|
+
)
|
|
1183
|
+
},
|
|
1184
|
+
async ({ path: pathToFile }) => {
|
|
1185
|
+
return {
|
|
1186
|
+
contents: `${await fs7.readFile(pathToFile, "utf8")};
|
|
1187
|
+
export { render } from 'react-email-module-that-will-export-render'
|
|
1188
|
+
export { createElement as reactEmailCreateReactElement } from 'react';
|
|
1189
|
+
`,
|
|
1190
|
+
loader: path11.extname(pathToFile).slice(1)
|
|
1191
|
+
};
|
|
1192
|
+
}
|
|
1193
|
+
);
|
|
1194
|
+
b.onResolve(
|
|
1195
|
+
{ filter: /^react-email-module-that-will-export-render$/ },
|
|
1196
|
+
async (args) => {
|
|
1197
|
+
const options = {
|
|
1198
|
+
kind: "import-statement",
|
|
1199
|
+
importer: args.importer,
|
|
1200
|
+
resolveDir: args.resolveDir,
|
|
1201
|
+
namespace: args.namespace
|
|
1202
|
+
};
|
|
1203
|
+
let result = await b.resolve("@react-email/render", options);
|
|
1204
|
+
if (result.errors.length === 0) {
|
|
1205
|
+
return result;
|
|
1206
|
+
}
|
|
1207
|
+
result = await b.resolve("@react-email/components", options);
|
|
1208
|
+
if (result.errors.length > 0 && result.errors[0]) {
|
|
1209
|
+
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?";
|
|
1210
|
+
}
|
|
1211
|
+
return result;
|
|
1212
|
+
}
|
|
1213
|
+
);
|
|
1214
|
+
}
|
|
1215
|
+
});
|
|
1216
|
+
|
|
1217
|
+
// src/commands/export.ts
|
|
1218
|
+
var getEmailTemplatesFromDirectory = (emailDirectory) => {
|
|
1219
|
+
const templatePaths = [];
|
|
1220
|
+
emailDirectory.emailFilenames.forEach(
|
|
1221
|
+
(filename3) => templatePaths.push(path12.join(emailDirectory.absolutePath, filename3))
|
|
1222
|
+
);
|
|
1223
|
+
emailDirectory.subDirectories.forEach((directory) => {
|
|
1224
|
+
templatePaths.push(...getEmailTemplatesFromDirectory(directory));
|
|
1225
|
+
});
|
|
1226
|
+
return templatePaths;
|
|
1227
|
+
};
|
|
1228
|
+
var filename2 = url3.fileURLToPath(import.meta.url);
|
|
1229
|
+
var require2 = createRequire(filename2);
|
|
1230
|
+
var exportTemplates = async (pathToWhereEmailMarkupShouldBeDumped, emailsDirectoryPath, options) => {
|
|
1231
|
+
if (fs8.existsSync(pathToWhereEmailMarkupShouldBeDumped)) {
|
|
1232
|
+
fs8.rmSync(pathToWhereEmailMarkupShouldBeDumped, { recursive: true });
|
|
1233
|
+
}
|
|
1234
|
+
let spinner;
|
|
1235
|
+
if (!options.silent) {
|
|
1236
|
+
spinner = ora3("Preparing files...\n").start();
|
|
1237
|
+
registerSpinnerAutostopping(spinner);
|
|
1238
|
+
}
|
|
1239
|
+
const emailsDirectoryMetadata = await getEmailsDirectoryMetadata(
|
|
1240
|
+
path12.resolve(process.cwd(), emailsDirectoryPath),
|
|
1241
|
+
true
|
|
1242
|
+
);
|
|
1243
|
+
if (typeof emailsDirectoryMetadata === "undefined") {
|
|
1244
|
+
if (spinner) {
|
|
1245
|
+
spinner.stopAndPersist({
|
|
1246
|
+
symbol: logSymbols4.error,
|
|
1247
|
+
text: `Could not find the directory at ${emailsDirectoryPath}`
|
|
1248
|
+
});
|
|
1249
|
+
}
|
|
1250
|
+
return;
|
|
1251
|
+
}
|
|
1252
|
+
const allTemplates = getEmailTemplatesFromDirectory(emailsDirectoryMetadata);
|
|
1253
|
+
try {
|
|
1254
|
+
await build2({
|
|
1255
|
+
bundle: true,
|
|
1256
|
+
entryPoints: allTemplates,
|
|
1257
|
+
format: "cjs",
|
|
1258
|
+
jsx: "transform",
|
|
1259
|
+
loader: { ".js": "jsx" },
|
|
1260
|
+
logLevel: "silent",
|
|
1261
|
+
outExtension: { ".js": ".cjs" },
|
|
1262
|
+
outdir: pathToWhereEmailMarkupShouldBeDumped,
|
|
1263
|
+
platform: "node",
|
|
1264
|
+
plugins: [renderingUtilitiesExporter(allTemplates)],
|
|
1265
|
+
write: true
|
|
1266
|
+
});
|
|
1267
|
+
} catch (exception) {
|
|
1268
|
+
if (spinner) {
|
|
1269
|
+
spinner.stopAndPersist({
|
|
1270
|
+
symbol: logSymbols4.error,
|
|
1271
|
+
text: "Failed to build emails"
|
|
1272
|
+
});
|
|
1273
|
+
}
|
|
1274
|
+
const buildFailure = exception;
|
|
1275
|
+
console.error(`
|
|
1276
|
+
${buildFailure.message}`);
|
|
1277
|
+
process.exit(1);
|
|
1278
|
+
}
|
|
1279
|
+
if (spinner) {
|
|
1280
|
+
spinner.succeed();
|
|
1281
|
+
}
|
|
1282
|
+
const allBuiltTemplates = glob.sync(
|
|
1283
|
+
normalize(`${pathToWhereEmailMarkupShouldBeDumped}/**/*.cjs`),
|
|
1284
|
+
{
|
|
1285
|
+
absolute: true
|
|
1286
|
+
}
|
|
1287
|
+
);
|
|
1288
|
+
for await (const template of allBuiltTemplates) {
|
|
1289
|
+
try {
|
|
1290
|
+
if (spinner) {
|
|
1291
|
+
spinner.text = `rendering ${template.split("/").pop()}`;
|
|
1292
|
+
spinner.render();
|
|
1293
|
+
}
|
|
1294
|
+
delete require2.cache[template];
|
|
1295
|
+
const emailModule = require2(template);
|
|
1296
|
+
const rendered = await emailModule.render(
|
|
1297
|
+
emailModule.reactEmailCreateReactElement(emailModule.default, {}),
|
|
1298
|
+
options
|
|
1299
|
+
);
|
|
1300
|
+
const htmlPath = template.replace(
|
|
1301
|
+
".cjs",
|
|
1302
|
+
options.plainText ? ".txt" : ".html"
|
|
1303
|
+
);
|
|
1304
|
+
writeFileSync(htmlPath, rendered);
|
|
1305
|
+
unlinkSync(template);
|
|
1306
|
+
} catch (exception) {
|
|
1307
|
+
if (spinner) {
|
|
1308
|
+
spinner.stopAndPersist({
|
|
1309
|
+
symbol: logSymbols4.error,
|
|
1310
|
+
text: `failed when rendering ${template.split("/").pop()}`
|
|
1311
|
+
});
|
|
1312
|
+
}
|
|
1313
|
+
console.error(exception);
|
|
1314
|
+
process.exit(1);
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
if (spinner) {
|
|
1318
|
+
spinner.succeed("Rendered all files");
|
|
1319
|
+
spinner.text = "Copying static files";
|
|
1320
|
+
spinner.render();
|
|
1321
|
+
}
|
|
1322
|
+
const staticDirectoryPath = path12.join(emailsDirectoryPath, "static");
|
|
1323
|
+
if (fs8.existsSync(staticDirectoryPath)) {
|
|
1324
|
+
const pathToDumpStaticFilesInto = path12.join(
|
|
1325
|
+
pathToWhereEmailMarkupShouldBeDumped,
|
|
1326
|
+
"static"
|
|
1327
|
+
);
|
|
1328
|
+
if (fs8.existsSync(pathToDumpStaticFilesInto))
|
|
1329
|
+
await fs8.promises.rm(pathToDumpStaticFilesInto, { recursive: true });
|
|
1330
|
+
try {
|
|
1331
|
+
await fs8.promises.cp(staticDirectoryPath, pathToDumpStaticFilesInto, {
|
|
1332
|
+
recursive: true
|
|
1333
|
+
});
|
|
1334
|
+
} catch (exception) {
|
|
1335
|
+
console.error(exception);
|
|
1336
|
+
if (spinner) {
|
|
1337
|
+
spinner.stopAndPersist({
|
|
1338
|
+
symbol: logSymbols4.error,
|
|
1339
|
+
text: "Failed to copy static files"
|
|
1340
|
+
});
|
|
1341
|
+
}
|
|
1342
|
+
console.error(
|
|
1343
|
+
`Something went wrong while copying the file to ${pathToWhereEmailMarkupShouldBeDumped}/static, ${exception}`
|
|
1344
|
+
);
|
|
1345
|
+
process.exit(1);
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
if (spinner && !options.silent) {
|
|
1349
|
+
spinner.succeed();
|
|
1350
|
+
const fileTree = await tree(pathToWhereEmailMarkupShouldBeDumped, 4);
|
|
1351
|
+
console.log(fileTree);
|
|
1352
|
+
spinner.stopAndPersist({
|
|
1353
|
+
symbol: logSymbols4.success,
|
|
1354
|
+
text: "Successfully exported emails"
|
|
1355
|
+
});
|
|
1356
|
+
}
|
|
1357
|
+
};
|
|
1358
|
+
|
|
1359
|
+
// src/commands/start.ts
|
|
1360
|
+
import { spawn as spawn2 } from "node:child_process";
|
|
1361
|
+
import fs9 from "node:fs";
|
|
1362
|
+
import path13 from "node:path";
|
|
1363
|
+
var start = async () => {
|
|
1364
|
+
try {
|
|
1365
|
+
const previewServerLocation = await getPreviewServerLocation();
|
|
1366
|
+
const usersProjectLocation = process.cwd();
|
|
1367
|
+
const builtPreviewPath = path13.resolve(
|
|
1368
|
+
usersProjectLocation,
|
|
1369
|
+
"./.react-email"
|
|
1370
|
+
);
|
|
1371
|
+
if (!fs9.existsSync(builtPreviewPath)) {
|
|
1372
|
+
console.error(
|
|
1373
|
+
"Could not find .react-email, maybe you haven't ran email build?"
|
|
1374
|
+
);
|
|
1375
|
+
process.exit(1);
|
|
1376
|
+
}
|
|
1377
|
+
const nextStart = spawn2("npx", ["next", "start", builtPreviewPath], {
|
|
1378
|
+
cwd: previewServerLocation,
|
|
1379
|
+
stdio: "inherit"
|
|
1380
|
+
});
|
|
1381
|
+
process.on("SIGINT", () => {
|
|
1382
|
+
nextStart.kill("SIGINT");
|
|
1383
|
+
});
|
|
1384
|
+
nextStart.on("exit", (code) => {
|
|
1385
|
+
process.exit(code ?? 0);
|
|
1386
|
+
});
|
|
1387
|
+
} catch (error) {
|
|
1388
|
+
console.log(error);
|
|
1389
|
+
process.exit(1);
|
|
1390
|
+
}
|
|
1391
|
+
};
|
|
1392
|
+
|
|
1393
|
+
// src/index.ts
|
|
1394
|
+
var PACKAGE_NAME = "react-email";
|
|
1395
|
+
program.name(PACKAGE_NAME).description("A live preview of your emails right in your browser").version(package_default.version);
|
|
1396
|
+
program.command("dev").description("Starts the preview email development app").option("-d, --dir <path>", "Directory with your email templates", "./emails").option("-p --port <port>", "Port to run dev server on", "3000").action(dev);
|
|
1397
|
+
program.command("build").description("Copies the preview app for onto .react-email and builds it").option("-d, --dir <path>", "Directory with your email templates", "./emails").option(
|
|
1398
|
+
"-p --packageManager <name>",
|
|
1399
|
+
"Package name to use on installation on `.react-email`",
|
|
1400
|
+
"npm"
|
|
1401
|
+
).action(build);
|
|
1402
|
+
program.command("start").description('Runs the built preview app that is inside of ".react-email"').action(start);
|
|
1403
|
+
program.command("export").description("Build the templates to the `out` directory").option("--outDir <path>", "Output directory", "out").option("-p, --pretty", "Pretty print the output", false).option("-t, --plainText", "Set output format as plain text", false).option("-d, --dir <path>", "Directory with your email templates", "./emails").option(
|
|
1404
|
+
"-s, --silent",
|
|
1405
|
+
"To, or not to show a spinner with process information",
|
|
1406
|
+
false
|
|
1407
|
+
).action(
|
|
1408
|
+
({ outDir, pretty, plainText, silent, dir: srcDir }) => exportTemplates(outDir, srcDir, { silent, plainText, pretty })
|
|
1409
|
+
);
|
|
1410
|
+
program.parse();
|