Javascript 循环遍历数组并生成Gulp任务
我试图生成我的gulp任务,而不是静态地编写它们。我想循环一个数组,并根据数组中的项目生成gulp任务 诸如此类的事情,任何帮助都将不胜感激Javascript 循环遍历数组并生成Gulp任务,javascript,arrays,node.js,gulp,Javascript,Arrays,Node.js,Gulp,我试图生成我的gulp任务,而不是静态地编写它们。我想循环一个数组,并根据数组中的项目生成gulp任务 诸如此类的事情,任何帮助都将不胜感激 var brands = ['google','facebook','twitter'] brands.forEach(function (brand) { gulp.task(brand, function() { return gulp.src('./'+ brand + '.scss') .pipe(rename('v
var brands = ['google','facebook','twitter']
brands.forEach(function (brand) {
gulp.task(brand, function() {
return gulp.src('./'+ brand + '.scss')
.pipe(rename('var.scss'))
.pipe(gulp.dest('./styles/tmp'));
})
})
这是一个结束问题。问题是,在您的情况下,传递给
gulp.task()
的函数中的变量引用“brand”应该与中的变量不同
gulp.task(OtherVariableNameThanBrand, [brand, 'scripts']);
解决办法是:
var brands = ['google','facebook','twitter'];
var style = 'style-';
var styleBrands =[];
brands.forEach(function (brand) {
gulp.task(brand, function() {
return gulp.src('./'+ brand + '.scss')
.pipe(rename('var.scss'))
.pipe(gulp.dest('./styles/tmp'));
})
});
brands.forEach(function(brand) {
gulp.task(style + brand, [brand, 'scripts']);
styleBrands.push(style + brand);
});
gulp.task('default', styleBrands);
有关更多详细信息,请阅读这在我看来是合理的。问题是什么?很抱歉,我以前尝试过一个for循环,但它不起作用。这似乎确实有效。谢谢:)