Skip to content

Commit 29c1de4

Browse files
authored
Merge pull request #655 from webstackdev/bugfix/bundle-import-error
Fix function bundle to include MJML source files
2 parents eb264be + f5ae3a3 commit 29c1de4

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

src/actions/utils/email/__tests__/templateCompiler.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ const importedExampleTemplate = createImportedEmailTemplate(
3737
exampleTemplateContent
3838
)
3939

40+
const importedBundleSafeTemplate = createImportedEmailTemplate(
41+
'src/actions/utils/email/__fixtures__/missing-from-runtime-bundle.mjml',
42+
exampleTemplateContent
43+
)
44+
4045
describe('compileEmailTemplate', () => {
4146
it('compiles the moved contact message template from its imported source', async () => {
4247
const result = await compileEmailTemplate(contactMessageTemplate, {
@@ -111,6 +116,18 @@ describe('compileEmailTemplate', () => {
111116
expect(result.text).toContain('Urgent message body')
112117
})
113118

119+
it('compiles imported template content when the source file is absent at runtime', async () => {
120+
const result = await compileEmailTemplate(importedBundleSafeTemplate, {
121+
htmlMessage: '<strong>Urgent</strong> message body',
122+
items: ['Coffee Beans'],
123+
name: 'Alex',
124+
})
125+
126+
expect(result.html).toContain('Hello, Alex!')
127+
expect(result.html).toContain('Coffee Beans')
128+
expect(result.text).toContain('Urgent message body')
129+
})
130+
114131
it('throws ActionsFunctionError when MJML compilation reports errors', async () => {
115132
await expect(compileEmailTemplate(invalidTemplate)).rejects.toMatchObject({
116133
appCode: 'EMAIL_TEMPLATE_COMPILE_FAILED',

src/actions/utils/email/templateCompiler.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { existsSync } from 'node:fs'
12
import { isAbsolute, relative, resolve } from 'node:path'
23
import { fileURLToPath } from 'node:url'
34
import { convert } from 'html-to-text'
@@ -218,6 +219,22 @@ export const createEmailTemplate = (
218219
}
219220
}
220221

222+
const createMjmlRenderOptions = (absolutePath: string): {
223+
filePath?: string
224+
keepComments: boolean
225+
} => {
226+
if (!existsSync(absolutePath)) {
227+
return {
228+
keepComments: false,
229+
}
230+
}
231+
232+
return {
233+
filePath: absolutePath,
234+
keepComments: false,
235+
}
236+
}
237+
221238
const createPlainText = (html: string): string =>
222239
convert(html, {
223240
wordwrap: 130,
@@ -249,10 +266,10 @@ export async function compileEmailTemplate(
249266
const mjml2html = (
250267
'default' in mjmlModule ? mjmlModule.default : mjmlModule
251268
) as MjmlRenderer
252-
const { html, errors } = await mjml2html(mjmlWithData, {
253-
filePath: absolutePath,
254-
keepComments: false,
255-
})
269+
const { html, errors } = await mjml2html(
270+
mjmlWithData,
271+
createMjmlRenderOptions(absolutePath)
272+
)
256273

257274
if (errors.length > 0) {
258275
throw new ActionsFunctionError({

0 commit comments

Comments
 (0)