Skip to content

Commit

Permalink
pass minifier callback to render method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaraujo6 committed Apr 20, 2023
1 parent 6f681c0 commit 37e8fb2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type BuildSendMailOptions<T> = {
transport: Transporter<T>;
defaultFrom: string;
configPath: string;
minifyCb?: (html: string) => string;
};

export type ComponentMail = SendMailOptions & {
Expand Down Expand Up @@ -114,7 +115,10 @@ export function buildSendMail<T>(options: BuildSendMailOptions<T>) {
// Get html from the rendered component if not provided
let derivedTemplateName;
if (component && !mailOptions.html) {
const { html: renderedHtml, errors } = render(component);
const { html: renderedHtml, errors } = render(
component,
options.minifyCb
);
if (errors?.length) {
error(errors);
throw new MalformedInputError(
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/mjml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ import { JSXElementConstructor, ReactElement } from "react";
import { render as mjRender } from "mjml-react";

export function render(
component: ReactElement<any, string | JSXElementConstructor<any>>
component: ReactElement<any, string | JSXElementConstructor<any>>,
minifyCb?: (html: string) => string
) {
return mjRender(component, {
const { html, errors } = mjRender(component, {
validationLevel: "soft",
minify: undefined,
});

return {
html: minifyCb?.(html) || html,
errors,
};
}
8 changes: 8 additions & 0 deletions packages/web/pages/docs/sending-email.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ const sendMail = buildSendMail({
}),
defaultFrom: "Steven from Dub <steven@dub.sh>",
configPath: "./mailing.config.json",
// optional
minifyCb: (html: string) => htmlMinify(html, {
caseSensitive: true,
collapseWhitespace: true,
minifyCSS: true,
removeComments: true,
removeEmptyAttributes: true,
})
});
```

Expand Down

0 comments on commit 37e8fb2

Please sign in to comment.