Skip to content

Instantly share code, notes, and snippets.

@benib
Created September 15, 2013 21:23
Show Gist options
  • Save benib/6574438 to your computer and use it in GitHub Desktop.
Save benib/6574438 to your computer and use it in GitHub Desktop.
Replace filenames after grunt-filerev
grunt.registerMultiTask('filerevreplace', 'filerev task', function() {
var files = grunt.file.expand(this.files[0].src)
var assets = grunt.filerev.summary;
for (var i = 0, length = files.length; i < length; i++) {
var fileContent = grunt.file.read(files[i]);
for (var key in assets) {
origArr = key.split('/');
origFile = origArr[origArr.length-1];
revArr = assets[key].split('/');
revFile = revArr[revArr.length-1];
//this regex is not safe, it doesn't look if the filename is inside <link href=""> or <script src=""> or in css file
searchRegex = new RegExp(origFile,'g');
fileContent = fileContent.replace(searchRegex,revFile);
}
grunt.file.write(files[i], fileContent);
grunt.log.writeln('saved ' + files[i] + ' with probably replaced filenames');
}
});
filerevreplace: {
all: {
src: ['path/to/files/**/*.{js,html,css}']
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment