vitkarpov / grunt-nunjucks-2-html

Compiles nunjucks templates *right* into HTML
MIT License
35 stars 11 forks source link

Async filters issues #47

Closed ArmorDarks closed 7 years ago

ArmorDarks commented 8 years ago

Just right now saw that async filters causing issues to calculation of processed pages.

With filter:

env.addFilter 'Icon', (name, complete) ->
  icon = grunt.file.read "source/icons/#{name}.svg"

  svgo = new SVGO()

  optimize = (icon) -> return new Promise (resolve) ->
    svgo.optimize icon, (result) ->
      resolve(result)

  optimize(icon)
    .then (result) =>
      complete(null, result.data)
, true

(svgo,optimize is async and it forcing us to use async filter)

grunt nunjucks will result in

Running "nunjucks:build" (nunjucks) task
>> 0/1 file compiled.

However, template actually compiled and no errors thrown. I guess, somehow async filter makes count of compiled pages improper.

vitkarpov commented 8 years ago

Oh, I see what you're talking about. I need to support async filters. Thanks!

ArmorDarks commented 8 years ago

Well, the way we've implemented Nunjucks render is already right and it's already async-capable, it uses proper callback, required by Nunjucks for async filters.

I think the issues is with Promise itself, since when I was writing it I had very little to no experience with Promises.

vitkarpov commented 8 years ago

Maybe you can point to the original gruntfile? (drop the listing of code or a link, if there's on github) If so, I can take a look.

ArmorDarks commented 8 years ago

Hm, it seems to be an error in my filter's promise somewhere.

I've changed it to not use Promise, just to dish out everything in svgo callback and suddenly it started to work:

# env.addFilter 'Icon', (name, width, height, viewBox, preserveAspectRatio, complete) ->
env.addFilter 'Icon', (name, complete) ->
  icon = grunt.file.read "source/icons/#{name}.svg"

  svgo = new SVGO()

  svgo.optimize icon, (result) ->
    complete(null, result.data)
, true
>> 1/1 file compiled.

Besides, my original point was wrong. With code from my first post in place nunjucks-2-html indeed processed 0/1 templates, no templates actually were rendered. So, I can't figure out why did that happened.

vitkarpov commented 8 years ago

Things happen :)

ArmorDarks commented 8 years ago

Let that issue hang around for a while, I will letter do some checks to understand is it's my Promise issue, or nunjucks-2-html indeed has issues with counting during async calls.

vitkarpov commented 7 years ago

@ArmorDarks hey, hope you're doing well. So, have you taken a look at this once again?

vitkarpov commented 7 years ago

trying to resurrect work here a little bit 😆

ArmorDarks commented 7 years ago

Ouch, completely forgot about this one. I think it was an error in my filter, since I wasn't able to reproduce issue.

vitkarpov commented 7 years ago

Sure, no worries, man 👌