简体   繁体   English

浏览器同步不会刷新gulp-watch观察到的文件

[英]Browser-sync is not refreshing files observed by gulp-watch

In my build process I want to put the artifacts of the build steps into .tmp/serve directory. 在构建过程中,我想将构建步骤的工件放入.tmp/serve目录。

I managed to set up gulp-watch to fire specific tasks when I modify files in my working directory. 在修改工作目录中的文件时,我设法设置了gulp-watch来执行特定任务。 Eq when I modify htmls, they are built and the artifacts are pasted into desired .tmp/serve directory 例如,当我修改html时,它们将被构建并将工件粘贴到所需的.tmp/serve目录中

I'm having problems with reloading the files from .tmp/serve with browser-sync . 我在使用browser-sync.tmp/serve重新加载文件时遇到问题。 When I save changes in my working directory multiple times, browser-sync refreshes only the previous changes ( 1 change delay ). 当我在工作目录中多次保存更改时, browser-sync仅刷新以前的更改( 1个更改延迟 )。 I guess the reload function fires before gulp-dest finishes. 我猜想在gulp-dest完成之前会启动reload函数。 Note that if I refresh browser manually the current changes appear. 请注意,如果我手动刷新浏览器,则会显示当前更改。

Am I doing something wrong? 难道我做错了什么?

Build htmls task 建立htmls任务

gulp.task('html', ['styles'], () => {
  return gulp.src('app/*.html')
    .pipe($.if('*.html', $.minifyHtml({conditionals: true, loose: true})))
    .pipe(gulp.dest('.tmp/serve'))
    ;
});

Sever task 服务器任务

const reload = browserSync.reload;

gulp.task('serve', ['styles', 'fonts', 'build:scripts', 'html', 'images'], () => {
  browserSync({
    notify: false,
    port: 9000,
    server: {
      baseDir: ['.tmp/serve'],
      routes: {
        '/bower_components': 'bower_components'
      }
    }
  });

  /***********  SEE THE LINES BELOW  **************/
  gulp.watch([
    '.tmp/serve/**/*',
  ]).on('change', reload);

  gulp.watch('app/styles/**/*.scss', ['styles']);
  gulp.watch('app/fonts/**/*', ['fonts']);
  gulp.watch('app/*.html', ['html']);
  gulp.watch('app/scripts/**/*', ['build:scripts']);
  gulp.watch('bower.json', ['wiredep', 'fonts']);
});

You can use run-sequence to reload after a build. 您可以在构建后使用运行顺序重新加载。

Your gulpfile would become like this.. 您的gulpfile会变成这样。

var gulp = require('gulp'),
    runSeq = require('run-sequence')
    ...
    ;

gulp.task('build', ['styles', 'fonts', 'build:scripts', 'html', 'images']);

gulp.task('serve', ['build', 'watch'], () => {
  browserSync({
    notify: false,
    port: 9000,
    server: {
      baseDir: ['.tmp/serve'],
      routes: {
        '/bower_components': 'bower_components'
      }
    }
  });

gulp.task('reload', function (callback) {
   browserSync.reload();
   callback();
});

gulp.task('watch', ['watch-styles', ...]);

gulp.task('watch-styles', function () {
   gulp.watch('app/styles/**/*.scss', function () {
      runSeq('styles', 'reload');
   });
});

/* And so on for every watch task */

Note that run-sequence needs your tasks to either return a stream or to call a callback, otherwise it will cause gulp to pause indefinitely. 请注意,运行顺序需要您的任务返回流或调用回调,否则将导致gulp无限期暂停。 Read the Doc to learn more. 阅读文档以了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM