lelinhtinh / jEpub

Simple EPUB builder library, works in modern browsers.
https://lelinhtinh.github.io/jEpub/
ISC License
46 stars 19 forks source link

Puzzling ejs error in production #25

Closed cosmin-novac closed 3 years ago

cosmin-novac commented 3 years ago

Everything is working fine locally, but as soon as I deploy the project, it seems that ejs has an issue with a template. I couldn't figure out what's wrong and can't reproduce locally... any ideas?

Uncaught (in promise) ReferenceError: ejs:3
    1| <?xml version="1.0" encoding="UTF-8" ?>
    2| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
 >> 3| <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%= i18n.code %>">
    4| 
    5| <head>
    6|  <title><%= i18n.info %></title>

i is not defined
    at escapeFn (eval at compile (e2bc53435618f67931716dd55169ffaa66bb2a34.js?meteor_js_resource=true:formatted:21840), <anonymous>:4:72)
    at eval (eval at compile (e2bc53435618f67931716dd55169ffaa66bb2a34.js?meteor_js_resource=true:formatted:21840), <anonymous>:26:16)
    at Object.n.render (e2bc53435618f67931716dd55169ffaa66bb2a34.js?meteor_js_resource=true:formatted:21768)
    at b.init (e2bc53435618f67931716dd55169ffaa66bb2a34.js?meteor_js_resource=true:formatted:21246)
    at generateEPUB (e2bc53435618f67931716dd55169ffaa66bb2a34.js?meteor_js_resource=true:formatted:230087)
    at e2bc53435618f67931716dd55169ffaa66bb2a34.js?meteor_js_resource=true:formatted:229997
    at e2bc53435618f67931716dd55169ffaa66bb2a34.js?meteor_js_resource=true:formatted:493

The code up to that point is as simple as it gets:

var author_name = "xyz";
  var story_name = "abc";
  const options = {
    i18n: 'en',
    title: story_name,
    author: author_name,
    publisher: "somePublisher",
    tags: [ 'epub' ]
  }

  //generate epub
  import ejs from "ejs";
  import jEpub from "jepub/dist/jepub";
  const jepub = new jEpub()
  jepub.init(options);
cosmin-novac commented 3 years ago

Quick note: I've installed ejs v3.1.6 via npm, but I just noticed that jepub comes with its own ejs@2.7.4 postinstall...

lelinhtinh commented 3 years ago

If you're building from source code, it's actually crashing. I'm developing on branch... master (つ﹏<。)

cosmin-novac commented 3 years ago

No, I just installed jEpub via npm, that is definitely the tagged v.2.1.4

And regarding ejs, I went down to v2.7.4 on my npm dependency, and then I removed it completely and just used the module in jEpub, but the error still persists... Any ideas?

lelinhtinh commented 3 years ago

Check it out https://codesandbox.io/s/7thjk

cosmin-novac commented 3 years ago

Hm, I'm not sure what I'm supposed to be looking at? Setting the global.ejs? Or adding metadata console logs to debug? Locally it's working fine, there is no issue.

cosmin-novac commented 3 years ago

I forked the repo and tried bumping ejs to the latest version as well as hardcoding the i18n labels, but the error persists...

cosmin-novac commented 3 years ago

I just ran the project with the production flag activated, and now I can reproduce the issue locally. So I guess it must have something to do with dev dependencies...

lelinhtinh commented 3 years ago

I created a branch on the last commit that it was working on.

git checkout develop
npm install
npm start
cosmin-novac commented 3 years ago

Thanks.

In case someone else is struggling with this: I identified the cause of the issue, I'm using meteor's standard minifier in production. It seems minifying js files messes with ejs templates. I'm now working to either skip minification for this package completely, or to identify what part can't be properly minified.

cosmin-novac commented 3 years ago

Ok, so after some struggle, the solution was quite simple. I loaded both ejs and jepub directly from a CDN instead of adding them via the dependency tree. This way they simply skip any build/minimization process.

I think both the webpack minification and the jake postinstall had caused conflicts with my own minifier, so the pre-built versions solved it.

Thanks for the support!