[英]How to pass object to gulp task to be able to run that task synchronously?
I have two gulp tasks 我有两个任务
gulp.task('build:dev', ['task1', task2', task3'], () => {
doCssmin({'destination': ['file1', 'file2']});
});
gulp.task('build:prod', ['task1', task2', task3'], () => {
doCssmin({'destination': ['file3', 'file4']});
});
Now I have cssMin
task which is shared between build:dev
and build:prod
so I created that as a function to share with both of them. 现在,我有了
cssMin
任务,该任务在build:dev
和build:prod
之间共享,因此我将其创建为与两者共享的函数。
function doCssmin(files) {
_.each(files, function(val, key) {
gulp.src(val)
.pipe(minifyCss({compatibility: 'ie8'}))
.pipe(rename(basename(key)))
.pipe(gulp.dest(dirname(key)));
});
}
and doCssmin
accept files
parameter as build:dev
and build:prod
they have different files to minify css. 和
doCssmin
接受files
参数为build:dev
和build:prod
它们具有不同的文件来最小化CSS。 If I run build:dev
or build:prod
doCssmin
will be run asynchronously. 如果我运行
build:dev
或build:prod
doCssmin
将异步运行。 But doCssmin
depends on task2
. 但是
doCssmin
取决于task2
。
My question is how do I extract doCssmin
to a gulp task and accept files
parameter so it can be shared with build:dev
and build:prod
tasks? 我的问题是如何将
doCssmin
提取到doCssmin
任务并接受files
参数,以便可以与build:dev
和build:prod
任务共享?
Not sure if it's too confusing? 不知道它是否太混乱了?
Yesterday, I answered a question on making your Gulp tasks more DRY . 昨天,我回答了一个使您的Gulp任务更加干燥的问题 。 I believe a good chunk of that answer applies here as well.
我相信该答案的很大一部分也适用于此。
To support loops with that, you want to merge the streams that you receive from gulp.src
. 为了支持循环,您希望合并从
gulp.src
接收的流。 Using the example here : 在这里使用示例 :
gulp.task('test', function() {
var bootstrap = gulp.src('bootstrap/js/*.js')
.pipe(gulp.dest('public/bootstrap'));
var jquery = gulp.src('jquery.cookie/jquery.cookie.js')
.pipe(gulp.dest('public/jquery'));
return merge(bootstrap, jquery);
});
For an array, you can use Array.prototype.map
to convert the items into streams, then merge all of those: 对于数组,可以使用
Array.prototype.map
将项目转换为流,然后合并所有这些项目:
function minifyCss(paths) {
return merge.apply(this, paths.map(function (path) {
gulp.src(path)
.pipe(minifyCss({compatibility: 'ie8'}))
.pipe(rename(basename(path)))
.pipe(gulp.dest(dirname(path)));
}));
}
gulp.task('test', function () {
return minifyCss(['foo', 'bar']);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.