Skip to content

Commit

Permalink
move the fileContents to be cached with the getEmailComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmfern committed Jan 6, 2025
1 parent e22c0c3 commit bb572c4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
23 changes: 10 additions & 13 deletions packages/react-email/src/actions/render-email-by-path.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,17 @@ export const renderEmailByPath = async (
const timeBeforeEmailRendered = performance.now();

const emailFilename = path.basename(emailPath);
let spinner: ora.Ora | undefined;
if (process.env.NEXT_PUBLIC_IS_BUILDING !== 'true') {
spinner = ora({
text: `Rendering email template ${emailFilename}\n`,
prefixText: ' ',
}).start();
registerSpinnerAutostopping(spinner);
}

const spinner = ora({
text: `Rendering email template ${emailFilename}\n`,
prefixText: ' ',
}).start();
registerSpinnerAutostopping(spinner);

const componentResult = await cachedGetEmailComponent(emailPath, invalidatingComponentCache);

if ('error' in componentResult) {
spinner?.stopAndPersist({
spinner.stopAndPersist({
symbol: logSymbols.error,
text: `Failed while rendering ${emailFilename}`,
});
Expand All @@ -75,6 +73,7 @@ export const renderEmailByPath = async (
emailComponent: Email,
createElement,
render,
fileContents: reactMarkup,
sourceMapToOriginalFile,
} = componentResult;

Expand All @@ -87,8 +86,6 @@ export const renderEmailByPath = async (
plainText: true,
});

const reactMarkup = await fs.promises.readFile(emailPath, 'utf-8');

const milisecondsToRendered = performance.now() - timeBeforeEmailRendered;
let timeForConsole = `${milisecondsToRendered.toFixed(0)}ms`;
if (milisecondsToRendered <= 450) {
Expand All @@ -98,7 +95,7 @@ export const renderEmailByPath = async (
} else {
timeForConsole = chalk.red(timeForConsole);
}
spinner?.stopAndPersist({
spinner.stopAndPersist({
symbol: logSymbols.success,
text: `Successfully rendered ${emailFilename} in ${timeForConsole}`,
});
Expand All @@ -118,7 +115,7 @@ export const renderEmailByPath = async (
} catch (exception) {
const error = exception as Error;

spinner?.stopAndPersist({
spinner.stopAndPersist({
symbol: logSymbols.error,
text: `Failed while rendering ${emailFilename}`,
});
Expand Down
9 changes: 6 additions & 3 deletions packages/react-email/src/utils/get-email-component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import path from 'node:path';
import vm from 'node:vm';
import fs from 'node:fs'
import type React from 'react';
import { type RawSourceMap } from 'source-map-js';
import { type OutputFile, build, type BuildFailure } from 'esbuild';
Expand All @@ -13,12 +14,10 @@ import { renderingUtilitiesExporter } from './esbuild/renderring-utilities-expor

export type GetEmailComponentResult =
| {
fileContents: string;
emailComponent: EmailComponent;

createElement: typeof React.createElement;

render: typeof render;

sourceMapToOriginalFile: RawSourceMap;
}
| { error: ErrorObject };
Expand Down Expand Up @@ -142,7 +141,11 @@ export const getEmailComponent = async (
};
}

const fileContents = await fs.promises.readFile(emailPath, 'utf-8');

return {
fileContents,

emailComponent: fakeContext.module.exports.default as EmailComponent,
render: fakeContext.module.exports.render as typeof render,
createElement: fakeContext.module.exports
Expand Down

0 comments on commit bb572c4

Please sign in to comment.