├── .gitignore ├── .jshintrc ├── Gruntfile.js ├── LICENSE-MIT ├── README.md ├── demo ├── css │ └── hello.css ├── index.html └── js │ └── hello.js ├── package.json └── tasks └── cache.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | tmp 4 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "curly": true, 3 | "eqeqeq": true, 4 | "immed": true, 5 | "latedef": true, 6 | "newcap": true, 7 | "noarg": true, 8 | "sub": true, 9 | "undef": true, 10 | "boss": true, 11 | "eqnull": true, 12 | "node": true 13 | } 14 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | /* 2 | * asset-cache-control 3 | * https://github.com/jessiehan/asset-cache-control 4 | * 5 | * Copyright (c) 2013 hpp 6 | * Licensed under the MIT license. 7 | */ 8 | 9 | 'use strict'; 10 | 11 | module.exports = function(grunt) { 12 | 13 | // Project configuration. 14 | grunt.initConfig({ 15 | 16 | // Before generating any new files, remove any previously-created files. 17 | clean: { 18 | tests: ['tmp'], 19 | }, 20 | 21 | // Configuration to be run (and then tested). 22 | cache: { 23 | js: { 24 | options: { 25 | }, 26 | assetUrl:'demo/js/hello.js', 27 | files: { 28 | 'tmp': ['demo/index.html'], 29 | }, 30 | }, 31 | css: { 32 | options: { 33 | }, 34 | assetUrl:'demo/css/hello.css', 35 | files: { 36 | 'tmp': ['demo/index.html'], 37 | }, 38 | }, 39 | }, 40 | 41 | // Unit tests. 42 | nodeunit: { 43 | tests: ['test/*_test.js'], 44 | }, 45 | 46 | }); 47 | 48 | // Actually load this plugin's task(s). 49 | grunt.loadTasks('tasks'); 50 | 51 | // These plugins provide necessary tasks. 52 | grunt.loadNpmTasks('grunt-contrib-clean'); 53 | grunt.loadNpmTasks('grunt-contrib-nodeunit'); 54 | 55 | // Whenever the "test" task is run, first clean the "tmp" dir, then run this 56 | // plugin's task(s), then test the result. 57 | //grunt.registerTask('test', ['clean', 'cache', 'nodeunit']); 58 | 59 | // By default, lint and run all tests. 60 | grunt.registerTask('default', ['cache']); 61 | 62 | }; 63 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 hpp 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # asset-cache-control 2 | 3 | control the cache of assets by appending md5 hash to asset url 4 | 5 | For example 6 | 7 | in index.html we have 8 | 9 | ```html 10 | 11 | ``` 12 | 13 | 14 | after running the task 15 | 16 | ```html 17 | 18 | ``` 19 | ## Getting Started 20 | This plugin requires Grunt `~0.4.1` 21 | 22 | If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command: 23 | 24 | ```shell 25 | npm install asset-cache-control --save-dev 26 | ``` 27 | 28 | Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript: 29 | 30 | ```js 31 | grunt.loadNpmTasks('asset-cache-control'); 32 | ``` 33 | 34 | ## The "cache" task 35 | 36 | ### Overview 37 | In your project's Gruntfile, add a section named `cache` to the data object passed into `grunt.initConfig()`. 38 | 39 | ```js 40 | grunt.initConfig({ 41 | cache: { 42 | js: { 43 | options: { 44 | }, 45 | assetUrl:'demo/js/hello.js', 46 | files: { 47 | 'tmp': ['demo/index.html'], 48 | }, 49 | }, 50 | }, 51 | }); 52 | ``` 53 | 54 | 55 | 56 | ### Usage Examples 57 | 58 | #### Default Options 59 | In this example, we have index.html which contains hello.js and hello.css. 60 | In Gruntfile.js, write as below, then `grunt`, we can get the index.html which has assets url with md5. 61 | 62 | `assetUrl` is the css or js file path 63 | `files` is the file which contains the assets(usually is html file) 64 | 65 | Notice to write the correct path. 66 | 67 | ```js 68 | grunt.initConfig({ 69 | cache: { 70 | js: { 71 | options: { 72 | }, 73 | assetUrl:'demo/js/hello.js', 74 | files: { 75 | 'tmp': ['demo/index.html'], 76 | }, 77 | }, 78 | css: { 79 | options: { 80 | }, 81 | assetUrl:'demo/css/hello.css', 82 | files: { 83 | 'tmp': ['demo/index.html'], 84 | }, 85 | }, 86 | }, 87 | }); 88 | ``` 89 | 90 | 91 | ## Contributing 92 | 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](http://gruntjs.com/). 93 | 94 | ## Release History 95 | _(Nothing yet)_ 96 | -------------------------------------------------------------------------------- /demo/css/hello.css: -------------------------------------------------------------------------------- 1 | .hello{ 2 | color:#fff; 3 | } -------------------------------------------------------------------------------- /demo/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |