Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/actions/utils/email/__tests__/templateCompiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ const importedExampleTemplate = createImportedEmailTemplate(
exampleTemplateContent
)

const importedBundleSafeTemplate = createImportedEmailTemplate(
'src/actions/utils/email/__fixtures__/missing-from-runtime-bundle.mjml',
exampleTemplateContent
)

describe('compileEmailTemplate', () => {
it('compiles the moved contact message template from its imported source', async () => {
const result = await compileEmailTemplate(contactMessageTemplate, {
Expand Down Expand Up @@ -111,6 +116,18 @@ describe('compileEmailTemplate', () => {
expect(result.text).toContain('Urgent message body')
})

it('compiles imported template content when the source file is absent at runtime', async () => {
const result = await compileEmailTemplate(importedBundleSafeTemplate, {
htmlMessage: '<strong>Urgent</strong> message body',
items: ['Coffee Beans'],
name: 'Alex',
})

expect(result.html).toContain('Hello, Alex!')
expect(result.html).toContain('Coffee Beans')
expect(result.text).toContain('Urgent message body')
})

it('throws ActionsFunctionError when MJML compilation reports errors', async () => {
await expect(compileEmailTemplate(invalidTemplate)).rejects.toMatchObject({
appCode: 'EMAIL_TEMPLATE_COMPILE_FAILED',
Expand Down
25 changes: 21 additions & 4 deletions src/actions/utils/email/templateCompiler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { existsSync } from 'node:fs'
import { isAbsolute, relative, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { convert } from 'html-to-text'
Expand Down Expand Up @@ -218,6 +219,22 @@ export const createEmailTemplate = (
}
}

const createMjmlRenderOptions = (absolutePath: string): {
filePath?: string
keepComments: boolean
} => {
if (!existsSync(absolutePath)) {
return {
keepComments: false,
}
}

return {
filePath: absolutePath,
keepComments: false,
}
}

const createPlainText = (html: string): string =>
convert(html, {
wordwrap: 130,
Expand Down Expand Up @@ -249,10 +266,10 @@ export async function compileEmailTemplate(
const mjml2html = (
'default' in mjmlModule ? mjmlModule.default : mjmlModule
) as MjmlRenderer
const { html, errors } = await mjml2html(mjmlWithData, {
filePath: absolutePath,
keepComments: false,
})
const { html, errors } = await mjml2html(
mjmlWithData,
createMjmlRenderOptions(absolutePath)
)

if (errors.length > 0) {
throw new ActionsFunctionError({
Expand Down
Loading