meinaart / grunt-hash

a grunt plugin for appending hashes to files for cache busting

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

grunt-hash

Calculates a unique hash of a file. This hash can be appended to the filename. For example:

examples/test1.js => examples/dist/test1.b93fd451.js

It can also add the hash as a comment to javascript, CSS, PHP and HTML files.

##Grunt 0.4

This task now depends on grunt 0.4.x. Please see the grunt 0.3 to 0.4 migration guide for more details.

If you are still using grunt 0.3, please install grunt-hash 0.1x

Getting Started

Install this grunt plugin next to your project's grunt.js gruntfile with: npm install grunt-hash

Then add this line to your project's grunt.js gruntfile:

grunt.loadNpmTasks('grunt-hash');

Documentation

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 `key` string in the mapping file
			destBasePath: 'out/', // the base Path you want to remove from the `value` string in the mapping file
			flatten: false, // Set to true if you don't want to keep folder structure in the `key` 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');
			},
      comment: true // Add hash as comment to file instead of to filename, supported: js, css, php and html
		},
        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

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.

License

Copyright (c) 2012 Greg Allen
Licensed under the MIT license.

About

a grunt plugin for appending hashes to files for cache busting

License:MIT License


Languages

Language:JavaScript 100.0%