UNPKG

5.04 kBMarkdownView Raw
1# grunt-newer
2
3Configure [Grunt](http://gruntjs.com/) tasks to run with only those files modified since the last successful run.
4
5## Getting Started
6This plugin requires Grunt `~0.4.1`
7
8If 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.js`](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:
9
10```shell
11npm install grunt-newer --save-dev
12```
13
14Once the plugin has been installed, it may be enabled inside your `gruntfile.js` with this line:
15
16```js
17grunt.loadNpmTasks('grunt-newer');
18```
19
20## The `newer` task
21
22The `newer` task doesn't require any special configuration. To use it, just add `newer` as the first argument when running other tasks.
23
24For example, if you want to run [JSHint](https://npmjs.org/package/grunt-contrib-jshint) on only those files that have been modified since the last successful run, configure the `jshint` task as you would otherwise, and then register a task with `newer` at the front.
25
26```js
27 grunt.initConfig({
28 jshint: {
29 options: {
30 jshintrc: '.jshintrc'
31 },
32 all: {
33 src: 'src/**/*.js'
34 }
35 }
36 });
37
38 grunt.loadNpmTasks('grunt-contrib-jshint');
39 grunt.loadNpmTasks('grunt-newer');
40
41 grunt.registerTask('lint', ['newer:jshint:all']);
42```
43
44With the above configuration, running `grunt lint` will configure your `jshint:all` task to use only files in the `src` config that have been modified since the last successful run of the same task.
45
46Another example is to use the `newer` task in conjunction with `watch`. For example, you might want to set up a watch to run a linter on all your `.js` files whenever any of them changes. With the `newer` task, instead of re-running the linter on all files, you only need to run it on the files that changed.
47
48```js
49 var srcFiles = 'src/**/*.js';
50
51 grunt.initConfig({
52 jshint: {
53 all: {
54 src: srcFiles
55 }
56 },
57 watch: {
58 all: {
59 files: srcFiles,
60 tasks: ['newer:jshint:all']
61 }
62 }
63 });
64
65 grunt.loadNpmTasks('grunt-contrib-jshint');
66 grunt.loadNpmTasks('grunt-contrib-watch');
67 grunt.loadNpmTasks('grunt-newer');
68
69```
70
71With the above configuration, running `grunt jshint watch` will first lint all your files with `jshint` and then set up a watch. Whenever one of your source files changes, the `jshint` task will be run on just the modified file.
72
73
74## The `any-newer` task
75
76The `newer` task described above reconfigures the target task to run with only those files that have been modified since the last run. This works well for tasks that don't generate new files (like linting). When you have a task that generates destination files based on configured source files, you likely want to process all source files if any one of them has been modified since the last run. The `any-newer` task serves this purpose.
77
78For example, if you want to run [UglifyJS](https://npmjs.org/package/grunt-contrib-uglify) on all your source files only when one or more have been modified since the last run, configure the `uglify` task as you would otherwise, and then register a task with `any-newer` at the front.
79
80
81```js
82 grunt.initConfig({
83 uglify: {
84 all: {
85 files: {
86 'dest/app.min.js': 'src/**/*.js'
87 }
88 }
89 }
90 });
91
92 grunt.loadNpmTasks('grunt-contrib-uglify');
93 grunt.loadNpmTasks('grunt-newer');
94
95 grunt.registerTask('minify', ['any-newer:uglify:all']);
96```
97
98With the above configuration, running `grunt minify` will only run the `uglify:all` task if one or more of the configured `src` files have been modified since the last successful run of the same task.
99
100## Options for the `newer` and `any-newer` tasks
101
102In most cases, you shouldn't need to add any special configuration for the `newer` or `any-newer` tasks. Just `grunt.loadNpmTasks('grunt-newer')` and you can use the tasks. The single option below is available if you need a custom configuration.
103
104#### <a id="optionstimestamps">options.timestamps</a>
105 * type: `string`
106 * default: `node_modules/grunt-newer/.cache`
107
108To keep track of timestamps for successful runs, the `newer` and `any-newer` tasks write to a cache directory. The default is to use a `.cache` directory within the `grunt-newer` installation directory. If you need timestamp info to be written to a different location, configure the task with a `timestamps` option.
109
110Example use of the `timestamps` option:
111
112```js
113 grunt.initConfig({
114 newer: {
115 options: {
116 timestamps: 'path/to/custom/cache/directory'
117 }
118 }
119 });
120```
121
122## That's it
123
124Please [submit an issue](https://github.com/tschaub/grunt-newer/issues) if you encounter any trouble. Contributions or suggestions for improvements welcome!
125
126[![Current Status](https://secure.travis-ci.org/tschaub/grunt-newer.png?branch=master)](https://travis-ci.org/tschaub/grunt-newer)