Skip to content

Instantly share code, notes, and snippets.

@madsleejensen
Last active April 11, 2016 16:07
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save madsleejensen/11082646 to your computer and use it in GitHub Desktop.
Save madsleejensen/11082646 to your computer and use it in GitHub Desktop.
# grunt-newer:
# Check for newer @import .less files example
# See: https://github.com/tschaub/grunt-newer/issues/29
newer: {
options: {
/**
* when changing a less file, we run an addional check on all the *.less files to see if they are @importing a modified *.less file, and if so we include it in the files which less task should execute.
*/
override: function(details, shouldIncludeCallback) {
var fs = require('fs');
var path = require('path');
var async = require('async');
var checkFileForModifiedImports = async.memoize(function(filepath, fileCheckCallback) {
fs.readFile(filepath, 'utf8', function(error, data) {
var directoryPath = path.dirname(filepath);
var regex = /@import (?:\([^)]+\) )?"(.+?)(\.less)?"/g
var match;
function checkNextImport() {
if ((match = regex.exec(data)) === null) {
return fileCheckCallback(false); // all @import files has been checked.
}
var importFilePath = path.join(directoryPath, match[1] + '.less');
fs.exists(importFilePath, function(exists) {
if (!exists) { // @import file does not exists.
return checkNextImport(); // skip to next
}
fs.stat(importFilePath, function(error, stats) {
if (stats.mtime > details.time) { // @import file has been modified, -> include it.
fileCheckCallback(true);
}
else { // @import file has not been modified but, lets check the @import's of this file.
checkFileForModifiedImports(importFilePath, function(hasModifiedImport) {
if (hasModifiedImport) {
fileCheckCallback(true);
}
else {
checkNextImport();
}
});
}
});
});
};
checkNextImport();
});
});
// only add override behavior to less tasks.
if (details.task == 'less') {
checkFileForModifiedImports(details.path, function(found) {
shouldIncludeCallback(found);
});
}
else {
shouldIncludeCallback(false);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment