【问题标题】:Gruntjs looping a grunt 'copy file' taskGruntjs 循环执行 grunt 的“复制文件”任务
【发布时间】:2020-09-17 14:13:45
【问题描述】:

我正在尝试将 index.html 文件复制到多个文件夹。我一直在寻找堆栈溢出,几乎找到了我正在寻找的解决方案。我的示例中的循环仅将 index.html 文件复制到文件夹数组的最后一个文件夹。我想知道我忽略了什么。有什么想法吗?

module.exports = function(grunt){

grunt.initConfig({
    copy:{
        files:{
            flatten:true,
            expand: false,
            src: [
                'scaffold/index.html',
            ],
            dest: "dist/<%= grunt.option('folder') %>/",
            filter: 'isFile',
            force: true
        }
    }
})

function copytoFolders() {

    var folders = ["300x600", "300x250", "336x280"], folder;

    for (folder in folders) 
    {
        grunt.option('folder', folders[folder]);
        grunt.task.run('copy');
    }
}

grunt.loadNpmTasks('grunt-contrib-copy-force');
grunt.registerTask('copyFol', copytoFolders)

}

【问题讨论】:

    标签: node.js loops gruntjs


    【解决方案1】:

    好的,我通过查看这篇文章解决了这个问题: stack overflow post

    所以基本上它现在返回一个文件对象数组,当调用默认任务时,它会执行对象文件。

    这是适合我的脚本:

    module.exports = function(grunt) {
    
    function getFiles() {
        var folders = ["300x600", "300x250", "336x280"];
        var files = [];
        for (var folder in folders) {
            files.push({
                flatten:true,
                expand: true,
                src: [
                    'scaffold/index.html',
                ],
                dest: "dist/" + folders[folder] + "/",
                filter: 'isFile'
            });
        }
        return files;
    }
    
    grunt.initConfig({
        copy: {
            core: {
                files: getFiles()
            }
        }
    });
    
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.registerTask('default', ['copy:core']);
    

    };

    【讨论】:

      猜你喜欢
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多