Forked from madsleejensen/checkFileForModifiedImports
Last active
August 29, 2015 14:28
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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