在这样的片段中:

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"

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


当前回答

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)

其他回答

这个问题的唯一好的解决方案可以在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同时运行任务,除非它们有显式的依赖关系。这对于像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时,没有理由等待。

试试这个技巧:-) 吞咽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

看看这个帖子!

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)

运行序列是最明确的方法(至少在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)