Last active
August 29, 2015 13:58
-
-
Save cgmartin/10328349 to your computer and use it in GitHub Desktop.
grunt-newer: Check for newer @import .less files
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 .less files example | |
// See: https://github.com/tschaub/grunt-newer/issues/29 | |
// | |
grunt.initConfig({ | |
// ... | |
newer: { | |
options: { | |
override: function(taskName, targetName, filePath, time, include) { | |
if (taskName === 'less') { | |
// call include with `true` if there are newer imports | |
checkForNewerImports(filePath, 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
Loved this, thanks. Looks like grunt-newer updated their API again for the override function:
https://github.com/tschaub/grunt-newer#optionsoverride
I forked and updated the gist here if you wanna take a look:
https://gist.github.com/migreva/2a926b95f25366da657c