在这样的片段中:

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.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/

其他回答

试试这个技巧:-) 吞咽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文档中找到:

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

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

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)