How to run Gulp tasks sequentially one after the other

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

    To wait and see if the task is finished and then the rest, I have it this way:

    gulp.task('default',
      gulp.series('set_env', gulp.parallel('build_scss', 'minify_js', 'minify_ts', 'minify_html', 'browser_sync_func', 'watch'),
        function () {
        }));
    

    Kudos: https://fettblog.eu/gulp-4-parallel-and-series/

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

    For me it was not running the minify task after concatenation as it expects concatenated input and it was not generated some times.

    I tried adding to a default task in execution order and it didn't worked. It worked after adding just a return for each tasks and getting the minification inside gulp.start() like below.

    /**
    * Concatenate JavaScripts
    */
    gulp.task('concat-js', function(){
        return gulp.src([
            'js/jquery.js',
            'js/jquery-ui.js',
            'js/bootstrap.js',
            'js/jquery.onepage-scroll.js',
            'js/script.js'])
        .pipe(maps.init())
        .pipe(concat('ux.js'))
        .pipe(maps.write('./'))
        .pipe(gulp.dest('dist/js'));
    });
    
    /**
    * Minify JavaScript
    */
    gulp.task('minify-js', function(){
        return gulp.src('dist/js/ux.js')
        .pipe(uglify())
        .pipe(rename('ux.min.js'))
        .pipe(gulp.dest('dist/js'));
    });
    
    gulp.task('concat', ['concat-js'], function(){
       gulp.start('minify-js');
    });
    
    gulp.task('default',['concat']); 
    

    Source http://schickling.me/synchronous-tasks-gulp/

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

    The only good solution to this problem can be found in the gulp documentation:

    var gulp = require('gulp');
    
    // takes in a callback so the engine knows when it'll be done
    gulp.task('one', function(cb) {
      // do stuff -- async or otherwise
      cb(err); // if err is not null and not undefined, the orchestration will stop, and 'two' will not run
    });
    
    // identifies a dependent task must be complete before this one begins
    gulp.task('two', ['one'], function() {
      // task 'one' is done now
    });
    
    gulp.task('default', ['one', 'two']);
    // alternatively: gulp.task('default', ['two']);
    
    0 讨论(0)
  • 2020-11-22 13:20

    It's not an official release yet, but the coming up Gulp 4.0 lets you easily do synchronous tasks with gulp.series. You can simply do it like this:

    gulp.task('develop', gulp.series('clean', 'coffee'))
    

    I found a good blog post introducing how to upgrade and make a use of those neat features: migrating to gulp 4 by example

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

    Simply make coffee depend on clean, and develop depend on coffee:

    gulp.task('coffee', ['clean'], function(){...});
    gulp.task('develop', ['coffee'], function(){...});
    

    Dispatch is now serial: cleancoffeedevelop. Note that clean's implementation and coffee's implementation must accept a callback, "so the engine knows when it'll be done":

    gulp.task('clean', function(callback){
      del(['dist/*'], callback);
    });
    

    In conclusion, below is a simple gulp pattern for a synchronous clean followed by asynchronous build dependencies:

    //build sub-tasks
    gulp.task('bar', ['clean'], function(){...});
    gulp.task('foo', ['clean'], function(){...});
    gulp.task('baz', ['clean'], function(){...});
    ...
    
    //main build task
    gulp.task('build', ['foo', 'baz', 'bar', ...], function(){...})
    

    Gulp is smart enough to run clean exactly once per build, no matter how many of build's dependencies depend on clean. As written above, clean is a synchronization barrier, then all of build's dependencies run in parallel, then build runs.

    0 讨论(0)
  • 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.

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