2014-03-05 86 views
3

考慮到這個樣本的配置(注意:我已經省略了依賴,以保持它的簡稱):GruntJS鏈腕錶

module.exports = function(grunt) { 

    grunt.initConfig({ 
     pkg: grunt.file.readJSON('package.json'), 
     sass: { 
      dev: { 
       files: { 
        'style.css': 'custom.scss' 
       } 
      }, 
     }, 
     watch: { 
      options: { 
       livereload: true 
      }, 
      css: { 
       files: ['css/sass/**/*.scss'] , 
       tasks: ['sass:dev'] 
      }, 
      js:{ 
       files: ['js/**/*.js'] 
      }, 
      html:{ 
       files: ['templates/**/*.html'] 
      }, 
      dontRun:{ 
       files: ['/randomdir/*'], 
       tasks:['randomtask'] 
      } 
     } 
    }); 

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

我如何開始一個手錶手錶和執行css, js and html,但不dontRun

我已經試過:

grunt.registerTask('default', ['watch:css:js:html']); 
grunt.registerTask('default', ['watch:css','watch:js','watch:html']); 

但這些都只是執行的第一隻手錶,第二和第三就是不啓動。

回答

2

這是因爲Grunt只能運行一系列的任務。所以觀察任務只能運行一個目標或所有目標。

您可以使用動態別名任務來解決此限制,但:

grunt.registerTask('watchweb', function() { 
    // Remove the targets you dont want 
    grunt.config('watch.dontRun', null); 
    // Then run the watch task after this task is done 
    grunt.task.run('watch'); 
}); 

運行與grunt watchweb

或與任何配置工作的更強大和通用的解決方案是這樣的:

// Run with: grunt switchwatch:target1:target2 to only watch those targets 
grunt.registerTask('switchwatch', function() { 
    var targets = Array.prototype.slice.call(arguments, 0); 
    Object.keys(grunt.config('watch')).filter(function(target) { 
    return !(grunt.util._.indexOf(targets, target) !== -1); 
    }).forEach(function(target) { 
    grunt.log.writeln('Ignoring ' + target + '...'); 
    grunt.config(['watch', target], {files: []}); 
    }); 
    grunt.task.run('watch'); 
});