Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 274
Sev Avatar asked Oct 31 '22 01:10

Sev


1 Answers

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.

like image 66
doldt Avatar answered Jan 04 '23 13:01

doldt