How to run Gulp tasks sequentially one after the other

前端 未结 14 754
眼角桃花
眼角桃花 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:13

    According to the Gulp docs:

    Are your tasks running before the dependencies are complete? Make sure your dependency tasks are correctly using the async run hints: take in a callback or return a promise or event stream.

    To run your sequence of tasks synchronously:

    1. Return the event stream (e.g. gulp.src) to gulp.task to inform the task of when the stream ends.
    2. Declare task dependencies in the second argument of gulp.task.

    See the revised code:

    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"
    
    0 讨论(0)
  • 2020-11-22 13:14

    Try this hack :-) Gulp v3.x Hack for Async bug

    I tried all of the "official" ways in the Readme, they didn't work for me but this did. You can also upgrade to gulp 4.x but I highly recommend you don't, it breaks so much stuff. You could use a real js promise, but hey, this is quick, dirty, simple :-) Essentially you use:

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

    Check out the post!

    0 讨论(0)
  • 2020-11-22 13:15

    run-sequence is the most clear way (at least until Gulp 4.0 is released)

    With run-sequence, your task will look like this:

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

    But if you (for some reason) prefer not using it, gulp.start method will help:

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

    Note: If you only start task without listening to result, develop task will finish earlier than coffee, and that may be confusing.

    You may also remove event listener when not needed

    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');
    });
    

    Consider there is also task_err event you may want to listen to. task_stop is triggered on successful finish, while task_err appears when there is some error.

    You may also wonder why there is no official documentation for gulp.start(). This answer from gulp member explains the things:

    gulp.start is undocumented on purpose because it can lead to complicated build files and we don't want people using it

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

    0 讨论(0)
  • 2020-11-22 13:16

    By default, gulp runs tasks simultaneously, unless they have explicit dependencies. This isn't very useful for tasks like clean, where you don't want to depend, but you need them to run before everything else.

    I wrote the run-sequence plugin specifically to fix this issue with gulp. After you install it, use it like this:

    var runSequence = require('run-sequence');
    
    gulp.task('develop', function(done) {
        runSequence('clean', 'coffee', function() {
            console.log('Run something else');
            done();
        });
    });
    

    You can read the full instructions on the package README — it also supports running some sets of tasks simultaneously.

    Please note, this will be (effectively) fixed in the next major release of gulp, as they are completely eliminating the automatic dependency ordering, and providing tools similar to run-sequence to allow you to manually specify run order how you want.

    However, that is a major breaking change, so there's no reason to wait when you can use run-sequence today.

    0 讨论(0)
  • 2020-11-22 13:16

    I was having this exact same problem and the solution turned out to be pretty easy for me. Basically change your code to the following and it should work. NOTE: the return before gulp.src made all the difference for me.

    gulp.task "coffee", ->
        return gulp.src("src/server/**/*.coffee")
            .pipe(coffee {bare: true}).on("error",gutil.log)
            .pipe(gulp.dest "bin")
    
    gulp.task "clean",->
        return gulp.src("bin", {read:false})
            .pipe clean
                force:true
    
    gulp.task 'develop',['clean','coffee'], ->
        console.log "run something else"
    
    0 讨论(0)
  • 2020-11-22 13:17

    I generated a node/gulp app using the generator-gulp-webapp Yeoman generator. It handled the "clean conundrum" this way (translating to the original tasks mentioned in the question):

    gulp.task('develop', ['clean'], function () {
      gulp.start('coffee');
    });
    
    0 讨论(0)
提交回复
热议问题