How to run Gulp tasks sequentially one after the other

前端 未结 14 941
眼角桃花
眼角桃花 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 13:20

    Gulp and Node use promises.

    So you can do this:

    // ... 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();
        });
      });
    });
    

    If you return the gulp stream, you can use the then() method to add a callback. Alternately, you can use Node's native Promise to create your own promises. Here I use Promise.all() to have one callback that fires when all the promises resolve.

提交回复
热议问题