Gulp.js task, return on src?

后端 未结 3 1332
别跟我提以往
别跟我提以往 2020-11-28 02:10

I\'m new to gulp and have been looking through example set-ups. Some people have the following structure:

gulp.task(\"XXXX\", function() {
    gulp.src(\"...         


        
3条回答
  •  长情又很酷
    2020-11-28 02:45

    If you have dependent tasks you need to return the stream for the tasks to wait on their dependent tasks to complete before running themselves.

    eg

    // without return
    gulp.task('task1', function() {
        gulp.src('src/coffee/*.coffee')
          /* eg compile coffeescript here */
         .pipe(gulp.dest('src'));
    });
    
    gulp.task('task2', ['task1'], function() {
        gulp.src('src/*.js')
          /* eg minfify js here */
         .pipe(gulp.dest('dest'));
    });
    

    in that example you'd expect task1 to complete ( eg compiling the coffeescript or whatever ) before task2 runs ... but unless we add return – like the example below – then they will run synchronously not asynchronously; and the compiled coffeescript will not be minified because task2 will not have waited for task 1 to complete and so will not pick up on the compiled output of task1. So we should always return in these circumstances.

    // with return
    gulp.task('task1', function() {
        return gulp.src('**/*.coffee')
          /* your operations here */
         .pipe(gulp.dest('dest'));
    });
    
    gulp.task('task2', ['task1'], function() {
        return gulp.src('**/*.js')
          /* your operations here */
         .pipe(gulp.dest('dest'));
    });
    

    Edit: The recipe here explains it further. https://github.com/gulpjs/gulp/blob/master/docs/recipes/running-tasks-in-series.md

提交回复
热议问题