我是Grunt的新手,而且我只构建了一些自定义的grunt任务。我将initConfig遍历到子任务的解决方案似乎非常不直观 - 我只是将一个正则表达式放在一起并使用开关来确定应该运行哪个子任务。有没有更好的方式来到这里?这是一个示例Gruntfile:
module.exports = function(grunt) {
grunt.initConfig({
myTask: {
add:{
//files object, options etc
},
remove:{
//files object, options etc
}
}
});
grunt.registerMultiTask('myTask', 'A sample task with a regex and switch for subtasks', function() {
var subTask = this.nameArgs.match(/.*?:(.*?):/)[1];
switch(subTask){
case "add":
//code for task "add"
break;
case "remove":
//code for task "remove"
break;
}
});
};
谢谢!
答案 0 :(得分:0)
在多任务中,"子任务"被称为目标。可以通过this.target
访问它。例如(取自http://gruntjs.com/api/grunt.task#grunt.task.registermultitask):
grunt.initConfig({
log: {
foo: [1, 2, 3],
bar: 'hello world',
baz: false
}
});
grunt.task.registerMultiTask('log', 'Log stuff.', function() {
grunt.log.writeln(this.target + ': ' + this.data);
});
正在运行grunt log:foo
会写出foo: 1,2,3
。
因此,在您的代码中,您可以将Regex行替换为:
var subTask = this.target;