在这样的片段中:

gulp.task "coffee", ->
    gulp.src("src/server/**/*.coffee")
        .pipe(coffee {bare: true}).on("error",gutil.log)
        .pipe(gulp.dest "bin")

gulp.task "clean",->
    gulp.src("bin", {read:false})
        .pipe clean
            force:true

gulp.task 'develop',['clean','coffee'], ->
    console.log "run something else"

在开发任务中,我想要干净地运行,在它完成后,运行咖啡,当它完成时,运行其他东西。但是我想不出来。这个零件坏了。请建议。


当前回答

我一直在寻找这个答案。现在我在gulp的官方文档里找到了。

如果你想在最后一个任务完成时执行gulp任务,你必须返回一个流:

饮而尽。任务('wiredep', ['dev-jade'],函数(){ Var stream = gulp.src(路径。输出+ '*.html') .pipe ($ .wiredep ()) .pipe (gulp.dest (paths.output)); 返回流;//当此任务完成时执行下一个任务 }); //首先执行并完成wiredep任务 饮而尽。任务('prod-jade', ['wiredep'],函数(){ gulp.src(路径。输出+ '**/*.html') .pipe ($ .minifyHtml ()) .pipe (gulp.dest (paths.output)); });

其他回答

这个问题的唯一好的解决方案可以在gulp文档中找到:

var gulp = require('gulp');

// takes in a callback so the engine knows when it'll be done
gulp.task('one', function(cb) {
  // do stuff -- async or otherwise
  cb(err); // if err is not null and not undefined, the orchestration will stop, and 'two' will not run
});

// identifies a dependent task must be complete before this one begins
gulp.task('two', ['one'], function() {
  // task 'one' is done now
});

gulp.task('default', ['one', 'two']);
// alternatively: gulp.task('default', ['two']);

Gulp和Node使用承诺。

所以你可以这样做:

// ... require gulp, del, etc

function cleanTask() {
  return del('./dist/');
}

function bundleVendorsTask() {
  return gulp.src([...])
    .pipe(...)
    .pipe(gulp.dest('...'));
}

function bundleAppTask() {
  return gulp.src([...])
    .pipe(...)
    .pipe(gulp.dest('...'));
}

function tarTask() {
  return gulp.src([...])
    .pipe(...)
    .pipe(gulp.dest('...'));
}

gulp.task('deploy', function deployTask() {
  // 1. Run the clean task
  cleanTask().then(function () {
    // 2. Clean is complete. Now run two tasks in parallel
    Promise.all([
      bundleVendorsTask(),
      bundleAppTask()
    ]).then(function () {
      // 3. Two tasks are complete, now run the final task.
      tarTask();
    });
  });
});

如果返回gulp流,则可以使用then()方法添加回调。或者,您可以使用Node的本机Promise创建自己的Promise。在这里,我使用Promise.all()来获得一个回调,当所有promise都解决时触发。

它还没有正式发布,但是即将发布的Gulp 4.0让您可以轻松地使用Gulp .series完成同步任务。你可以简单地这样做:

gulp.task('develop', gulp.series('clean', 'coffee'))

我发现了一篇很好的博客文章,介绍了如何升级和使用这些简洁的功能: 通过示例迁移到gulp 4

默认情况下,gulp同时运行任务,除非它们有显式的依赖关系。这对于像clean这样的任务不是很有用,在这些任务中,您不希望依赖它们,但您需要在所有其他任务之前运行它们。

我专门用gulp编写了run-sequence插件来解决这个问题。安装后,像这样使用它:

var runSequence = require('run-sequence');

gulp.task('develop', function(done) {
    runSequence('clean', 'coffee', function() {
        console.log('Run something else');
        done();
    });
});

您可以在README包上阅读完整的说明-它还支持同时运行一些任务集。

请注意,这将在gulp的下一个主要版本中(有效地)修复,因为他们完全消除了自动依赖顺序,并提供类似于run-sequence的工具,允许您手动指定您想要的运行顺序。

然而,这是一个重大的突破性变化,所以当您现在可以使用run-sequence时,没有理由等待。

The very simple and efficient solution that I found out to perform tasks one after the other(when one task gets completed then second task will be initiated) (providing just an example) is : gulp.task('watch', () => gulp.watch(['src/**/*.css', 'src/**/*.pcss'], gulp.series('build',['copy'])) ); This means when you need to run the first-task before second-task, you need to write the second task(copy in this case) in square brackets. NOTE There should be round parenthesis externally for the tasks(until you want them to occur simultaneously)