1- import { sendEmail } from '$lib/preview/index.js' ;
2- import { env } from '$env/dynamic/private' ;
3- import type { PreviewData } from '$lib/preview/index.js' ;
4- import type { RequestEvent } from '@sveltejs/kit' ;
5- import { render } from 'svelte/server' ;
6- import prettier from 'prettier/standalone' ;
7- import parserHtml from 'prettier/parser-html' ;
8-
9- // Import all email components at build time using import.meta.glob
10- // This creates a map of all email components that can be accessed at runtime
11- const emailModules = import . meta. glob ( '/src/lib/emails/**/*.svelte' , { eager : true } ) ;
12-
13- /**
14- * Vercel-compatible email list function that uses Vite's import.meta.glob
15- * to statically analyze email files at build time instead of runtime fs access
16- */
17- function emailListVercel ( ) : PreviewData {
18- const files = Object . keys ( emailModules )
19- . map ( ( path ) => {
20- // Extract filename without extension from the full path
21- // e.g., '/src/lib/emails/apple-receipt.svelte' -> 'apple-receipt'
22- const match = path . match ( / \/ s r c \/ l i b \/ e m a i l s \/ ( .+ ) \. s v e l t e $ / ) ;
23- return match ? match [ 1 ] : null ;
24- } )
25- . filter ( ( name ) : name is string => name !== null ) ;
26-
27- if ( files . length === 0 ) {
28- return { files : null , path : '/src/lib/emails' } ;
29- }
30-
31- return { files, path : '/src/lib/emails' } ;
32- }
33-
34- /**
35- * Vercel-compatible createEmail action that uses pre-imported email components
36- * instead of dynamic runtime imports
37- */
38- const createEmailVercel = {
39- 'create-email' : async ( event : RequestEvent ) => {
40- try {
41- const data = await event . request . formData ( ) ;
42- const file = data . get ( 'file' ) ;
43- const emailPath = data . get ( 'path' ) ;
44-
45- if ( ! file || ! emailPath ) {
46- return {
47- status : 400 ,
48- body : { error : 'Missing file or path parameter' }
49- } ;
50- }
51-
52- // Construct the full path to match the keys in emailModules
53- const fullPath = `${ emailPath } /${ file } .svelte` ;
54-
55- // Get the component from the pre-imported modules
56- const module = emailModules [ fullPath ] as { default : any } | undefined ;
57-
58- if ( ! module || ! module . default ) {
59- throw new Error (
60- `Failed to import email component '${ file } ' in '${ emailPath } '. Make sure the file exists and includes the <Head /> component.`
61- ) ;
62- }
63-
64- const emailComponent = module . default ;
65-
66- // Render the component to HTML
67- const { body } = render ( emailComponent ) ;
68-
69- // Remove all HTML comments from the body before formatting
70- const bodyWithoutComments = body . replace ( / < ! - - [ \s \S ] * ?- - > / g, '' ) ;
71- const formattedBody = await prettier . format ( bodyWithoutComments , {
72- parser : 'html' ,
73- plugins : [ parserHtml ]
74- } ) ;
75-
76- return {
77- body : formattedBody
78- } ;
79- } catch ( error ) {
80- console . error ( 'Error rendering email:' , error ) ;
81- return {
82- status : 500 ,
83- error : {
84- message : error instanceof Error ? error . message : 'Failed to render email'
85- }
86- } ;
87- }
88- }
89- } ;
90-
91- export function load ( ) {
92- const emails = emailListVercel ( ) ;
93- return { emails } ;
1+ //const emailModules = import.meta.glob('/src/lib/server/email/templates/**/*.svelte', { eager: true });
2+
3+ import { createEmail , emailList } from "$lib/preview/index.js" ;
4+
5+ /*function emailList(): PreviewData {
6+ const files = Object.keys(emailModules)
7+ .map((path) => {
8+ const match = path.match(/\/src\/lib\/server\/email\/templates\/(.+)\.svelte$/);
9+ return match ? match[1] : null;
10+ })
11+ .filter((name): name is string => name !== null);
12+
13+ if (files.length === 0) {
14+ return { files: null, path: '/src/lib/server/email/templates' };
15+ }
16+ return { files, path: '/src/lib/server/email/templates' };
17+ }*/
18+
19+ export async function load ( ) {
20+ const emails = emailList ( {
21+ path : '/src/lib/test/emails' ,
22+ } ) ;
23+ return { emails } ;
9424}
9525
9626export const actions = {
97- ...createEmailVercel ,
98- ...sendEmail ( { resendApiKey : env . RESEND_API_KEY ?? 're_1234' } )
99- } ;
27+ ...createEmail ,
28+ } ;
0 commit comments