创建一个用于 grunt concat 的动态数组

Create a dynamic array for use in grunt concat

我需要根据我定义的变量连接一组文件 package.json。

// package.json
...

"layouts": [
    { 
      "page": "home",
      "version": "a"
    },
    { 
      "page": "about",
      "version": "a" 
    },
    { 
      "page": "contact",
      "version": "b" 
    }
  ]

...

然后在 grunt 中,我将这些构建到一个 JSON 数组中,并将其泵入我的 grunt-concat-contrib 任务中的 src 参数。

// gruntfile.js
...

var package = grunt.file.readJSON('package.json'),
    targets = package.layouts,
    paths = [];

    for (var target = 0; target < targets.length; target++) {
        paths.push("layouts/" + targets[target]['page'] + "/" + targets[target]['version'] + "/*.php");
    };

    var paths = JSON.stringify(paths);

    grunt.log.write(paths); // Writing this to console for debugging

    grunt.initConfig({
        concat: {
            build: {
                src: paths,
                dest: 'mysite/Code.php',
                options: {
                    separator: '?>\n\n'
                }
            }
        }
    });

...

我的问题是 paths 变量在分配给 JSON.stringify(paths) 时在 initConfig 内部不起作用。

如果我像下面这样手动输入我从记录路径变量的地方复制到控制台的数组,它就可以工作!

var paths = ["layouts/home/a/*.php","layouts/about/a/*.php","layouts/contact/b/*.php"];

我错过了什么?

德普。我修好了,我不需要 JSON.stringify() 数组。

最终工作的 gruntfile 如下:

// gruntfile.js
...

var package = grunt.file.readJSON('package.json'),
    targets = package.layouts,
    paths = [];

for (var target = 0; target < targets.length; target++) {
    paths.push("layouts/" + targets[target]['page'] + "/" + targets[target]['version'] + "/*.php");
};

grunt.initConfig({
    concat: {
        build: {
            src: paths,
            dest: 'mysite/Code.php',
            options: {
                separator: '?>\n\n'
            }
        }
    }
});

...