[DEV-3700][DEV-3701] Refactor cognito-functions email templating to use build-time precompiled templates#2139
Conversation
…te functions to return promises
…tes during prebuild phase
🦋 Changeset detectedLatest commit: adfda0f The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Pull request overview
This PR refactors the apps/cognito-functions package to precompile MJML email templates at build time, removing runtime MJML/HTML minification from the Lambda bundle and updating handlers to render emails from generated, localized HTML templates.
Changes:
- Added a build-time template generation script that compiles MJML to minified HTML and writes a generated
PRECOMPILED_EMAIL_TEMPLATESmodule. - Refactored Cognito trigger handlers and template helpers to use
renderPrecompiledTemplatewith placeholder replacement (instead of runtime MJML + minification). - Updated scripts/config (tsconfig for scripts, ESLint ignore, .gitignore, esbuild config) and dependency layout (move MJML/minifier tooling to dev deps, introduce
DEFAULT_LOCALE).
Reviewed changes
Copilot reviewed 20 out of 21 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| package-lock.json | Updates dependency graph to drop runtime html-minifier/MJML usage and add build-time tooling (incl. html-minifier-next). |
| apps/cognito-functions/tsconfig.scripts.json | Adds a TS config for running build-time scripts with Node typings. |
| apps/cognito-functions/src/templates/template-sources.ts | Centralizes MJML source templates and placeholder IDs for build-time compilation. |
| apps/cognito-functions/src/templates/render-precompiled-template.ts | Adds runtime renderer that selects localized precompiled HTML and replaces placeholders with sanitized values. |
| apps/cognito-functions/src/templates/post-confirmation-confirm-sign-up-message.ts | Switches welcome email generation to the precompiled template renderer. |
| apps/cognito-functions/src/templates/otp-message.ts | Switches OTP email generation to the precompiled template renderer (keeping translation-based code duration text). |
| apps/cognito-functions/src/templates/mjmlParser.ts | Removes runtime MJML → HTML conversion + minification helper. |
| apps/cognito-functions/src/templates/confirmation-update-email-address.ts | Switches email-change confirmation email to precompiled template rendering. |
| apps/cognito-functions/src/templates/confirmation-message.ts | Switches sign-up confirmation email to precompiled template rendering. |
| apps/cognito-functions/src/templates/confirmation-forgot-password.ts | Switches forgot-password email to precompiled template rendering. |
| apps/cognito-functions/src/post-confirmation-handler.ts | Uses centralized DEFAULT_LOCALE and simplifies body generation for SES welcome email. |
| apps/cognito-functions/src/i18n/locales.ts | Introduces DEFAULT_LOCALE constant and keeps supported locales list. |
| apps/cognito-functions/src/custom-message-handler.ts | Uses centralized DEFAULT_LOCALE for locale fallback when generating Cognito custom messages. |
| apps/cognito-functions/src/create-auth-challenge-handler.ts | Uses centralized DEFAULT_LOCALE and refactors OTP email body creation before SES send. |
| apps/cognito-functions/scripts/generate-email-templates.ts | Adds build-time compilation + minification pipeline that writes generated TS module. |
| apps/cognito-functions/package.json | Adds template generation scripts/hooks, cleans generated outputs, and shifts MJML/minifier tooling to dev deps. |
| apps/cognito-functions/eslint.config.mjs | Ignores generated templates directory in ESLint. |
| apps/cognito-functions/esbuild.config.mjs | Removes MJML/uglify workaround plugin since MJML is no longer bundled at runtime. |
| apps/cognito-functions/README.md | Documents the new build-time template pipeline, scripts, and handler responsibilities. |
| apps/cognito-functions/.gitignore | Ignores generated templates directory. |
| .changeset/short-peaches-take.md | Declares a major bump and documents the breaking change. |
Comments suppressed due to low confidence (1)
apps/cognito-functions/src/create-auth-challenge-handler.ts:85
subjectTemplatefalls back toEMAIL_TRANSLATIONS.otp.it.subjecteven though the handler now centralizes the default locale inDEFAULT_LOCALE. To keep the fallback behavior consistent (and avoid future drift ifDEFAULT_LOCALEchanges), useEMAIL_TRANSLATIONS.otp[DEFAULT_LOCALE].subjectfor the fallback instead of hardcodingit.
const subjectTemplate =
EMAIL_TRANSLATIONS.otp[locale as keyof typeof EMAIL_TRANSLATIONS.otp]
?.subject || EMAIL_TRANSLATIONS.otp.it.subject;
const subject = subjectTemplate.replace('{{code}}', verificationCode);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…-minifier-with-html-minifier-next
|
This PR exceeds the recommended size of 800 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size. |
| ## Deployment artifact | ||
|
|
||
| The build produces: | ||
|
|
||
| ```sh | ||
| apps/cognito-functions/out/cognito-functions.zip | ||
| ``` | ||
|
|
||
| Infrastructure code can then point multiple Cognito triggers at different exported handlers inside the same bundle. |
There was a problem hiding this comment.
Isn't there a dedicated workflow?
Co-authored-by: marcobottaro <39835990+marcobottaro@users.noreply.github.com>
Jira Pull Request LinkThis Pull Request refers to the following Jira issue DEV-3700 |
Branch is not up to date with base branch@Sebastiano-Bertolin it seems this Pull Request is not updated with base branch. |
This pull request introduces a build-time MJML email template compilation pipeline for the Cognito Lambda functions package, replacing runtime MJML and HTML minification. The new approach generates precompiled HTML templates during build, reducing Lambda dependencies and improving performance. It also updates scripts, configuration, and handler code to support the new pipeline and cleans up unused dependencies.
List of Changes
Build pipeline and template precompilation:
scripts/generate-email-templates.jsto compile MJML templates to minified HTML at build time, writing them tosrc/templates/generated/precompiled-email-templates.ts.renderPrecompiledTemplatefor email generation, replacing runtime MJML rendering and minification. (src/templates/confirmation-message.ts,src/templates/confirmation-forgot-password.ts) [1] [2]package.jsonscripts to include template generation in build/test flows, and added a clean step for generated templates..gitignoreand ESLint ignore rules for generated templates directory. [1] [2]Dependency and configuration cleanup:
mjml,html-minifier, and their type definitions; switched tohtml-minifier-nextandmjmlonly for build-time use. [1] [2]esbuild.config.mjsby removing the workaround for MJML's runtime dependency issues, since MJML is no longer bundled into Lambda.Handler code improvements:
src/create-auth-challenge-handler.ts,src/post-confirmation-handler.ts) [1] [2] [3] [4]Documentation updates:
README.mddescribing the new build pipeline, handler exports, scripts, and workflow.Motivation and Context
How Has This Been Tested?
Screenshots (if appropriate):
Types of changes
Checklist: