Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] issue with eta.js #458

Open
3 tasks done
floriannari opened this issue Mar 5, 2024 · 5 comments
Open
3 tasks done

[fix] issue with eta.js #458

floriannari opened this issue Mar 5, 2024 · 5 comments
Labels

Comments

@floriannari
Copy link

floriannari commented Mar 5, 2024

Describe the bug

@ladjs/consolidate configures eta.js to expect a path relative to "."

new (require('eta').Eta)({
  views: '.'
})

https://github.com/ladjs/consolidate/blob/master/lib/consolidate.js#L763

So when email-templates passes it an absolute path, eta executes path.join('.', '/absolute/path/html.eta'); which removes the first "/".
https://github.com/eta-dev/eta/blob/main/src/file-handling.ts#L72

So, the requested file is not found
EtaError [Eta Error]: Could not find template: absolute/path/html.eta
at Eta.readFile (node_modules/eta/dist/eta.umd.js:593:15)
at Eta.handleCache (node_modules/eta/dist/eta.umd.js:457:37)
at Eta.render (node_modules/eta/dist/eta.umd.js:483:32)
at node_modules/@ladjs/consolidate/lib/consolidate.js:765:23
at node_modules/@ladjs/consolidate/lib/consolidate.js:169:5
at new Promise ()
at promisify (node_modules/@ladjs/consolidate/lib/consolidate.js:158:10)
at exports.eta (node_modules/@ladjs/consolidate/lib/consolidate.js:758:10)
at node:internal/util:375:7
at new Promise ()

By replacing

new (require('eta').Eta)({
  views: '.'
})

by

new (require('eta').Eta)({
  views: '/'
})

This solves our problem, but it would affect people using @ladjs/consolidate with relative paths

Node.js version: v18.17.1

OS version: Debian 11

Description:

Actual behavior

email-templates don't work with eta.js templating engine

Expected behavior

email-templates should work with eta.js templating engine

Code to reproduce

app.mjs

import * as nodemailer from "nodemailer";
import EmailTemplates from 'email-templates';

const transporter = nodemailer.createTransport({
    host: "https://smtp.myserver.fr",
    port: 25
});

const emailTemplates = new EmailTemplates({
    message: {
        from: "tutu@mail.com"
    },
    views: {
        root: "./email_templates",
        options: {
            extension: 'eta'
        }
    },
    transport: transporter
});


const mailOptions = {
    text: "toto",
    to: "toto@mail.com",
    subject: "tata",
}


emailTemplates.send({
    template: "a_template",
    message: mailOptions,
    locals: {
        name: "titi"
    },
})

email_templates/a_template/html.eta

Hi <%= it.name %> !

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.
@floriannari floriannari added the bug label Mar 5, 2024
@floriannari
Copy link
Author

(I'm aware that this is mostly an @ladjs/consolidate bug and not an email-template bug. But they don't allow issues, and their project is maintained by forwardemail)

@exoup
Copy link

exoup commented Mar 6, 2024

I'll note, this issue is not reproducible in a Windows environment. (That is, both absolute and relative paths just work)

In Linux (Ubuntu), the error does occur.
It does seem to resolve if I prepend an extra . to the beginning of my path although I only have a headless environment to test in, so I can't confirm it's properly loaded in a browser.

@floriannari, can you confirm?

You may also be able to use a custom render function to pass in a custom eta instance, similar to (the docs)[https://github.com/forwardemail/email-templates/tree/master?tab=readme-ov-file#custom-rendering-eg-from-a-mongodb-database]

I'll admit that's not the best DX though.

@floriannari
Copy link
Author

It does seem to resolve if I prepend an extra . to the beginning of my path

I'm not sure to understand where to add the "."?

@exoup
Copy link

exoup commented Mar 6, 2024

In the views root path.

const emailTemplates = new EmailTemplates({
    message: {
        from: "tutu@mail.com"
    },
    views: {
        root: "../email_templates",
        options: {
            extension: 'eta'
        }
    },
    transport: transporter
});

@floriannari
Copy link
Author

floriannari commented Mar 6, 2024

I have the impression that in this case, email-templates notices that the template doesn't exist (because it's in "./email-templates" and not "../email-templates").

And so @ladjs/consolidate (and therefore eta.js) are simply not called.

edit :
github.com/forwardemail/email-templates/blob/master/index.js#L192

const exists = await this.templateExists(string);
if (!exists) return;

return this.render( // ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants