[英]Better way to access a subtask in custom grunt task?
我是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;
}
});
};
謝謝!
在多任務中,“子任務”被稱為目標。 可通過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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.