Skip to content

Instantly share code, notes, and snippets.

@migreva
Forked from cgmartin/gist:10328349
Last active January 15, 2016 03:44
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save migreva/2a926b95f25366da657c to your computer and use it in GitHub Desktop.
Save migreva/2a926b95f25366da657c to your computer and use it in GitHub Desktop.
Less + newer: Watch for imported file changes
//
// grunt-newer:
// Check for newer @import .less files example
// See: https://github.com/tschaub/grunt-newer/issues/29
//
grunt.initConfig({
// ...
newer: {
options: {
override: function(details, include) {
if (details.task === 'less') {
checkForNewerImports(details.path, details.time, include);
}
else {
include(false);
}
}
}
}
// ...
});
var fs = require('fs');
var path = require('path');
function checkForNewerImports(lessFile, mTime, include) {
fs.readFile(lessFile, "utf8", function(err, data) {
var lessDir = path.dirname(lessFile),
regex = /@import "(.+?)(\.less)?";/g,
shouldInclude = false,
match;
while ((match = regex.exec(data)) !== null) {
// All of my less files are in the same directory,
// other paths may need to be traversed for different setups...
var importFile = lessDir + '/' + match[1] + '.less';
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