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); | |
}); | |
} |
@tschaub thanks for catching that... had copied from two separate sources. Fixed and included some missing requires.
is that a stylus version? And how to use it?
I don't think this is a good solution, by checking the mTime, it results in compiling the script no matter what.
Helped me a lot, thanks!
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:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice. The function should be named
checkForNewerImports
to be consistent with the config. Or change the config to match.