Browser sync not working with gulp when SASS updat

2019-09-10 11:17发布

I have a gulpfile with browser sync running and js/sass compiling. It serves my Angular SPA. It looks like this:

gulp.task('default', function(){
  return runSequence(['build-css', 'build-js'], 'serve-dev');
});

gulp.task('serve-dev', function() {

  browserSync.init(null, {
    server: {
      baseDir: './',
      middleware: [
        modRewrite([
          '!\\.\\w+$ /index.html [L]'
        ])
      ]
    }
  });

  gulp.watch(paths.js.dir + '/**/*.js', ['watch-js']);
  gulp.watch(paths.css.dir + '/**/*.scss', ['watch-css']);
  gulp.watch(paths.html.dir + '/**/*.html').on('change', browserSync.reload);

});

gulp.task('watch-css', ['build-css'], browserSync.reload);
gulp.task('watch-js', ['build-js'], browserSync.reload);

gulp.task('build-css', function() {
  return gulp.src(paths.css.src)
    .pipe(sourcemaps.init())
    .pipe(sass().on('error', sass.logError))
    .pipe(autoprefixer({
      browsers: ['> 0.5%']
    }))
    .pipe(cleanCSS())
    .pipe(rename('app.min.css'))
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest(paths.css.dest));
});

gulp.task('build-js', function() {
  return gulp.src(paths.js.src)
    .pipe(sourcemaps.init())
    .pipe(concat('app.min.js'))
    .pipe(ngAnnotate())
    .pipe(uglify())
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest(paths.js.dest));
});

It works initially to serve the site, and when I update html files it refreshes, however when I update a SASS file it doesn't refresh. All the paths are definitely correct because the initial compiling works fine.

1条回答
Evening l夕情丶
2楼-- · 2019-09-10 11:42

Add

.pipe(browserSync.stream())

at the end of 'build-css' task. It worked for me

查看更多
登录 后发表回答