rotundasoftware / nunjucksify

Everything you expect from a module named nunjucksify and more.
34 stars 10 forks source link

Tweak for usage under NodeJS. #20

Closed darrennolan closed 7 years ago

darrennolan commented 7 years ago

Added node only option that stubs out the window object (as it appears to be touched and fails under node) and prefer non-slim nunjucks. Tidy up of using vars and normalised to tabs.

dgbeck commented 7 years ago

HI @darrennolan thx very much for this PR.

Unfortunately the require( "nunjucks" ) statement will make browserify include the source for the main (non-slim) nunjucks file, even though that conditional may not be executed.

I'm not seeing an elegant way around this issue. At a minimum, two different entry points would be needed for the two different cases, and even then, I don't think there is a way to express which entry point should be used when including the transform in package.json files. However that seems like the best option. The only other alternative I see is to create a new package entirely.

How exactly are you applying the transform on the server side?

darrennolan commented 7 years ago

That conditional is not in the path of browserify while at the transform level, it's not put to the output for compiling. It's dependant on the node true/false flag, right? I'm not wanting both to happen at the same time (as in, either I'm compiling source entirely for node use, or I'd disable that flag and have it done as per normal with slim.

Transform is called for inside my package.json.

I started using nunjucks about 3 days ago as a means to do server-side templating inside my express application. Sadly (for better or for worse) - I compile my server source for babel / avoiding node module dependencies on the server. Which when using things that tend to like touching file systems doesn't work very well.

Back when nunjucksify was using 1.x - this all worked fine. Sadly moving to v3 (much needed, v1 was broken for many things particularly functions passed to templates) has broken the ability to use it in this fashion, as even after being pre-compiled the templates are expecting things like the window object to be available.

Naturally accessing the window object in node typically breaks things.

As https://github.com/rotundasoftware/nunjucksify/issues/13#issuecomment-260224416 - this kind of server side bundling may or may not be inline with the goals of this project. I completely understand if it's not :)

dgbeck commented 7 years ago

Ah, you are right! If the nodeOnly flag is false, the require( "nunjucks" ) statement will not be included in the templates, and browserify will therefore only include the slim version. Nice!

Merging, thank you!

dgbeck commented 7 years ago

published @2.1.0.

Thanks again, @darrennolan !