简体   繁体   English

如何将对象传递给gulp任务以能够同步运行该任务?

[英]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:devbuild: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:devbuild:prod它们具有不同的文件来最小化CSS。 If I run build:dev or build:prod doCssmin will be run asynchronously. 如果我运行build:devbuild: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:devbuild: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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM