How to run Gulp tasks sequentially one after the other

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

    tried all proposed solutions, all seem to have issues of their own.

    If you actually look into the Orchestrator source, particularly the .start() implementation you will see that if the last parameter is a function it will treat it as a callback.

    I wrote this snippet for my own tasks:

      gulp.task( 'task1', () => console.log(a) )
      gulp.task( 'task2', () => console.log(a) )
      gulp.task( 'task3', () => console.log(a) )
      gulp.task( 'task4', () => console.log(a) )
      gulp.task( 'task5', () => console.log(a) )
    
      function runSequential( tasks ) {
        if( !tasks || tasks.length <= 0 ) return;
    
        const task = tasks[0];
        gulp.start( task, () => {
            console.log( `${task} finished` );
            runSequential( tasks.slice(1) );
        } );
      }
      gulp.task( "run-all", () => runSequential([ "task1", "task2", "task3", "task4", "task5" ));
    
    0 讨论(0)
  • 2020-11-22 13:25

    I was searching for this answer for a while. Now I got it in the official gulp documentation.

    If you want to perform a gulp task when the last one is complete, you have to return a stream:

    gulp.task('wiredep', ['dev-jade'], function () {
        var stream = gulp.src(paths.output + '*.html')
            .pipe($.wiredep())
            .pipe(gulp.dest(paths.output));
    
        return stream; // execute next task when this is completed
    });
    
    // First will execute and complete wiredep task
    gulp.task('prod-jade', ['wiredep'], function() {
        gulp.src(paths.output + '**/*.html')
            .pipe($.minifyHtml())
            .pipe(gulp.dest(paths.output));
    });

    0 讨论(0)
提交回复
热议问题