vitkarpov / grunt-nunjucks-2-html

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

export `nunjucks` variable during call of `configureEnvironment` #22

Closed ArmorDarks closed 9 years ago

ArmorDarks commented 9 years ago

Sometimes you need access to nunjucks during configuration of environment.

For example, if you need to pre-render some string in custom filter or extension.

vitkarpov commented 9 years ago

Maybe you just need to require('nunjucks') into local variable inside your gruntfile and use it?

ArmorDarks commented 9 years ago

Yeap, that was my initial thought. And it didn't work.

Later I understood why — when you declare require('nunjucks'), Node searching for that module only in root of node_modules, and since there is no such there — it fails to find requested module.

It can't find it, because nunjucks installed as part of grunt-nunjucks-2-html, and to request it from Task body or Gruntfile itself, you need to write full path:

nunjucks = require('./node_modules/grunt-nunjucks-2-html/node_modules/nunjucks')

Unfortunately, it isn't solid solution, since some other task can have Nunjucks in dependency too, then it will be installed to the root, instead of local node_modules of particular task.

Maybe there is another solution for it, which I'm missing.

Of course, we can force nunjucks as main dependency in project's packages.json, then it always will be installed to the root, but it isn't proper way of using packages.json too.

Edit: well, as soon as you start to use exported modules for tasks, even full path like ./node_modules/grunt-nunjucks-2-html/node_modules/nunjucks stops to work.

vitkarpov commented 9 years ago

Of course, we can force nunjucks as main dependency in project's packages.json, then it always will be installed to the root, but it isn't proper way of using packages.json too.

Frankly, at first I thought about it. But seems you use grunt-nunjucks exactly 'cause you don't want to use nunjucks directly :)

So, I think it's ok to merge.

ArmorDarks commented 9 years ago

Thanks

Frankly, at first I thought about it. But seems you use grunt-nunjucks exactly 'cause you don't want to use nunjucks directly :)

Well, I always thinking about possibility that I'm wrong. Maybe there is some way to require dependencies of other packages without forcing it as dep in main packages.json.

Also, maybe forcing nunjucks as main dep isn't wrong way too. To be honest, can't decide for now. It's just feels a bit wrong when you have to add something somewhere what can be avoided just to make something work.

I think that require('nunjucks') would be the most proper way to do that, but I was quite surprised that Node doesn't resolve paths to modules, which aren't part of root.

ArmorDarks commented 9 years ago

Hm, some (maybe) quite strange idea come to my mind, as a workaround:

  try
    nunjucks = require('nunjucks')
  catch e
    nunjucks = require('grunt-nunjucks-2-html/node_modules/nunjucks')

Maybe not best code, but it pretty much works — it will try to load from root first, and if fails — will try to look at module specific folder.

vitkarpov commented 9 years ago

You can explicitly define nunjucks into package.json and use require('nunjucks'). But it's not necessary anymore.

ArmorDarks commented 9 years ago

Yeap, thanks