Skip to content

Instantly share code, notes, and snippets.

@nwade
Last active August 29, 2015 14:21
Show Gist options
  • Save nwade/863ef19284b012a7f73f to your computer and use it in GitHub Desktop.
Save nwade/863ef19284b012a7f73f to your computer and use it in GitHub Desktop.
How to get grunt-newer to compile parent jade templates that include modified templates
grunt.initConfig({
// ...
newer: {
options: {
override: function (detail, include) {
if (detail.task === 'jade') {
checkForNewerImports(detail.path, detail.time, include);
} else {
include(false);
}
}
}
},
watch: {
jade: {
files: ['<%= yeoman.app %>/**/*.jade'],
tasks: ['newer:jade']
},
// ...
},
// ...
});
// ...
var fs = require('fs');
var path = require('path');
function checkForNewerImports(jadeFile, mTime, include) {
fs.readFile(jadeFile, 'utf8', function (err, data) {
var jadeDir = path.dirname(jadeFile),
// be careful with this regex - specific to my project's needs - change as needed.
regex = /include (.+)/g,
shouldInclude = false,
match;
while ((match = regex.exec(data)) !== null) {
var importFile = jadeDir + '/' + match[1] + '.jade';
if (fs.existsSync(importFile)) {
var stat = fs.statSync(importFile);
if (stat.mtime > mTime) {
shouldInclude = true;
break;
}
}
}
include(shouldInclude);
});
}
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment