Browser sync not working with gulp when SASS updated

会有一股神秘感。 提交于 2019-12-12 04:11:06

问题


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:


Add

.pipe(browserSync.stream())

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



来源:https://stackoverflow.com/questions/36918358/browser-sync-not-working-with-gulp-when-sass-updated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!