vitkarpov / grunt-nunjucks-2-html

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

Compile only changed files #49

Closed johnruane closed 7 years ago

johnruane commented 7 years ago

I have nunjucks setup as:

        nunjucks: {
            options: {
                data: grunt.file.readJSON('data.json')
            },
            render: {
                files: [{
                    expand: true,
                    src: ['**/*.nunjucks'],
                    dest: "build/",
                    ext: ".html"
                }]
            }
        },

The 'grunt-contrib-watch' plugin has the ability to pass in to a task a list of the files that have changed when saving. Has anyone got this working as I have tried everything i can think of for 2 days.

It works for 'jshint' plugin. Is nunjucks-2-html different in its workings?

This is the documented way of changing a grunt attribute: grunt.config('nunjucks.render.files.src', Object.keys(changedFiles)); but once you specify a 'src' in nunjucks task it won't change.

vitkarpov commented 7 years ago

Hey, @johnteh !

Frankly, I don't get it, can you elaborate? Seems it'll be more convenient if you describe the code, the way it should work and the way it actually works.

ArmorDarks commented 7 years ago

You can achieve this by using grunt-newer, and writing your watch task like so

But don't forget about partials or files with exported macros — since those files don't have anything to render inside of them, any attempts to use newer or similar task on them will result in zero updated in related templates. For such cases best you can do is to re-render all template. Or write your own dependency management tool for Nunjucks :D.

vitkarpov commented 7 years ago

@ArmorDarks 🔥 👌

johnruane commented 7 years ago

Apologies for the bad description.

@ArmorDarks you have highlighted an issue i had not thought about relating to the changed files. I have partials and macros in all my templates so making updates to these will not compile the correct file.

Im going to have to think about how/if it's possible to find all templates that contain any partial or macro being worked on.

Thanks for your time.

ArmorDarks commented 7 years ago

One of workarounds is to always store shared macros in standalone files. Like so.

In fact, it will make project structure cleaner.

johnruane commented 7 years ago

@ArmorDarks thank you for the advice. Some great code commenting ideas.

We already store our macros in separate files and we have found that by watching for changes on templates only, any macro changes require a small extra step of saving the parent template in order to see the change. Thats fine as it's got our compilation time down from 28s to less than 1!

ArmorDarks commented 7 years ago

Btw, it may be worth to enable Nunjucks cache. In our case it shimmed rendering of 200 pages from 25s to 8s.

Right now it isn't possible with this task, but here is PR https://github.com/vitkarpov/grunt-nunjucks-2-html/pull/50 which will allow to pass noCache: false to Nunjucks.

morganfeeney commented 7 years ago

@ArmorDarks using noCache: false on all the files combined with newer for templates FTW!

Great advice.