为什么不通过gulp-connect重新加载工作?

Mis*_*hko 14 livereload gulp

我用gulp-connect这样的:

gulp.task('watch', function() {
  gulp.watch(paths.scss, ['scss']);

  gulp.watch(distPaths, function() {
    return gulp.src(distPaths)
               .pipe(connect.reload());
  });
});
Run Code Online (Sandbox Code Playgroud)

实时重载位工作正常.

我的问题是,为什么以下不起作用:

gulp.task('watch', function() {
  gulp.watch(paths.scss, ['scss']);
  gulp.watch(distPaths, connect.reload);
});
Run Code Online (Sandbox Code Playgroud)

gulp.src(distPaths)是为了什么?

还有什么重要性return

Ove*_*ous 21

  1. connect.reload()需要在流中使用.在您的示例中,您尝试将其称为独立函数,它不执行任何操作.调用connect.reload()返回一个流处理程序,它接受输入并对其执行某些操作.在第二个例子中,它实际上是一个无操作.

  2. 您需要return任务同步运行.(您还可以返回承诺或使用回调函数.)

    基本上,如果你没有为gulp提供某种方法来跟踪异步任务的进度,那么当你从函数返回时它必须假设它们是完整的.如果你return是管道,它可以听取它到达终点.

此外,它可能更适合gulp-watch用于您的手表,因为它只传递更改的文件,这是您想要的实时重新加载:

var watch = require('gulp-watch');

gulp.task('watch', function() {
    gulp.watch(paths.scss, ['scss']);
    watch(distPath).pipe(connect.reload());
});
Run Code Online (Sandbox Code Playgroud)