考虑这个示例配置(请注意,为了简短起见,我省略了依赖项):
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 和 html
而不是 dontRun
的监视?
我试过:
grunt.registerTask('default', ['watch:css:js:html']);
grunt.registerTask('default', ['watch:css','watch:js','watch:html']);
但是这两个都只是执行第一个watch,第二个和第三个都没有开始。
最佳答案
这是因为 Grunt 只能运行一系列任务。因此 watch 任务只能与一个目标或所有目标一起运行。
不过,您可以使用动态别名任务来解决此限制:
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');
});
关于javascript - GruntJS 链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22186476/