A Svelte preprocessor that allows you to write MJML directly inside your Svelte components. It compiles MJML to HTML while intelligently preserving Svelte's logic syntax ({#if}, {#each}, etc.), making it perfect for building dynamic responsive emails.
.svelte files.npm install -D svelte-mjml-plugin mjml
Note: mjml is a peer dependency.
Add the preprocessor to your svelte.config.js:
import { mjmlPreprocessor } from "svelte-mjml-plugin";
/** @type {import('@sveltejs/kit').Config} */
const config = {
preprocess: [
mjmlPreprocessor({
// Optional: change the tag name (default is 'mjml')
tagName: "mjml",
// Optional: MJML compilation options
mjmlOptions: {
validationLevel: "soft",
minify: true,
},
}),
],
// ... rest of config
};
export default config;
In your .svelte component, wrap your MJML code in the <mjml> tag:
<script>
let name = "Guest";
let items = ["Apple", "Banana", "Cherry"];
</script>
<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-text font-size="20px">Hello, {name}!</mj-text>
{#if items.length > 0}
<mj-text>Your list:</mj-text>
{#each items as item}
<mj-text>- {item}</mj-text>
{/each}
{:else}
<mj-text>Your list is empty.</mj-text>
{/if}
</mj-column>
</mj-section>
</mj-body>
</mjml>
The preprocessor finds <mjml> blocks, extracts the content, and:
{#...}, {@...}, etc.) with temporary placeholders inside <mj-raw> tags.{@html ...} to ensure Svelte doesn't escape the compiled MJML structure.| Option | Type | Default | Description |
|---|---|---|---|
tagName |
string |
'mjml' |
The tag used to identify MJML blocks in your Svelte files. |
mjmlOptions |
object |
{} |
Options passed directly to the mjml2html compiler. |
MIT