-
-
Save migreva/2a926b95f25366da657c to your computer and use it in GitHub Desktop.
Less + newer: Watch for imported file changes
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(details, include) { | |
if (details.task === 'less') { | |
checkForNewerImports(details.path, details.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