I’m having a bit of an issue here. I’m using Vite, React, Node. What I’m trying to do:
vercel receives form information after submit in backend
stuff gets sanitized etc
if okay send form values structured using my template with Resend
The issue is is that the template is a .tsx (because it uses react email components) file, which cannot be rendered by the backend. It’s not found as template.js if I put it somewhere in the api dir. So I figured, well, Vite will render it if I put it in the root > lib/templates/formmailtemplate.tsx which should be shared?
Using .tsx is correct and if that’s not working then the issue is in your vite configuration which needs to be able to pre-process the tsx files
With the React Router vite plugin you get this out of the box, otherwise you may need to add an extra entrypoint to your vite config to get it to compile them to regular ts
currently I’ve got the ContactEmailTemplate.tsx in a mails folder which is in the ‘root’, next to the ‘api’ folder and the ‘src’ folder. I’ve added “include”: [“src”, “emails”, “api”] to my tsconfig.json and trying to import from api/services/emailService.ts like so:
import ContactEmail from ‘../../mails/ContactEmailTemplate.js’;
however, Vercel still can’t find it from the backend.
This isn’t related to the tsconfig at all, you’ll need to get Vite to compile your tsx files into js files that become Vercel functions.
You could do this with a prebuild step that you run on your server-side tsx files to compile them into the same locations (with .js extension instead of .tsx) and then delete the tsx, which should make the imports line up properly
I strongly recommend using a framework with bundling support built in as it takes a lot of legwork away from you.
If you want to do it yourself the same way the frameworks do, here are the full docs on the file structure to compile to
thanks for all the time you put into this. I know I had to compile with Vite before and the files were actually there. I just couldn’t get them to import correctly.
the final solution was to discard the ‘import ContactEmail from…’ line and use an absolute path: