在这样的片段中:

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 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的文档:

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

同步运行你的任务序列:

返回事件流(例如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"

等着看任务是否完成,然后剩下的,我是这样做的:

gulp.task('default',
  gulp.series('set_env', gulp.parallel('build_scss', 'minify_js', 'minify_ts', 'minify_html', 'browser_sync_func', 'watch'),
    function () {
    }));

荣誉:https://fettblog.eu/gulp-4-parallel-and-series/

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

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和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都解决时触发。