3

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

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 ;)
});
4

2 回答 2

1

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

咕噜声配置

'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)
      }
}

任务名称.js

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);

});
于 2013-04-11T00:34:06.840 回答
0

只需将files属性存储在变量中并在需要的地方使用它:

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);
于 2013-04-10T15:05:22.130 回答