@emailens/cli 0.13.1 → 0.13.3
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/README.md +188 -186
- package/dist/chunk-2AP7CT6R.js +65 -0
- package/dist/chunk-ABDTBRVK.js +278 -0
- package/dist/chunk-FRXG2KHP.js +72790 -0
- package/dist/chunk-K6T7YHQE.js +50 -0
- package/dist/chunk-NXBRJX3V.js +127 -0
- package/dist/chunk-TQGPCAMW.js +52 -0
- package/dist/index.js +44368 -250
- package/dist/maizzle-2EWV25PZ-MFDX4VFS.js +12 -0
- package/dist/mjml-C46UWTTK-K5PRQEV4.js +10 -0
- package/dist/prompt-2EJPRDMQ.js +756 -0
- package/dist/react-email-4EWH2SB4-CUKHMPBB.js +10 -0
- package/dist/{screenshots-UOLRXFAJ.js → screenshots-TICHS3Q4.js} +4 -4
- package/dist/{sdk-IYJIYBYM.js → sdk-SK3KXIBO.js} +3 -3
- package/dist/test.d.ts +16 -6
- package/dist/test.js +9 -5
- package/package.json +16 -11
- package/dist/chunk-55EWHWWT.js +0 -648
- package/dist/chunk-55EWHWWT.js.map +0 -1
- package/dist/chunk-7OCVIDC7.js +0 -12
- package/dist/chunk-7OCVIDC7.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/screenshots-UOLRXFAJ.js.map +0 -1
- package/dist/sdk-IYJIYBYM.js.map +0 -1
- package/dist/test.js.map +0 -1
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { createRequire as __cr } from "node:module";
|
|
2
|
+
const require = __cr(import.meta.url);
|
|
3
|
+
import {
|
|
4
|
+
CompileError
|
|
5
|
+
} from "./chunk-TQGPCAMW.js";
|
|
6
|
+
|
|
7
|
+
// node_modules/@emailens/engine/dist/chunk-OXR2HGQV.js
|
|
8
|
+
var MAX_SOURCE_SIZE = 256e3;
|
|
9
|
+
var EXECUTION_TIMEOUT_MS = 5e3;
|
|
10
|
+
async function compileReactEmail(source, options) {
|
|
11
|
+
var _a, _b;
|
|
12
|
+
const strategy = (_a = options == null ? void 0 : options.sandbox) != null ? _a : "isolated-vm";
|
|
13
|
+
if (!source || !source.trim()) {
|
|
14
|
+
throw new CompileError("JSX source must not be empty.", "jsx", "validation");
|
|
15
|
+
}
|
|
16
|
+
if (source.length > MAX_SOURCE_SIZE) {
|
|
17
|
+
throw new CompileError(
|
|
18
|
+
`JSX source exceeds ${MAX_SOURCE_SIZE / 1e3}KB limit.`,
|
|
19
|
+
"jsx",
|
|
20
|
+
"validation"
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
let transform;
|
|
24
|
+
let React;
|
|
25
|
+
let ReactEmailComponents;
|
|
26
|
+
let render;
|
|
27
|
+
try {
|
|
28
|
+
({ transform } = await import("sucrase"));
|
|
29
|
+
} catch (e) {
|
|
30
|
+
throw new CompileError(
|
|
31
|
+
'JSX compilation requires "sucrase". Install it:\n npm install sucrase',
|
|
32
|
+
"jsx",
|
|
33
|
+
"transpile"
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
React = await import("react");
|
|
38
|
+
} catch (e) {
|
|
39
|
+
throw new CompileError(
|
|
40
|
+
'JSX compilation requires "react". Install it:\n npm install react',
|
|
41
|
+
"jsx",
|
|
42
|
+
"transpile"
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
ReactEmailComponents = await import("@react-email/components");
|
|
47
|
+
} catch (e) {
|
|
48
|
+
throw new CompileError(
|
|
49
|
+
'JSX compilation requires "@react-email/components". Install it:\n npm install @react-email/components',
|
|
50
|
+
"jsx",
|
|
51
|
+
"transpile"
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
({ render } = await import("@react-email/render"));
|
|
56
|
+
} catch (e) {
|
|
57
|
+
throw new CompileError(
|
|
58
|
+
'JSX compilation requires "@react-email/render". Install it:\n npm install @react-email/render',
|
|
59
|
+
"jsx",
|
|
60
|
+
"transpile"
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
let transpiledCode;
|
|
64
|
+
try {
|
|
65
|
+
const result = transform(source, {
|
|
66
|
+
transforms: ["typescript", "jsx", "imports"],
|
|
67
|
+
jsxRuntime: "classic",
|
|
68
|
+
production: true
|
|
69
|
+
});
|
|
70
|
+
transpiledCode = result.code;
|
|
71
|
+
} catch (err) {
|
|
72
|
+
const message = err instanceof Error ? err.message : "Unknown transpilation error";
|
|
73
|
+
throw new CompileError(`JSX syntax error: ${message}`, "jsx", "transpile");
|
|
74
|
+
}
|
|
75
|
+
let moduleExports;
|
|
76
|
+
switch (strategy) {
|
|
77
|
+
case "isolated-vm":
|
|
78
|
+
moduleExports = await executeInIsolatedVm(transpiledCode, React, ReactEmailComponents);
|
|
79
|
+
break;
|
|
80
|
+
case "vm":
|
|
81
|
+
moduleExports = await executeInVm(transpiledCode, React, ReactEmailComponents);
|
|
82
|
+
break;
|
|
83
|
+
default:
|
|
84
|
+
throw new CompileError(
|
|
85
|
+
`Unknown sandbox strategy: "${strategy}". Use "vm" or "isolated-vm".`,
|
|
86
|
+
"jsx",
|
|
87
|
+
"execution"
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
let Component = (_b = moduleExports.default) != null ? _b : moduleExports;
|
|
91
|
+
if (typeof Component !== "function" && typeof Component === "object" && Component !== null) {
|
|
92
|
+
const values = Object.values(Component);
|
|
93
|
+
const fn = values.find((v) => typeof v === "function");
|
|
94
|
+
if (fn) Component = fn;
|
|
95
|
+
}
|
|
96
|
+
if (typeof Component !== "function") {
|
|
97
|
+
throw new CompileError(
|
|
98
|
+
'The JSX source must export a React component function. Use "export default function Email() { ... }" or "export function Email() { ... }".',
|
|
99
|
+
"jsx",
|
|
100
|
+
"execution"
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
const element = React.createElement(Component);
|
|
105
|
+
const html = await render(element);
|
|
106
|
+
return html;
|
|
107
|
+
} catch (err) {
|
|
108
|
+
const message = err instanceof Error ? err.message : "Unknown rendering error";
|
|
109
|
+
throw new CompileError(`React rendering error: ${message}`, "jsx", "render");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
async function executeInVm(code, React, ReactEmailComponents) {
|
|
113
|
+
const { createContext, Script } = await import("vm");
|
|
114
|
+
const ALLOWED_MODULES = {
|
|
115
|
+
react: React,
|
|
116
|
+
"@react-email/components": ReactEmailComponents
|
|
117
|
+
};
|
|
118
|
+
const moduleExports = {};
|
|
119
|
+
const moduleObj = { exports: moduleExports };
|
|
120
|
+
const mockRequire = (moduleName) => {
|
|
121
|
+
if (moduleName in ALLOWED_MODULES) {
|
|
122
|
+
return ALLOWED_MODULES[moduleName];
|
|
123
|
+
}
|
|
124
|
+
throw new Error(
|
|
125
|
+
`Import of "${moduleName}" is not allowed. Only "react" and "@react-email/components" can be imported.`
|
|
126
|
+
);
|
|
127
|
+
};
|
|
128
|
+
const sandbox = {
|
|
129
|
+
module: moduleObj,
|
|
130
|
+
exports: moduleExports,
|
|
131
|
+
require: mockRequire,
|
|
132
|
+
React,
|
|
133
|
+
Object,
|
|
134
|
+
Array,
|
|
135
|
+
String,
|
|
136
|
+
Number,
|
|
137
|
+
Boolean,
|
|
138
|
+
Map,
|
|
139
|
+
Set,
|
|
140
|
+
WeakMap,
|
|
141
|
+
WeakSet,
|
|
142
|
+
JSON,
|
|
143
|
+
Math,
|
|
144
|
+
Date,
|
|
145
|
+
RegExp,
|
|
146
|
+
Error,
|
|
147
|
+
TypeError,
|
|
148
|
+
RangeError,
|
|
149
|
+
ReferenceError,
|
|
150
|
+
SyntaxError,
|
|
151
|
+
URIError,
|
|
152
|
+
Promise,
|
|
153
|
+
Symbol,
|
|
154
|
+
Proxy: void 0,
|
|
155
|
+
Reflect: void 0,
|
|
156
|
+
parseInt,
|
|
157
|
+
parseFloat,
|
|
158
|
+
isNaN,
|
|
159
|
+
isFinite,
|
|
160
|
+
encodeURIComponent,
|
|
161
|
+
decodeURIComponent,
|
|
162
|
+
encodeURI,
|
|
163
|
+
decodeURI,
|
|
164
|
+
undefined: void 0,
|
|
165
|
+
NaN: NaN,
|
|
166
|
+
Infinity: Infinity,
|
|
167
|
+
console: { log: () => {
|
|
168
|
+
}, warn: () => {
|
|
169
|
+
}, error: () => {
|
|
170
|
+
}, info: () => {
|
|
171
|
+
}, debug: () => {
|
|
172
|
+
} },
|
|
173
|
+
setTimeout: void 0,
|
|
174
|
+
setInterval: void 0,
|
|
175
|
+
setImmediate: void 0,
|
|
176
|
+
queueMicrotask: void 0,
|
|
177
|
+
process: void 0,
|
|
178
|
+
globalThis: void 0,
|
|
179
|
+
global: void 0,
|
|
180
|
+
Buffer: void 0,
|
|
181
|
+
__dirname: void 0,
|
|
182
|
+
__filename: void 0
|
|
183
|
+
};
|
|
184
|
+
const context = createContext(sandbox, {
|
|
185
|
+
codeGeneration: { strings: false, wasm: false }
|
|
186
|
+
});
|
|
187
|
+
try {
|
|
188
|
+
const script = new Script(code, { filename: "user-email-component.tsx" });
|
|
189
|
+
script.runInContext(context, { timeout: EXECUTION_TIMEOUT_MS, displayErrors: true });
|
|
190
|
+
} catch (err) {
|
|
191
|
+
const message = err instanceof Error ? err.message : "Unknown execution error";
|
|
192
|
+
if (message.includes("Script execution timed out")) {
|
|
193
|
+
throw new CompileError("JSX execution timed out (possible infinite loop).", "jsx", "execution");
|
|
194
|
+
}
|
|
195
|
+
throw new CompileError(`JSX execution error: ${message}`, "jsx", "execution");
|
|
196
|
+
}
|
|
197
|
+
return moduleObj.exports;
|
|
198
|
+
}
|
|
199
|
+
async function executeInIsolatedVm(code, React, ReactEmailComponents) {
|
|
200
|
+
var _a, _b;
|
|
201
|
+
let ivm;
|
|
202
|
+
try {
|
|
203
|
+
const ivmMod = await import("isolated-vm");
|
|
204
|
+
ivm = (_a = ivmMod.default) != null ? _a : ivmMod;
|
|
205
|
+
} catch (e) {
|
|
206
|
+
throw new CompileError(
|
|
207
|
+
'Sandbox strategy "isolated-vm" requires the "isolated-vm" package. Install it:\n npm install isolated-vm\nOr pass sandbox: "vm", which needs no native addon and is NOT a security boundary: code you compile can reach the host process and its environment. Only choose it for templates you already trust.',
|
|
208
|
+
"jsx",
|
|
209
|
+
"execution"
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
const isolate = new ivm.Isolate({ memoryLimit: 128 });
|
|
213
|
+
try {
|
|
214
|
+
const ivmContext = await isolate.createContext();
|
|
215
|
+
const validationCode = `
|
|
216
|
+
(function() {
|
|
217
|
+
var module = { exports: {} };
|
|
218
|
+
var exports = module.exports;
|
|
219
|
+
var noop = function() { return {}; };
|
|
220
|
+
var React = new Proxy({
|
|
221
|
+
createElement: noop,
|
|
222
|
+
forwardRef: function(fn) { return fn; },
|
|
223
|
+
Fragment: "Fragment",
|
|
224
|
+
createContext: function() { return { Provider: noop, Consumer: noop }; },
|
|
225
|
+
useState: function(v) { return [v, noop]; },
|
|
226
|
+
useRef: function() { return { current: null }; },
|
|
227
|
+
useEffect: noop,
|
|
228
|
+
useMemo: function(fn) { return fn(); },
|
|
229
|
+
useCallback: function(fn) { return fn; },
|
|
230
|
+
Children: { map: noop, forEach: noop, toArray: function() { return []; } },
|
|
231
|
+
}, { get: function(t, p) { return p in t ? t[p] : noop; } });
|
|
232
|
+
var componentsProxy = new Proxy({}, {
|
|
233
|
+
get: function() { return noop; }
|
|
234
|
+
});
|
|
235
|
+
function require(name) {
|
|
236
|
+
if (name === "react") return React;
|
|
237
|
+
if (name === "@react-email/components") return componentsProxy;
|
|
238
|
+
throw new Error('Import of "' + name + '" is not allowed. Only "react" and "@react-email/components" can be imported.');
|
|
239
|
+
}
|
|
240
|
+
try {
|
|
241
|
+
${code}
|
|
242
|
+
return JSON.stringify({ ok: true });
|
|
243
|
+
} catch(e) {
|
|
244
|
+
return JSON.stringify({ ok: false, error: e.message || "Unknown error" });
|
|
245
|
+
}
|
|
246
|
+
})()
|
|
247
|
+
`;
|
|
248
|
+
try {
|
|
249
|
+
const result = await ivmContext.eval(validationCode, {
|
|
250
|
+
timeout: EXECUTION_TIMEOUT_MS
|
|
251
|
+
});
|
|
252
|
+
if (typeof result === "string") {
|
|
253
|
+
const parsed = JSON.parse(result);
|
|
254
|
+
if (!parsed.ok) {
|
|
255
|
+
throw new CompileError(
|
|
256
|
+
`JSX execution error: ${(_b = parsed.error) != null ? _b : "Unknown error"}`,
|
|
257
|
+
"jsx",
|
|
258
|
+
"execution"
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
} catch (err) {
|
|
263
|
+
if (err instanceof CompileError) throw err;
|
|
264
|
+
const message = err instanceof Error ? err.message : "Unknown execution error";
|
|
265
|
+
if (message.includes("timed out") || message.includes("Timeout")) {
|
|
266
|
+
throw new CompileError("JSX execution timed out (possible infinite loop).", "jsx", "execution");
|
|
267
|
+
}
|
|
268
|
+
throw new CompileError(`JSX execution error: ${message}`, "jsx", "execution");
|
|
269
|
+
}
|
|
270
|
+
} finally {
|
|
271
|
+
isolate.dispose();
|
|
272
|
+
}
|
|
273
|
+
return executeInVm(code, React, ReactEmailComponents);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export {
|
|
277
|
+
compileReactEmail
|
|
278
|
+
};
|