Append a unique hash to the end of a filename for cache busting.
Append a unique hash to the end of a filename for cache busting. For example:
examples/test1.js => examples/dist/test1.b93fd451.js
##Grunt 0.4
This task now depends on grunt 0.4.x. Please see the [grunt 0.3 to 0.4 migration guide][migration_guide] for more details.
If you are still using grunt 0.3, please install grunt-hash 0.1x
npm install grunt-hashThen add this line to your project's grunt.js gruntfile:
``javascript`
grunt.loadNpmTasks('grunt-hash');
[grunt]: http://gruntjs.com/
[getting_started]: https://github.com/gruntjs/grunt/blob/master/docs/getting_started.md
`javascriptkey
grunt.initConfig({
hash: {
options: {
mapping: 'examples/assets.json', //mapping file so your server can serve the right files
srcBasePath: 'examples/', // the base Path you want to remove from the string in the mapping filevalue
destBasePath: 'out/', // the base Path you want to remove from the string in the mapping filekey
flatten: false, // Set to true if you don't want to keep folder structure in the value in the mapping file``
hashLength: 8, // hash length, the max value depends on your hash function
hashFunction: function(source, encoding){ // default is md5
return require('crypto').createHash('sha1').update(source, encoding).digest('hex');
}
},
js: {
src: 'examples/*.js', //all your js that needs a hash appended to it
dest: 'examples/dist/js/' //where the new files will be created
},
css: {
src: 'examples/*.css', //all your css that needs a hash appended to it
dest: 'examples/dist/css/' //where the new files will be created
}
}
});
grunt.loadNpmTasks('grunt-hash');
Configuration follow the multi-task standard configuration format: http://gruntjs.com/configuring-tasks
[migration_guide]: https://github.com/gruntjs/grunt/wiki/Upgrading-from-0.3-to-0.4