Skip to content

Commit

Permalink
feat: Use dynamic import for @react-email/render (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
vcapretz authored and bukinoshita committed Nov 18, 2024
1 parent fa34907 commit cfad2ce
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "resend",
"version": "4.0.0",
"version": "4.0.1-alpha.0",
"description": "Node.js library for the Resend API",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down Expand Up @@ -42,7 +42,7 @@
},
"homepage": "https://github.com/resendlabs/resend-node#readme",
"dependencies": {
"@react-email/render": "0.0.17"
"@react-email/render": "1.0.1"
},
"devDependencies": {
"@biomejs/biome": "1.9.4",
Expand Down
34 changes: 17 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 14 additions & 4 deletions src/batch/batch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { renderAsync } from '@react-email/render';
import type * as React from 'react';
import type { Resend } from '../resend';
import type {
Expand All @@ -9,6 +8,7 @@ import type {
} from './interfaces/create-batch-options.interface';

export class Batch {
private renderAsync?: (component: React.ReactElement) => Promise<string>;
constructor(private readonly resend: Resend) {}

async send(
Expand All @@ -24,9 +24,19 @@ export class Batch {
): Promise<CreateBatchResponse> {
for (const email of payload) {
if (email.react) {
email.html = await renderAsync(email.react as React.ReactElement);
// biome-ignore lint/performance/noDelete: <explanation>
delete email.react;
if (!this.renderAsync) {
try {
const { renderAsync } = await import('@react-email/render');
this.renderAsync = renderAsync;
} catch (error) {
throw new Error(
'Failed to render React component. Make sure to install `@react-email/render`',
);
}
}

email.html = await this.renderAsync(email.react as React.ReactElement);
email.react = undefined;
}
}

Expand Down
19 changes: 15 additions & 4 deletions src/emails/emails.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { renderAsync } from '@react-email/render';
import type * as React from 'react';
import type { Resend } from '../resend';
import type {
Expand All @@ -22,6 +21,7 @@ import type {
} from './interfaces/update-email-options.interface';

export class Emails {
private renderAsync?: (component: React.ReactElement) => Promise<string>;
constructor(private readonly resend: Resend) {}

async send(
Expand All @@ -36,9 +36,20 @@ export class Emails {
options: CreateEmailRequestOptions = {},
): Promise<CreateEmailResponse> {
if (payload.react) {
payload.html = await renderAsync(payload.react as React.ReactElement);
// biome-ignore lint/performance/noDelete: <explanation>
delete payload.react;
if (!this.renderAsync) {
try {
const { renderAsync } = await import('@react-email/render');
this.renderAsync = renderAsync;
} catch (error) {
throw new Error(
'Failed to render React component. Make sure to install `@react-email/render`',
);
}
}

payload.html = await this.renderAsync(
payload.react as React.ReactElement,
);
}

const data = await this.resend.post<CreateEmailResponseSuccess>(
Expand Down

0 comments on commit cfad2ce

Please sign in to comment.