Gulp task don't wait for previous task to complete

I have a gulp task that uses streams to write a revised file (using gulp-rev ) and another task that uses a previously written manifest file to replace variables in the html file.

My first task:

gulp.task('scripts', function() {
  // other stuff...
  var b = browserify(mainscript);
  b.external('External')
    .transform(babelify)
    .bundle()
    .pipe(source('app.js'))
    .pipe(buffer())
    .pipe(rev())
    .pipe(gulp.dest(dest))
    .pipe(rev.manifest({
      path: 'appjs-manifest.json'
    }))
    .pipe(gulp.dest(dest))
}

      

The previous task will write a file named "app-34f01b9d.js" and a manifest file "appjs-manifest.json" containing:

{
  "app.js": "app-34f01b9d.js"
}

      

My second task, in theory, should wait for the first task to complete before starting:

gulp.task('renameindex',['scripts'], function () {
    var paths = gulp.paths;
    var jsonContent = JSON.parse(fs.readFileSync(paths.buildDest+'/public/scripts/appjs-manifest.json', 'utf8'));
    var src=paths.srcDir+"/index.hbs";
    var appjs=jsonContent["app.js"];
    console.log("app.js",appjs)
    return gulp.src(src)
            .pipe(handlebars({appjs:appjs}, null))
            .pipe(rename('index.html'))
            .pipe(gulp.dest(paths.buildDest));

});

      

Tasks should read the manifest file and replace "app-34f01b9d.js" with the {appjs}} variable in the descriptor template, creating an "index.html" file.

What happens is that the javascript file and the manifest file are written correctly, but the second task is executed without waiting for the manifest file to be written, thus capturing the wrong manifest file (one from the previous execution).

+3


source to share


1 answer


Your task scripts

does not return any value. When you don't return anything, gulp has no way of knowing when the task is finished, so the next one will continue.

just tell

gulp.task('scripts', function() {
  // other stuff...
  var b = browserify(mainscript);
  return b.external('External')
    ...

      



If your task is executing something other than a gulp line, you can manually invoke a callback that is passed to the task as a parameter:

gulp.task('someTask', function(done) {
...
done();
}

      

+3


source







All Articles