在这样的片段中:

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"

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


当前回答

试试这个技巧:-) 吞咽v3。x针对异步错误的Hack

我在Readme中尝试了所有的“官方”方法,它们都不适合我,但是这个方法管用。你也可以升级到gulp 4。x,但是我强烈建议你不要这样做,这样会弄坏很多东西。你可以使用一个真正的js承诺,但嘿,这是快速,肮脏,简单:-) 基本上你可以使用:

var wait = 0; // flag to signal thread that task is done
if(wait == 0) setTimeout(... // sleep and let nodejs schedule other threads

看看这个帖子!

其他回答

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

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

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

我一直在寻找这个答案。现在我在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 4.0发布之前)

使用run-sequence,你的任务看起来像这样:

var sequence = require('run-sequence');
/* ... */
gulp.task('develop', function (done) {
    sequence('clean', 'coffee', done);
});

但如果你(出于某种原因)不喜欢使用它,那就咽下去吧。Start方法将帮助:

gulp.task('develop', ['clean'], function (done) {
    gulp.on('task_stop', function (event) {
        if (event.task === 'coffee') {
            done();
        }
    });
    gulp.start('coffee');
});

注意:如果你只开始任务而不听结果,开发任务会比喝咖啡更早完成,这可能会让人困惑。

您也可以在不需要时删除事件侦听器

gulp.task('develop', ['clean'], function (done) {
    function onFinish(event) {
        if (event.task === 'coffee') {
            gulp.removeListener('task_stop', onFinish);
            done();
        }
    }
    gulp.on('task_stop', onFinish);
    gulp.start('coffee');
});

考虑还有一个task_err事件,您可能想要监听。 Task_stop在成功完成时被触发,而task_err则在出现一些错误时出现。

您可能还想知道为什么没有gulp.start()的官方文档。来自gulp成员的回答解释了这些事情:

饮而尽。Start是故意没有记录的,因为它会导致复杂的构建文件,我们不希望人们使用它

(来源:https://github.com/gulpjs/gulp/issues/426 # issuecomment - 41208007)

我使用生成器-gulp-webapp Yeoman生成器生成了一个node/gulp应用程序。它是这样处理“干净的难题”的(翻译成问题中提到的原始任务):

gulp.task('develop', ['clean'], function () {
  gulp.start('coffee');
});

这个问题的唯一好的解决方案可以在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']);