grunt-contrib-watch does not work with eslint to watch only changed file sets

574 views Asked by At

I can't seem to get ESLint to run only on changed files using grunt-contrib-watch.

I was able to do it with JSHint.

Any help to get ESLint doing the same would be greatly appreciated.

1

There are 1 answers

0
doldt On

I managed to set this up the other day, so I'll post a possible solution.

Regardless of whether you're trying to use grunt.config or <%= ... %> templating to dynamically modify the config object (to share data between the tasks), your problem might be that watch, by default, spawns child processes for the triggered tasks, making eslint run in a different context.

To get around this, just use the spawn:false options flag while configuring watch.

Basically, configure your tasks as such:

watch: {
  scripts: {
    files: ['**/*.js'],
    tasks: ['eslint'],
    options: {
      spawn: false, // !!!
    },
  },
},
eslint: {
  target: '<%= changedFiles %>'
}

Then attach an event handler to the watch event, setting changedFiles:

grunt.event.on('watch', function(action, filepath){
  grunt.config('changedFiles', filepath);
}

You can also modify eslint.target directly in the event handler, but having an attribute carry the changed files makes it available to any number of task that might use them.