Luismahou / grunt-hashres

Hashes your js and css files and rename the <script> and <link> declarations that refer to them in your html/php/etc files.
MIT License
115 stars 41 forks source link

DEPRECATION NOTICE

This project is not maintained anymore. I moved to webpack, and you should too. :smile:

grunt-hashres has been updated to Grunt 0.4. if you're still using Grunt 0.3.x go to old version documentation

grunt-hashres

Build Status

Hashes your js and css files and rename the <script> and <link> declarations that refer to them in your html/php/etc files.

Getting Started

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

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

grunt.loadNpmTasks('grunt-hashres');

Documentation

Add the following to your Gruntfile.js file, inside the initConfig function:

hashres: {
  // Global options
  options: {
    // Optional. Encoding used to read/write files. Default value 'utf8'
    encoding: 'utf8',
    // Optional. Format used to name the files specified in 'files' property.
    // Default value: '${hash}.${name}.cache.${ext}'
    fileNameFormat: '${hash}.${name}.cache.${ext}',
    // Optional. Should files be renamed or only alter the references to the files
    // Use it with '${name}.${ext}?${hash} to get perfect caching without renaming your files
    // Default value: true
    renameFiles: true
  },
  // hashres is a multitask. Here 'prod' is the name of the subtask. You can have as many as you want.
  prod: {
    // Specific options, override the global ones
    options: {
      // You can override encoding, fileNameFormat or renameFiles
    },
    // Files to hash
    src: [
      // WARNING: These files will be renamed!
      'dist/prod/scripts/my-compressed-and-minified-scripts.js',
      'dist/prod/styles/my-compressed-and-minified-styles.css'],
    // File that refers to above files and needs to be updated with the hashed name
    dest: 'dist/prod/home.php',
  }
}

Recommended workflow

grunt-hashres, as a general rule, should be run when you're going to release your code. Ideally, you should create a stage folder where you'll copy your html, minified js and css and all your resources. And then, on this clean copy, hash the resource names.

Alternative workflow

Due to popular demand, the task support to update references that where already hashed. This means, that you won't need to create a stage folder before running grunt-hashres. See #26 and #29 for more info.

Heads up

If you have upgraded from Grunt 0.3 version: 'files' and 'out' config properties have been replaced by 'src' and 'dest'

Properties

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.

Release History

License

Copyright (c) 2013 Luismahou Licensed under the MIT license.