【问题标题】:grunt-contrib-copy exclude a list of filesgrunt-contrib-copy 排除文件列表
【发布时间】:2017-03-12 21:02:24
【问题描述】:

我使用 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/'
}]  
}

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

【问题讨论】:

    标签: javascript json gruntjs copy


    【解决方案1】:

    编辑:

    filestoexcludefromcopy 数组的 length 为 1 时,Grunt 似乎以不同的方式处理 template

    为了避免这种怪癖,您可以在 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%>'
    

    注意:在下面的要点中,Target 也被添加到 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/'
            }]
        }
    }
    //...
    

    【讨论】:

    • 感谢您的回答,但删除花括号只会复制所有文件; json 中列出的文件被完全忽略
    • 有趣,它在 Mac OS 上成功运行...您使用的是哪个平台/操作系统?
    • 我在 Windows 和 MacOSX 上都试过了,但都没有成功。它将以这种方式工作:src: [ '**/*', '!{Css,Images}/**/*', '&lt;%= files.filestoexcludefromcopy%&gt;' ], 并以这种方式编写 json 文件:"filestoexcludefromcopy":[ "!Js/file1.js", "!Js/file2.js" ] 但我希望语法更简洁
    • 更新的答案...是的,当filestoexcludefromcopy 数组中的length 只有一个时,grunt 处理模板的方式似乎不同——这很奇怪!虽然这个修改后的解决方案也不是特别优雅,但它可以让您保留原来的 .json 文件语法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2018-02-13
    • 2015-07-05
    • 1970-01-01
    相关资源
    最近更新 更多