Making gulp write files synchronously before moving on to the next task

后端 未结 4 1344
没有蜡笔的小新
没有蜡笔的小新 2021-01-01 17:03

gulpfile.js

gulp.task(\'browser-bundle\', [\'react\'], function() {
...

});


gulp.task(\'react\', function(){
  gulp.src(options.JSX_SOURCE)
      .pipe(re         


        
4条回答
  •  無奈伤痛
    2021-01-01 17:16

    back to 2019: if some one come here with similar problem

    In gulp 4.*, at least, gulp wait for promise to resolve but ignore the result. so... if you use async await pattern and return the result of gulp.src('...') you got a surprise. the task not wait for stream finish before it continue! somthing that can result to serious bug and waist of time. the solution is "promisify" gulp.src

    example:

    
    gulp.task( async function notWaitingTask(){
       // the return stream are ignored because function return promise not stream
       return gulp.src('file.js')
         .pipe(gulp.dest('new-location'))
    
    })
    
    gulp.task( async function waitingTask(){
       // the return stream are respect
    
       await promisifyStream(
         gulp.src('file.js')
          .pipe(gulp.dest('new-location'))
       )
    
    })
    
    function promisifyStream(stream) {
        return new Promise( res => stream.on('end',res));
    }
    
    
    

提交回复
热议问题