Rename assets files with hash for better caching and add to JSON file
This plugin requires Grunt ~0.4.1
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install grunt-assets-hash --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks('grunt-assets-hash');
This plugin is based in grunt-asset-version-json by Andy Ford
assets_hash: {
assets: {
options: {
algorithm: 'sha1',
jsonFile: 'assets/versions.json',
clear: true
},
src: 'public/assets/css/main.css'
}
},
This example task will rename public/assets/css/main.css
to public/assets/css/main.{sha1hash}.css
and update assets reference in assets/versions.json
which would look something like { "public/assets/css/main.css": "public/assets/css/main.SOMEHASH.css" }
;
Type: Boolean
Default: false
It will rename the src
target instead of copy.
Type: Boolean
Default: false
Define the file name format as suffixed or prefixed.
true: {filename}.{hash}.{ext}
false: {hash}.{filename}.{ext}
Type: String
Default: 'utf8'
The file encoding.
Type: String
Default: 'md5'
algorithm
is dependent on the available algorithms supported by the version of OpenSSL on the platform. Examples are 'sha1'
, 'md5'
, 'sha256'
, 'sha512'
, etc. On recent releases, openssl list-message-digest-algorithms
will display the available digest algorithms.
Type: Number
Default: 8
The number of characters of the file hash to prefix the file name with.
Type: String
Default: assets-hash.json
The JSON file that will contain all reference to files and hashs. If that file doesn't exists, will be created.
Type: Boolean
Default: false
If true, will delete old hashed file.
Type: Boolean
Default: true
If true, the paths will be present in JSON file.
Type: String
Default: ``
If this string is not empty, will be removed from path.
Example: if you hash public/assets/main.90ea3212.css
and removeFromPath is set to public/
your JSON will contain only assets/main.90ea3212.css