以编程方式运行多个gulp4任务

时间:2017-06-15 11:16:56

标签: node.js gulp gulp-4

如何顺序运行多个节点流(由gulp生成)?

让我们假设一个包含两个依赖任务的简单gulpfile:

var gulp = require("gulp");

gulp.task("do1", function() {
  return gulp.src("./test/*")
             .pipe(gulp.dest("./dest/"));
});

gulp.task("do2", function() {
  return gulp.src("./dest/*")
             .pipe(gulp.dest("./final/"));
});

gulp.task("default", gulp.series("do1", "do2"));

到目前为止我想出了什么:

var gulp = require("gulp");

function do1() {
  return gulp.src("./test/*")
             .pipe(gulp.dest("./dest/"));
}

function do2() {
  return gulp.src("./dest/*")
             .pipe(gulp.dest("./final/"));
}

gulp.task("do1", function() {
  return do1();
});

gulp.task("do2", function() {
  return do2();
});

gulp.task("default", gulp.series("do1", "do2"));

// ...
// somewhere else in the same file, with some preconditions ...
do1().on("finish", do2);

确实这适用于这个简单的例子,但有更方便/更优雅的方式吗?特别是对于较长的依赖链。也许是使用gulp.series并执行整个系列的解决方案?

1 个答案:

答案 0 :(得分:0)

Gulp任务可以相互依赖(https://github.com/gulpjs/gulp/blob/master/docs/API.md#gulptaskname--deps--fn):

var gulp = require("gulp");

gulp.task("do1", function() {
  return gulp.src("./test/*")
             .pipe(gulp.dest("./dest/"));
});

gulp.task("do2", function() {
  return gulp.src("./dest/*")
             .pipe(gulp.dest("./final/"));
});

gulp.task("do3", ["do1", "do2"], function() {
  console.log("Tasks do1 and do2 are finished");
});

gulp.task("default", ["do3"], function() {
  console.log("All default tasks are finished");
});