gruntjs:对所有目标使用一次多任务配置中具有动态映射的文件

gruntjs:对所有目标使用一次多任务配置中具有动态映射的文件,gruntjs,Gruntjs,在我的多任务中,我想对文件配置属性使用动态映射 files: [ { expand: true, // Enable dynamic expansion. cwd: 'lib/', // Src matches are relative to this path. src: ['**/*.js'], // Actual pattern(s) to match. dest: 'build/', // Destinatio

在我的多任务中,我想对文件配置属性使用动态映射

files: [
    {
      expand: true,     // Enable dynamic expansion.
      cwd: 'lib/',      // Src matches are relative to this path.
      src: ['**/*.js'], // Actual pattern(s) to match.
      dest: 'build/',   // Destination path prefix.
    },
  ] 
是否可以为所有目标指定一次“文件”属性(它们将被扩展)以避免冗余? 所有目标都使用相同的文件结构和相同的文件

比如:

taskName: {
  target1: { prop1:1 },
  target2: { prop1:2 },
  files: [
    {
      expand: true,     // Enable dynamic expansion.
      cwd: 'li...
      ...
    }
  ]
} 
我可以在“选项”属性中写入文件,但我需要手动调用该文件上的展开函数

多谢各位

[编辑]

对于测试:

grunt.registerMultiTask('taskname', 'im looking for files', function () {
  grunt.log.writeflags(this.files, 'this.files');
  console.log('this.files'.yellow, this.files); //double check ;)
});

只需将
文件
属性存储在变量中,并在需要时使用它:

var myFiles = [
    {
      expand: true,     // Enable dynamic expansion.
      cwd: 'lib/',      // Src matches are relative to this path.
      src: ['**/*.js'], // Actual pattern(s) to match.
      dest: 'build/',   // Destination path prefix.
    }
  ]; 


taskName: {
  target1: { 
     prop1:1,
     files: myFiles
  },
  target2: { 
     prop1:2,
     files: myFiles
  }
} 
如果这仍然太多,您可以通过更改配置完全通过javascript完成:

var tasknameConfig = grunt.config('taskname');
var target;

for (target in tasknameConfig) {
  tasknameConfig[target].files = myFiles;
}
grunt.config('taskname', tasknameConfig);

我发现解决方案是使用grunt.file.expandMapping方法以编程方式生成文件数组

grunt.config

taskname.js


好的,tasknameConfig[target].files=myFiles;但是您的建议都不起作用,当前任务的files属性仍然为空。如果grunt能够处理选项,那就太好了。files扩展
'taskname': {
    target1: { prop1:1 },
    target2: { prop1:2 },

    options: {
      defProperty: "defValue",
      dFiles: { //default files object
        cwd: 'lib/',      // Src matches are relative to this path.
        src: ['**/*.js'], // Actual pattern(s) to match.
        dest: 'build/'   // Destination path prefix.
        //any other property if you need (e.g. flatten, ext)
      }
}
grunt.registerMultiTask('taskname', 'im looking for files', function () {

    var curTask = this,
        opts = curTask.options();

    if (!curTask.files.length && 'dFiles' in opts) {
      var df = opts.dFiles;

      curTask.files = grunt.file.expandMapping(df.src, df.dest , df);
    }

    console.log('this.files: '.yellow, this.files);

});