How to run Gulp tasks sequentially one after the other

前端 未结 14 984
眼角桃花
眼角桃花 2020-11-22 13:00

in the snippet like this:

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


        
14条回答
  •  借酒劲吻你
    2020-11-22 13:20

    Simply make coffee depend on clean, and develop depend on coffee:

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

    Dispatch is now serial: cleancoffeedevelop. Note that clean's implementation and coffee's implementation must accept a callback, "so the engine knows when it'll be done":

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

    In conclusion, below is a simple gulp pattern for a synchronous clean followed by asynchronous build dependencies:

    //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 is smart enough to run clean exactly once per build, no matter how many of build's dependencies depend on clean. As written above, clean is a synchronization barrier, then all of build's dependencies run in parallel, then build runs.

提交回复
热议问题