0

Consider this example given on the BrowserSync + Gulp page regarding Browser Reloading, especially this part:

// use default task to launch BrowserSync and watch JS files
gulp.task('default', ['browser-sync'], function () {

    // add browserSync.reload to the tasks array to make
    // all browsers reload after tasks are complete.
    gulp.watch("js/*.js", ['js', browserSync.reload]);
});

As task dependencies are run asynchronously (here: the js and browserSync.reload) couldn't it happen that the reload finishes before the js task?

4

1 回答 1

1

是的,根据文档,这是可能的。

在同一页上...

 (make sure you return the stream from your tasks to ensure the browser is reloaded at the correct time)

如果它是一个异步任务,它只会触发而不返回任何内容,并且观察者将不知道刷新。或者它可能会在该过程完成之前重新加载。

为了解决这个问题,您应该为您的任务添加回调。

gulp.task('somename', function() {
  var stream = gulp.src('client/**/*.js')
    .pipe(minify())
    .pipe(gulp.dest('build'));
  return stream;
});

只需返回流,以便 Gulp 知道发生了什么。然后为您想要的任务设置手表:

gulp.task('default', ['browser-sync'], function () {
  // Watched tasks are run in parallel, not in series.
  gulp.watch(['*.js'], ['somename', browserSync.reload]);
});

这都包含在文档中:

https://github.com/gulpjs/gulp/blob/master/docs/API.md#async-task-support

于 2015-02-04T23:36:48.740 回答