簡體   English   中英

任務目標目標陣陣?

[英]Grunt task target array?

我可以將多個文件數組設置為一個目標:

task:{
    target:{
        files:[
            {
                expand:true,
                cwd:'client/',
                dest:'server/',
                src:[
                    'scripts/**/*.js',
                    'styles/**/*.css',
                    'images/**'
                ]
            },
            {
                expand:true,
                cwd:'client/assets/',
                src:'**/*',
                dest:'server/'
            }
        ]
    }
}

現在我要對目標執行相同的操作。

像這樣:

task:{
    server:[
        {
            options:{
                …
            },
            files:{
                …
            }
        },
        {
            options:{
                …
            },
            files:{
                …
            }
        }
    ]
}

但這不適用於Grunt:

Warning: Object #<Object> has no method 'indexOf' Use --force to continue.

我該怎么做?

現在,我使用此方案執行相同的操作:

task:{
    server_<subtask_one>:{
        options:{
            …
        },
        files:{
            …
        }
    },
    server_<subtask_second>:{
        options:{
            …
        },
        files:{
            …
        }
    }
}

但是將任務前綴重復給每個子任務然后將它們啟動到這樣的單獨行中並不方便:

'dataSeparator:<target>_<subtask_one>',
'dataSeparator:<target>_<subtask_second>',

除非您要編寫自定義任務,否則這是您唯一的選擇。 但是大多數任務的確允許您在任務級別指定一個options塊,因此您至少可以節省一些重復:

task:{
    options:{
        // options common to all tasks
    },
    server_<subtask_one>:{
        options:{
            // override options if necessary
        },
        files:{
            // custom for this target
        }
    },
    server_<subtask_second>:{
        options:{
            // override options if necessary
        },
        files:{
            // custom for this target
        }
    }
}

就像我說的那樣,您也許可以編寫一個自定義任務來動態重置每個目標的grunt config選項 ,但這很麻煩,我不建議這樣做……甚至不確定它是否可以正常工作。

grunt.registerTask('mutli-task', 'Compile options and pass to task', function() {

    grunt.config.set('task.server_<subtask_one>.some_setting', 'value');
    // ...
    grunt.task.run('task');

    // Now do it again, but with different settings... maybe in a loop?
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM