grunt-contrib-copy排除文件列表



我使用grunt和grunt-contrib-copy;我的目标是:我想将文件夹中的所有文件复制到另一个文件夹,但可配置的文件列表

  copy: {
    files: [{
     expand:true, 
     cwd: './dist/Assets/',
     src: [
      '**/*',
      '!{Css,Images}/**/*',
      '!Js/{filetoexlude1.js,filetoexclude2.js}'  
      ], 
    filter: 'isFile',    
    dest:'./deploy/Assets/'
}]  
}

由于我不想在任务中编写文件列表,所以我写了一个名为files.json的json文件。

{  
"filestoexcludefromcopy":[
"filetoexclude1.js",
"filetoexclude2.js"
]
}

gruntfile.js内部:

filestoexcludefromcopy: grunt.file.readJSON('files.json').filestoexcludefromcopy,

修改了我的任务:

  copy: {
files: [{
  expand:true, 
  cwd: './dist/Assets/',
  src: [
    '**/*',
    '!{Css,Images}/**/*',
    '!Js/{<%= filestoexcludefromcopy%>}'  
    ], 
  filter: 'isFile',    
  dest:'./deploy/Assets/'
}]  
}

效果很好...除非在文件内部。json只有一个文件...有人可以帮助我正确配置任务吗?谢谢!

编辑:

Grunt似乎在filestoexcludefromcopy数组的length是一个。

时,对模板进行处理。

要避免使用此怪癖,您可以将空项目推到filestoexcludefromcopy数组时,当它的length仅为一个。

虽然这不是特别优雅 - 它成功地工作。

gruntfile.js

filestoexcludefromcopy: (function() {
    var files = grunt.file.readJSON('files.json').filestoexcludefromcopy;
    if (files.length === 1) {
        files.push(''); // Ensures files array length is greater than 1.
    }
    return files;
}()),
copy: {
    foo: { // Added a target
        files: [{
            expand: true,
            cwd: './dist/Assets/',
            src: [
                '**/*',
                '!{Css,Images}/**/*',
                '!Js/{<%= filestoexcludefromcopy%>}'
            ],
            filter: 'isFile',
            dest: './deploy/Assets/'
        }]
    }
}

原始答案

从模板上卸下卷曲括号{}。IE。更改此:

'!Js/{<%= filestoexcludefromcopy%>}'

'!Js/<%= filestoexcludefromcopy%>'

注意:在copy任务中,目标也添加了目标。

gruntfile.js

//...
filestoexcludefromcopy: grunt.file.readJSON('files.json').filestoexcludefromcopy,
copy: {
    foo: { // Add a Target to the 'copy' Task
        files: [{
            expand: true,
            cwd: './dist/Assets/',
            src: [
                '**/*',
                '!{Css,Images}/**/*',
                '!Js/<%= filestoexcludefromcopy%>' // Remove the curly braces.
            ],
            filter: 'isFile',
            dest: './deploy/Assets/'
        }]
    }
}
//...

最新更新