gruntjsgrunt-contrib-watchgrunt-concurrent

Grunt - lint only modified files using grunt-newer


I'm running a Grunt task that uses Concurrent to run both Nodemon and Watch/Livereload. On default load, I lint and launch Concurrent. I would also like to set up a Watch to lint individual files on change. Currently, all files are linted when any one file is changed.

I have examined a similar question on StackOverflow and decided to go with grunt-newer as a potential solution. In my implementation below, however, the 'newer' prefix doesn't seem to do anything. How can I fix this so that only changed files are linted?

module.exports = function(grunt) {
  //load all dependencies
  require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    concurrent: {
      dev: {
        options: {
          logConcurrentOutput: true
        },
        tasks: ['watch', 'nodemon']
      }
    },
    jshint: {
      files: ['Gruntfile.js', 'client/src/*.js', 'server/**/*.js'],
      options: {
        '-W030': true,
        '-W083': true,
        globals: {
          console: true,
          module: true,
          document: true
        }
      }
    },
    watch: {
      all: {
        files: ['<%= jshint.files %>'],
        tasks: ['newer:jshint']
      },
      frontend: {
        files: ['client/**/*.{css,js,html}'],
        options: {
          livereload: true
        }
      }
    },
    nodemon: {
      dev: {
        options: {
          file: 'server/server.js',
          watchedFolders: ['server']
        }
      }
    }
  });

  grunt.registerTask('test', ['jshint']);
  grunt.registerTask('default', ['jshint', 'concurrent']);

};

Solution

  • I was having the same problem and finally figured it out. The solution is hidden deep in the documentation and very misleading with the spawn option in the code sample: https://github.com/gruntjs/grunt-contrib-watch#compiling-files-as-needed

    Your config file should remain the same as you have it in your question, but you need to add a listener to the watch event. I recommend the 'robust' option they provide (modified for you specific task config). Place this code just above the call to grunt.initConfig and after you require calls.

    var changedFiles = Object.create(null);
    var onChange = grunt.util._.debounce(function() {
      // Modified to point to jshint.files as per the task example in the question.
      grunt.config('jshint.files', Object.keys(changedFiles));
      changedFiles = Object.create(null);
    }, 200);
    
    grunt.event.on('watch', function(action, filepath) {
      changedFiles[filepath] = action;
      onChange();
    });
    

    Add the nospawn option to the all watch task. This is what is misleading in in the documentation. It mentions it should be disabled if you want dynamically modify your config but basically prevents it from working with newer unless it's set to true:

     watch: {
       all: {
         files: ['<%= jshint.files %>'],
         tasks: ['newer:jshint'],
         options: {
           nospawn: true,
         }
       },
       ...
    

    NOTE: If you modify your grunt file while it's running then it will lint all files, not sure why it does this but then it gets stuck and will just keep linting everything for all changes you make. I just took out the 'gruntfile.js' from the list of files that should be linted to avoid it.