gruntjs / grunt-contrib-pug

Compile Pug templates.
http://gruntjs.com/
MIT License
328 stars 77 forks source link

Anyone else having issues with the filters? #168

Closed zmanring-zz closed 7 years ago

zmanring-zz commented 8 years ago

I have a filter under the options object but when I build it gives me an error.

Options: namespace="JST", separator="\n\n", amd=false, filters={}
Reading index.pug...OK
>> TypeError: Cannot read property 'filters' of undefined
shinnn commented 8 years ago

👉 https://stackoverflow.com/ or https://github.com/pugjs/pug/issues

zmanring-zz commented 8 years ago

Yeah I got the filters working in Pug but not through through the grunt system.

shinnn commented 8 years ago

Are you using the same version? Currently this plugin depends on this version: https://github.com/gruntjs/grunt-contrib-pug/blob/563653da682115af43767aa8d1d90343daab9d70/package.json#L20

zmanring-zz commented 8 years ago

I was on alpha7 and I reverted, same issue.

Example use:

options: {
    data: {
      dist: true
    },
    filters: {
      renderComponent: function(block) {
        return block;
      },
      content: function(block) {
        return block;
      }
    }
ajwild commented 7 years ago

For anyone else who may run into this issue, I fixed it by adding an empty data object to the options. If you look at tasks/pug.js#L69 you can see that f.orig.data can end up being undefined if options.data is not specified, so the check for filters on tasks/pug.js#L72 will throw the error.

Maybe the code could be updated to f.orig.data = data || {};.

franciscop commented 7 years ago

As a side note, if you are require()ing the filters then you'll have to manually restart the grunt process instead of relying on grunt watch, since the require() calls are cached and grunt watch does not invalidate this cache.