2

I have a directory with a bunch of jade templates, and a grunt task that compiles all of them to individual html files.

I'd like to have a watch task that recompiles a template when it changes, but right now my task recompiles every template when any of them change.

Here is a demo in a gist.

Is there a succinct way to write a task that recompiles a template when it changes, but not all of the other templates?

aaronstacy
  • 6,189
  • 13
  • 59
  • 72
  • 1
    does one of these options work for you? http://stackoverflow.com/questions/17551720/running-grunt-contrib-jshint-only-on-recently-modified-files – explunit Aug 12 '13 at 19:57
  • yep, adding a 'filter' to the files config was the solution. i'll post an answer soon. – aaronstacy Aug 15 '13 at 17:49

1 Answers1

0

the solution is to add a filter function to the files list:

var fs = require('fs');
var join = require('path').join;
module.exports = function(grunt) {
  grunt.initConfig({
    jade: {
      files: {
        src: ['*.jade'],
        dest: './',
        expand: true,
        ext: '.html',
        filter: function(destDir, src) {
          var dest = join(destDir, src.replace(/jade$/, 'html'));
          var destMod;
          try {
              destMod = +fs.lstatSync(dest).mtime;
          } catch (e) {
              if (e.code === 'ENOENT') {
                  // there's no html file, so ensure that the
                  // jade file is compiled by returning true
                  return true;
              }
              throw e;
          }
          return fs.lstatSync(src).mtime > destMod;
        }
      }
    },
    watch: {
      files: ['*.jade'],
      tasks: ['jade']
    }
  });

  grunt.loadNpmTasks('grunt-contrib-jade');
  grunt.loadNpmTasks('grunt-contrib-watch');
};
aaronstacy
  • 6,189
  • 13
  • 59
  • 72