diff --git a/package.json b/package.json index 87072ab..6e87274 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@xtr-dev/payload-mailing", - "version": "0.0.10", + "version": "0.0.11", "description": "Template-based email system with scheduling and job processing for PayloadCMS", "type": "module", "main": "dist/index.js", diff --git a/src/services/MailingService.ts b/src/services/MailingService.ts index 1765770..a3429ff 100644 --- a/src/services/MailingService.ts +++ b/src/services/MailingService.ts @@ -18,7 +18,7 @@ export class MailingService implements IMailingService { private transporter!: Transporter | any private templatesCollection: string private emailsCollection: string - private liquid: Liquid | null = null + private liquid: Liquid | null | false = null constructor(payload: Payload, config: MailingPluginConfig) { this.payload = payload @@ -84,15 +84,15 @@ export class MailingService implements IMailingService { } private async initializeLiquidJS(): Promise { - if (this.liquid) return // Already initialized + if (this.liquid !== null) return // Already initialized or failed try { - const liquidModule = await Function('return import("liquidjs")')() as any + const liquidModule = await import('liquidjs') as any const { Liquid: LiquidEngine } = liquidModule this.liquid = new LiquidEngine() // Register custom filters (equivalent to Handlebars helpers) - if (this.liquid) { + if (this.liquid && typeof this.liquid !== 'boolean') { this.liquid.registerFilter('formatDate', (date: any, format?: string) => { if (!date) return '' const d = new Date(date) @@ -124,7 +124,7 @@ export class MailingService implements IMailingService { } } catch (error) { console.warn('LiquidJS not available. Falling back to simple variable replacement. Install liquidjs or use a different templateEngine.') - this.liquid = null + this.liquid = false // Mark as failed to avoid retries } } @@ -404,7 +404,7 @@ export class MailingService implements IMailingService { if (engine === 'liquidjs') { try { await this.initializeLiquidJS() - if (this.liquid) { + if (this.liquid && typeof this.liquid !== 'boolean') { return await this.liquid.parseAndRender(template, variables) } } catch (error) { @@ -430,8 +430,7 @@ export class MailingService implements IMailingService { private async renderWithMustache(template: string, variables: Record): Promise { try { - // Dynamic import with proper typing - const mustacheModule = await Function('return import("mustache")')() as any + const mustacheModule = await import('mustache') as any const Mustache = mustacheModule.default || mustacheModule return Mustache.render(template, variables) } catch (error) { diff --git a/src/types/mustache.d.ts b/src/types/mustache.d.ts new file mode 100644 index 0000000..85a34b3 --- /dev/null +++ b/src/types/mustache.d.ts @@ -0,0 +1,7 @@ +declare module 'mustache' { + interface MustacheStatic { + render(template: string, view?: any, partials?: any, tags?: string[]): string + } + const mustache: MustacheStatic + export = mustache +} \ No newline at end of file