2014-01-16 139 views
4

当定义在Gruntjs多任务,我做grunt.registerTask当我能够做到咕噜this.files非多任务

grunt.registerMultiTask("taskName", "taskDescription", function() { 
    this.files.forEach(function (mapping) { 
    // mapping.src and mapping.dest are defined here, 
    // no matter which format was used to configure files in the task 
    }); 
}); 

为什么this.files不可用?或者,当它不是多任务(紧凑格式,对象格式,阵列格式,定义为here)时,我不允许在任务配置中使用不同的文件格式?

当不在多任务内时,访问src和目标文件映射的最简单方法是什么?我想要做的

grunt.initConfig({ 
    my_task: { 
    // I don't want to define a target here 
    files: { 
     // I want to be able to use any format here 
     "my/target/folder": "my/src/files/*" 
    } 
} 

grunt.registerTask("my_task", "description", function() { 
    this.files // ==> undefined 
}); 

回答

1

据咕噜API,你必须带参数运行的任务让你能够得到任何参数的taskFunction回调。

如果您运行grunt foo,您的将得到foo, no args,如果您运行grunt foo:testing:123将导致foo, testing 123

grunt.registerTask('foo', 'A sample task that logs stuff.', function(arg1, arg2) { 
    if (arguments.length === 0) { 
    grunt.log.writeln(this.name + ", no args"); 
    } else { 
    grunt.log.writeln(this.name + ", " + arg1 + " " + arg2); 
    } 
}); 

this对象grunt.registerTask的情景功能taskFunction回调会给你这个..

{ nameArgs: 'foo', 
    name: 'foo', 
    args: [], 
    flags: {}, 
    async: [Function], 
    errorCount: [Getter], 
    requires: [Function], 
    requiresConfig: [Function], 
    options: [Function] } 

,而这是grunt.registerMultiTask功能taskFunction回调会给你这样的背景下。

{ nameArgs: 'my_task:files', 
    name: 'my_task', 
    args: [], 
    flags: {}, 
    async: [Function], 
    errorCount: [Getter], 
    requires: [Function], 
    requiresConfig: [Function], 
    options: [Function], 
    data: { thefile: 'thesource' }, 
    files: [], 
    filesSrc: [Getter], 
    target: 'files' } 

要回答你的问题files obj在您的my_task任务中无法访问您需要的任务。