Skip to content

Instantly share code, notes, and snippets.

@augbog
Forked from madsleejensen/checkFileForModifiedImports
Last active August 29, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save augbog/af2296319406947e685c to your computer and use it in GitHub Desktop.
Save augbog/af2296319406947e685c to your computer and use it in GitHub Desktop.
grunt-newer configuration to only preprocess scss files that have imported files that have changed
# grunt-newer:
# Check for newer @import .scss files example
# See: https://github.com/tschaub/grunt-newer/issues/29
newer: {
options: {
override: function(details, include) {
if (details.task === 'sass') {
checkForNewerImports(details.path, details.time, include);
}
else {
include(false);
}
function checkForNewerImports(scssFile, mTime, include) {
fs.readFile(scssFile, "utf8", function(err, data) {
var scssDir = path.dirname(scssFile),
regex = /\@import \"(.+?)(\.scss)?\"\;/g,
shouldInclude = false,
match;
while ((match = regex.exec(data)) !== null) {
// i.e. @import "modules/header/file";
var scssDirPath = match[1].substr(0, match[1].lastIndexOf('/')); // modules/header
var scssPartial = match[1].substr(match[1].lastIndexOf('/')+1); // file
// because scss files are formatted as _partial.scss we have to parse and add it in
var importFile = scssDir + '/' + scssDirPath + '/_' + scssPartial + '.scss';
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