Created
May 24, 2024 20:32
-
-
Save ahmadawais/65e3d5a73e005d0b56bf90dd087deec3 to your computer and use it in GitHub Desktop.
React email build script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { render } from '@react-email/render'; | |
import fs from 'fs'; | |
import path from 'path'; | |
import React from 'react'; | |
// Get a list of all files in the 'emails' directory | |
const emailFiles = fs.readdirSync('./emails'); | |
// Create a new directory named 'build' if it doesn't exist | |
if (!fs.existsSync('build')) fs.mkdirSync('build'); | |
// For each file in the 'emails' directory | |
emailFiles.forEach(file => { | |
// Only process files with the '.tsx' extension | |
if (path.extname(file) === '.tsx') { | |
// Import the email file dynamically | |
const EmailComponent = require(`./emails/${file}`).default; | |
// Get the name of the imported email file | |
const emailFileName = path.basename(file, path.extname(file)); | |
const html = render(<EmailComponent />); | |
// Write the rendered HTML to a file in the 'build' directory | |
fs.writeFileSync(path.join('build', `${emailFileName}.html`), html); | |
// Write the plain text version of the email to a file in the 'build' directory | |
const plainText = render(<EmailComponent />, { plainText: true }); | |
fs.writeFileSync(path.join('build', `${emailFileName}.txt`), plainText); | |
console.log(`Files ${emailFileName}.html and ${emailFileName}.txt have been created in the 'build' directory.`); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment