grunt-contrib-watch: Cant not defined task which include a subset of multiple watch tasks

The problem is quite simple, I defined my watch tasks like:

watch: {
   "a": { files: "...", tasks: [ .. ] },
   "b": { files: "...", tasks: [ .. ] },
   "c": { files: "...", tasks: [ .. ] }
}

and after that I want to define a new task which only contain a subset of all the watch tasks:

grunt.registerTask( 'myWatch', [ 'watch:a', 'watch:c' ] );

But it seems like only the first watch subtask works.

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 24 (9 by maintainers)

Most upvoted comments

A solution I’ve used is to define multiple watch targets and rename the watch task like so:

watch: {
    scripts: {
        files: ['js/**/*.js'],
        tasks: ['concat', 'uglify'],
        options: {
            spawn: false
        }
    }
},

// Don't uglify in dev task
watchdev: {
    scripts: {
        files: ['js/**/*.js'],
        tasks: ['concat'],
        options: {
            spawn: false
        }
    }
}

grunt.loadNpmTasks('grunt-contrib-watch');
// Rename watch to watchdev and load it again
grunt.renameTask('watch', 'watchdev');
grunt.loadNpmTasks('grunt-contrib-watch');

grunt.registerTask('default', ['watch']);
grunt.registerTask('dev', ['watchdev']);

You can dynamically write the task config in a custom task:

  grunt.registerTask('watch:test', function() {
    // Configuration for watch:test tasks.
    var config = {
      options: {
        interrupt: true
      },
      unit: {
        files: [
          'test/unit/**/*.spec.coffee'
        ],
        tasks: ['karma:unit']
      },
      integration: {
        files: [
          'test/integration/**/*.rb',
          '.tmp/scripts/**/*.js'
        ],
        tasks: ['exec:rspec']
      }
    };

    grunt.config('watch', config);
    grunt.task.run('watch');
  });

Success!