如何减少gruntfile中的重复

时间:2015-12-01 09:12:14

标签: gruntjs

我的grunt文件如下所示:

grunt.initConfig({

    compress: {

        foo: {
            options: { archive: 'deploy/foo.zip', mode: 'zip' },
            files: [{ expand: true, cwd: 'release/foo/', src: ['**'], dot: true }]
        },
        bar: {
            options: { archive: 'deploy/bar.zip', mode: 'zip' },
            files: [{ expand: true, cwd: 'release/bar/', src: ['**'], dot: true }]
        },
        baz: {
            options: { archive: 'deploy/baz.zip', mode: 'zip' },
            files: [{ expand: true, cwd: 'release/baz/', src: ['**'], dot: true }]
        },...

我如何参数化,所以我不需要N个定义,我可以调用(伪代码)

> grunt compress "foo"

1 个答案:

答案 0 :(得分:1)

如果我理解你的问题:

var initData = {
    compress: { }
};

var compress = function(n){
    initData.compress[n] = {
        options: {archive: 'deploy/' + n + '.zip', mode: 'zip'},
        files: [{expand: true, cwd: 'release/' + n + '/', src: ['**'], dot: true}]
    };
}

compress('foo');
compress('bar');
compress('baz');

grunt.initConfig(initData);