gulpgulp-replace

How to run gup task in series


I am new to gulp.

I have written two task that need to be performed. When I run them separately, they work fine. But when I combine them, the "replace" does not work.

gulp.task('bundle-source', function () {
  return bundler.bundle(config);
});

gulp.task('bundle-config', function(){
   return gulp.src(['config.js'])
     .pipe(replace('src/*', 'dist/*'))
    .pipe(gulp.dest(''));
});

gulp.task('bundle', ['bundle-config', 'bundle-source']);

I think the issue is that they both manipulate config.js. I think the second task when it saves to disk overwrites the change the first one made. The second task is about 30 seconds.


Solution

  • Gulp tasks are run in parallel by default. So if your tasks are working on the same files, they might step on each others' toes indeed.

    You can use gulp's tasks dependencies to have them run one after the other. So if bundle-config should be run before bundle-source :

    gulp.task('bundle-source', ['bundle-config'], function () {
      return bundler.bundle(config);
    });
    

    You can also use a package like run-sequence if you need them to run one after the other :

    var seq = require('run-sequence');
    
    gulp.task('bundle', function(cb) {
        return seq('bundle-config', 'bundle-source', cb);
    });
    

    Finally, You could use gulp 4, which has a built-in mechanism to run tasks in series.