When running a task that processes files in one way or another, you'll soon realize that you probably don't want it to process all the files each time it is performed. This is especially true when the amount of files the task has to process becomes quite large.
This is where the newer (0.7.0)
plugin can help out by ensuring that only the files that have changed since the task's previous run are processed, each time it is called to run. It can be used with any plugin that makes use of the standard files
configuration and becomes especially useful when using a watcher to rerun tasks each time a file change is detected.
In this example, we'll work with the basic project structure that we created in the Setting up Grunt on a project recipe of this chapter. Be sure to refer to it if you are not yet familiar with it's contents.
The following steps take us through making use of the newer
plugin to check the code quality of only the JavaScript source files that have changed since the last run.
We'll start by installing the package containing the
newer
plugin and loading its tasks by following the instructions provided in the Installing a plugin recipe of this chapter.For the purpose of our example, we'll also install the
contrib-jshint
plugin and load its tasks by following the instructions provided in the Installing a plugin recipe of this chapter.With all the required plugins installed, we can now add sample configuration for the
jshint
task, which will perform a code quality check on all the JavaScript files in thesrc
directory. This is done by adding the following to our configuration:jshint: { sample: { src: 'src/**/*.js' } }
Now we can run the
jshint
task a couple of times using thegrunt jshint
command to observe its behavior. Each time we run it, we'll see that it scans all the files in thesrc
directory. This should produce the same output each time, similar to the following:Running "jshint:sample" (jshint) task >> 3 files lint free.
In order to make use of the
newer
plugin, we prependnewer:
to the name of the task we wish to run. On running thegrunt newer:jshint
command the first time, thenewer
plugin will cache the timestamps of the files that have been processed. This produces output similar to the following:Running "newer:jshint" (newer) task Running "newer:jshint:sample" (newer) task Running "jshint:sample" (jshint) task >> 3 files lint free. Running "newer-postrun:jshint:sample:.cache" (newer-postrun) task
When we run the
grunt newer:jshint
command again, we'll see that no files are processed by thejshint
task, which produces output similar to the following:Running "newer:jshint" (newer) task Running "newer:jshint:sample" (newer) task No newer files to process.
Now, we can change one of the files in the
src
directory and run the command again to see that the changed file is processed again:Running "newer:jshint" (newer) task Running "newer:jshint:sample" (newer) task Running "jshint:sample" (jshint) task >> 1 file lint free. Running "newer-postrun:jshint:sample:1:.cache" (newer-postrun) task
Processing only changed files becomes especially useful when making use of the watch
task provided by the contrib-watch
plugin. A watch
task will rerun its indicated tasks every time it observes a file change, which can happen quite often during development, and can take quite a bit of time if the tasks target a large number of files.
The following steps provide an example of how to use the newer
plugin in conjunction with contrib-watch
and continues with the work we did in the main recipe:
We'll start by installing the package that contains the
contrib-watch
plugin and loading its tasks by following the instructions provided in the Installing a plugin recipe of this chapter.Now, we'll add a
watch
task, which will run thejshint
task when it observes changes on any of the JavaScript files contained in thesrc
directory. We'll also prepend thejshint
task withnewer:
to indicate that we only want to process the files that actually changed. This is done by adding the following to our configuration:watch: { jshint: { files: ['src/**/*.js'], tasks: ['newer:jshint'] } }
Now, we can start the
watch
task by using thegrunt watch
command in the terminal. This should produce output similar to the following, indicating that the watcher is running:Running "watch" task Waiting...
If we now change and save a file in the
src
directory, we should see output similar to the following, indicating that only the file that was changed has been processed by thejshint
task:>> File "src/file.js" changed. Running "newer:jshint" (newer) task Running "newer:jshint:sample" (newer) task Running "jshint:sample" (jshint) task >> 1 file lint free. Running "newer-postrun:jshint:sample:1:.cache" (newer-postrun) task