nuxt-generation-emails 1.0.7 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/index.mjs +5 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +32 -33
- package/dist/runtime/types/mjml.d.ts +1 -1
- package/package.json +3 -3
package/dist/cli/index.mjs
CHANGED
|
@@ -49,8 +49,12 @@ const props = withDefaults(defineProps<{
|
|
|
49
49
|
* Handlebars, and returns a reactive ComputedRef<string> of rendered HTML.
|
|
50
50
|
* MJML components from components/ are registered automatically.
|
|
51
51
|
*/
|
|
52
|
-
useNgeTemplate('${templatePath}', props)
|
|
52
|
+
const html = useNgeTemplate('${templatePath}', props)
|
|
53
53
|
${scriptClose}
|
|
54
|
+
|
|
55
|
+
<template>
|
|
56
|
+
<div v-html="html" />
|
|
57
|
+
</template>
|
|
54
58
|
`;
|
|
55
59
|
}
|
|
56
60
|
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -623,7 +623,7 @@ export default defineEventHandler(async (event) => {
|
|
|
623
623
|
const mjmlSource = readFileSync(join(emailsDir, '${emailPath}.mjml'), 'utf-8')
|
|
624
624
|
const compiledTemplate = Handlebars.compile(mjmlSource)
|
|
625
625
|
const mjmlString = compiledTemplate(templateData)
|
|
626
|
-
const { html, errors } = mjml2html(mjmlString)
|
|
626
|
+
const { html, errors } = await mjml2html(mjmlString)
|
|
627
627
|
|
|
628
628
|
if (errors && errors.length > 0) {
|
|
629
629
|
throw new Error('MJML compilation errors: ' + errors.map(e => e.formattedMessage).join('; '))
|
|
@@ -789,15 +789,18 @@ export function registerMjmlComponents(): void {
|
|
|
789
789
|
addTemplate({
|
|
790
790
|
filename: "nge/use-template.ts",
|
|
791
791
|
write: true,
|
|
792
|
-
getContents: () => `import {
|
|
793
|
-
import type {
|
|
792
|
+
getContents: () => `import { ref, watch, toRef } from 'vue'
|
|
793
|
+
import type { Ref } from 'vue'
|
|
794
794
|
import Handlebars from 'handlebars'
|
|
795
795
|
import { registerMjmlComponents } from './register-components'
|
|
796
796
|
|
|
797
|
-
// Lazy-load mjml-browser only on the client to avoid "window is not defined" during SSR
|
|
798
|
-
|
|
797
|
+
// Lazy-load mjml-browser only on the client to avoid "window is not defined" during SSR.
|
|
798
|
+
// mjml-browser v5+ returns a Promise, so we store the convert function in a ref
|
|
799
|
+
// and resolve the HTML asynchronously via watchEffect.
|
|
800
|
+
type Mjml2Html = (mjml: string) => Promise<{ html: string; errors: unknown[] }>
|
|
801
|
+
const mjml2html: Ref<Mjml2Html | null> = ref(null)
|
|
799
802
|
if (import.meta.client) {
|
|
800
|
-
|
|
803
|
+
import('mjml-browser').then(m => { mjml2html.value = m.default })
|
|
801
804
|
}
|
|
802
805
|
|
|
803
806
|
const mjmlTemplates: Record<string, string> = import.meta.glob(
|
|
@@ -820,13 +823,12 @@ let _componentsRegistered = false
|
|
|
820
823
|
/**
|
|
821
824
|
* Load and render an MJML email template by name.
|
|
822
825
|
* Registers MJML components (Handlebars partials) automatically on first call.
|
|
823
|
-
* Sets the component's render function so no <template> block is needed.
|
|
824
826
|
*
|
|
825
827
|
* @param name - Template name relative to the emails directory (e.g. 'example', 'v1/test')
|
|
826
828
|
* @param props - Reactive props object from defineProps
|
|
827
|
-
* @returns
|
|
829
|
+
* @returns Ref<string> containing the rendered HTML \u2014 use with v-html in your template
|
|
828
830
|
*/
|
|
829
|
-
export function useNgeTemplate(name: string, props: Record<string, unknown>):
|
|
831
|
+
export function useNgeTemplate(name: string, props: Record<string, unknown>): Ref<string> {
|
|
830
832
|
if (!_componentsRegistered) {
|
|
831
833
|
registerMjmlComponents()
|
|
832
834
|
_componentsRegistered = true
|
|
@@ -836,33 +838,30 @@ export function useNgeTemplate(name: string, props: Record<string, unknown>): Co
|
|
|
836
838
|
if (!source) {
|
|
837
839
|
const available = Object.keys(templateMap).join(', ')
|
|
838
840
|
console.error(\`[nuxt-generation-emails] Template "\${name}" not found. Available: \${available}\`)
|
|
839
|
-
|
|
840
|
-
const instance = getCurrentInstance()
|
|
841
|
-
if (instance) instance.render = () => h('div', { innerHTML: fallback.value })
|
|
842
|
-
return fallback
|
|
841
|
+
return ref(\`<pre style="color:red;">Template "\${name}" not found</pre>\`) as Ref<string>
|
|
843
842
|
}
|
|
844
843
|
|
|
845
844
|
const compiled = Handlebars.compile(source)
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
return
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
845
|
+
const renderedHtml = ref('')
|
|
846
|
+
|
|
847
|
+
// mjml-browser v5 returns a Promise \u2014 use watch to resolve async results reactively.
|
|
848
|
+
// Tracks both the mjml2html loader ref AND all accessed props (via the Handlebars compile spread).
|
|
849
|
+
watch(
|
|
850
|
+
[mjml2html, toRef(() => ({ ...props }))],
|
|
851
|
+
async ([convert]) => {
|
|
852
|
+
if (!convert) { renderedHtml.value = ''; return }
|
|
853
|
+
try {
|
|
854
|
+
const mjmlString = compiled({ ...props })
|
|
855
|
+
const result = await convert(mjmlString)
|
|
856
|
+
renderedHtml.value = result.html
|
|
857
|
+
}
|
|
858
|
+
catch (e: unknown) {
|
|
859
|
+
console.error(\`[\${name}] Error rendering MJML:\`, e)
|
|
860
|
+
renderedHtml.value = \`<pre style="color:red;">\${e instanceof Error ? e.message : String(e)}\\n\${e instanceof Error ? e.stack : ''}</pre>\`
|
|
861
|
+
}
|
|
862
|
+
},
|
|
863
|
+
{ immediate: true, deep: true },
|
|
864
|
+
)
|
|
866
865
|
|
|
867
866
|
return renderedHtml
|
|
868
867
|
}
|
|
@@ -21,7 +21,7 @@ declare module 'mjml-browser' {
|
|
|
21
21
|
preprocessors?: Array<(xml: string) => string>
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
function mjml2html(mjml: string, options?: MjmlOptions): MjmlResult
|
|
24
|
+
function mjml2html(mjml: string, options?: MjmlOptions): Promise<MjmlResult>
|
|
25
25
|
export default mjml2html
|
|
26
26
|
}
|
|
27
27
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-generation-emails",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A Nuxt module for authoring, previewing, and sending transactional email templates with MJML and Handlebars.",
|
|
5
5
|
"author": "nullcarry@icloud.com",
|
|
6
6
|
"repository": {
|
|
@@ -66,8 +66,8 @@
|
|
|
66
66
|
"citty": "^0.2.1",
|
|
67
67
|
"consola": "^3.4.2",
|
|
68
68
|
"handlebars": "^4.7.8",
|
|
69
|
-
"mjml": "^
|
|
70
|
-
"mjml-browser": "^
|
|
69
|
+
"mjml": "^5.0.0-alpha.11",
|
|
70
|
+
"mjml-browser": "^5.0.0-alpha.11",
|
|
71
71
|
"pathe": "^2.0.3"
|
|
72
72
|
},
|
|
73
73
|
"peerDependencies": {
|