Gruntjs 仅针对grunt自定义任务中的最后一个值执行任务
在执行grunt自定义任务时,我正在迭代一个循环,在循环内部,我正在调用一个grunt任务,同时调用使用当前循环值对任务进行设置的值,但当循环执行头韵时,它总是设置数组的最后一个值Gruntjs 仅针对grunt自定义任务中的最后一个值执行任务,gruntjs,Gruntjs,在执行grunt自定义任务时,我正在迭代一个循环,在循环内部,我正在调用一个grunt任务,同时调用使用当前循环值对任务进行设置的值,但当循环执行头韵时,它总是设置数组的最后一个值 var productionUrl = "http://www.abc.de"; var phantomPagesPath = { "index": "/index.php", "search": "/Suche?searchString=test", "warenkorb": "/waren
var productionUrl = "http://www.abc.de";
var phantomPagesPath = {
"index": "/index.php",
"search": "/Suche?searchString=test",
"warenkorb": "/warenkorb",
"product": "/4-ecksofa",
"cms": "/content/25-service",
"category": "/bestellung",
"pageNotFound": "/404"
};
grunt.initConfig({`phantomas: {
prod: {
options: {
options: {
'timeout': 30
},
buildUi: true
}
}
}`});`
grunt.registerTask('fantomas', 'Custome Phantomas task', function () {
var done = this.async();
var pageUrl = '';
var location = '';
for (var page in phantomPagesPath) {
pageUrl = '';
location = '';
if (phantomPagesPath.hasOwnProperty(page)) {
pageUrl = productionUrl + phantomPagesPath[page];
location = './public/phantomas/' + page + "/";
console.log("process started for: " + pageUrl);
console.log("location: " + location);
grunt.config.set('phantomas.options.url', pageUrl);
grunt.config.set('phantomas.options.indexPath', location);
grunt.task.run('phantomas');
}
}
done();
});
现在我得到了输出
已启动以下进程:
位置:/公共/幻影/索引/
已启动以下进程:
位置:./公共/幻影/搜索/
已启动以下进程:
地点:./public/phantomas/warenkorb/
已启动以下进程:
位置:/公共/幻影/产品/
已启动以下进程:
位置:./public/phantomas/cms/
已启动以下进程:
位置:/公共/幻影/类别/
已启动以下进程:
位置:./public/phantomas/pageNotFound/
运行“phantomas:prod”(phantomas)任务
幻象执行已开始
正在为所有数量的条目执行任务,但为任务发送的数据是循环“pageNotFound”中的最后一个条目
我的意思是,这个进程工作了7次,但在每个进程上,它都取循环的最后一个值。在搜索了很多之后,我找到了解决方案,尽管调用
grunt.task.run('phantomas');
在foreach循环中,应该在循环外调用它,这意味着任务只执行一次。因此,为此,我们需要在我编写以下代码的任务中增加make tasks/target:
grunt.loadNpmTasks('grunt-phantomas');
var phantomPagesPath = {
"index": "/index.php",
"search": "/Suche?searchString=test",
"warenkorb": "/warenkorb",
"product": "/4-ecksofa",
"cms": "/content/25-service",
"category": "/bestellung",
"pageNotFound": "/404"
};
grunt.initConfig({
phantomas: {
//For Executing this task run: grunt fantomas
},
})代码>
快跑
grunt fantomas
grunt fantomas