Created
November 13, 2014 02:07
-
-
Save giiska/ff0ba90f883acff6bd7a to your computer and use it in GitHub Desktop.
auto_create_ts grunt task for auto append timestamp to static files
This file contains 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.registerMultiTask('auto_create_ts', 'The best Grunt plugin ever.', function() { | |
function replaceAssets(fileSrc) { | |
//read page file data | |
var htmlData = grunt.file.read(fileSrc); | |
//get the full asset text, like "text/javascript" src="js/hello.js?auto_create_ts=3232323233" | |
// console.log(/(".*?auto_create_ts=)(\d+)([\"|\'])/g.test(htmlData)); | |
var newdata = htmlData.replace(/(".*?auto_create_ts=)(\d+)([\"|\'])/g, "$1" + (Math.round(+new Date() / 1000)) + "$3"); | |
if (grunt.file.write(fileSrc, newdata)) { | |
grunt.log.success(fileSrc + ' add ts successfully'); | |
} else { | |
grunt.log.error(fileSrc + ' add ts failed'); | |
} | |
} | |
// Iterate over all specified file groups. | |
this.files.forEach(function(f) { | |
// Concat specified files. | |
console.log(f.dest); | |
var src = f.src.filter(function(filepath) { | |
console.log(filepath); | |
return; | |
// Warn on and remove invalid source files (if nonull was set). | |
if (!grunt.file.exists(filepath)) { | |
grunt.log.warn('Source file "' + filepath + '" not found.'); | |
return false; | |
} else { | |
replaceAssets(filepath); | |
return true; | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment