gruntjs:在多任务配置中针对所有目标使用动态映射的文件 [英] gruntjs: use files with dynamic mapping in multitask config once for all targets

查看:101
本文介绍了gruntjs:在多任务配置中针对所有目标使用动态映射的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的多任务中,我想为文件配置属性

使用动态映射

  files: [
{
展开:true,//启用动态展开。
cwd:'lib /',// Src匹配相对于此路径。
src:['** / *。js'],//实际匹配的模式。
dest:'build /',//目标路径前缀。
},
]

是否可以指定files属性一次为所有目标(他们将扩大),以避免冗余?
所有目标都使用相同的文件结构,使用相同的文件



类似于:

 任务名称:{
目标1:{prop1:1},
目标2:{prop1:2},
文件:[
{
展开:true,//启用动态展开。
cwd:'li ...
...
}
]
}

我可以在'options'属性中编写文件,但是我需要手动调用这些文件的扩展函数。

谢谢

用于测试:

$ pre $ g $ c $ g $ g $ g $ g $ g $ g $ g $ g $ g $% grunt.log.writeflags(this.files,'this.files');
console.log('this.files'.yellow,this.files); //重复检查;)
}) ;


解决方案

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

grunt.config >

 'taskname':{
target1:{prop1:1},
target2:{prop1 :2},

选项:{
defProperty:defValue,
dFiles:{//默认文件对象
cwd:'lib /',// Src匹配是相对于这条路径。
src:['** / *。js'],//实际匹配的模式。
dest:'build /'//目标路径前缀。
//任何其他财产,如果你需要(例如平摊,分机)
}
}

taskname.js

  grunt.registerMultiTask('taskname','即时查找文件',函数(){

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

if(!curTask.files。长度&'dFiles'在opts中){
var df = opts.dFiles;

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

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

});


in my multi task I want to use dynamic mappings for files config property

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.
    },
  ] 

is it possible to specify the "files" property once for all targets (and they would be expanded) to avoid redundancy? all targets are working with the same file structure, with the same files

something like:

taskName: {
  target1: { prop1:1 },
  target2: { prop1:2 },
  files: [
    {
      expand: true,     // Enable dynamic expansion.
      cwd: 'li...
      ...
    }
  ]
} 

I can write files inside 'options' property, but then I need to call expand functions on that files manually.

Thank you

[EDIT]

for testing:

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

解决方案

the solution, I've found, is to use grunt.file.expandMapping method to programmatically generate a files array

grunt.config

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

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

});

这篇关于gruntjs:在多任务配置中针对所有目标使用动态映射的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆