在这样的片段中:

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文档中找到:

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.task('coffee', ['clean'], function(){...});
gulp.task('develop', ['coffee'], function(){...});

现在的调度顺序是:清洁→咖啡→开发。注意,clean的实现和coffee的实现必须接受一个回调,“这样引擎就知道它什么时候会完成”:

gulp.task('clean', function(callback){
  del(['dist/*'], callback);
});

总之,下面是一个简单的gulp模式,用于同步清理,然后是异步构建依赖:

//build sub-tasks
gulp.task('bar', ['clean'], function(){...});
gulp.task('foo', ['clean'], function(){...});
gulp.task('baz', ['clean'], function(){...});
...

//main build task
gulp.task('build', ['foo', 'baz', 'bar', ...], function(){...})

Gulp非常聪明,无论有多少构建依赖于clean,它都可以在每个构建中精确地运行一次clean。如上所述,clean是一个同步障碍,然后构建的所有依赖项并行运行,然后构建运行。

这个问题的唯一好的解决方案可以在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 4.0让您可以轻松地使用Gulp .series完成同步任务。你可以简单地这样做:

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

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

尝试了所有提出的解决方案,似乎都有自己的问题。

如果您实际查看Orchestrator源代码,特别是.start()实现,您将看到如果最后一个参数是一个函数,它将把它视为一个回调。

我为自己的任务写了这个片段:

  gulp.task( 'task1', () => console.log(a) )
  gulp.task( 'task2', () => console.log(a) )
  gulp.task( 'task3', () => console.log(a) )
  gulp.task( 'task4', () => console.log(a) )
  gulp.task( 'task5', () => console.log(a) )

  function runSequential( tasks ) {
    if( !tasks || tasks.length <= 0 ) return;

    const task = tasks[0];
    gulp.start( task, () => {
        console.log( `${task} finished` );
        runSequential( tasks.slice(1) );
    } );
  }
  gulp.task( "run-all", () => runSequential([ "task1", "task2", "task3", "task4", "task5" ));

根据Gulp的文档:

您的任务是否在依赖项完成之前运行?确保你的依赖任务正确使用异步运行提示:接受回调或返回承诺或事件流。

同步运行你的任务序列:

返回事件流(例如gulp.src)给gulp。通知任务 流何时结束的任务。 在gulp.task的第二个参数中声明任务依赖性。

参见修订后的代码:

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

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

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