gulp-livereload - 当只有 CSS 发生变化时,gulp livereload 重新加载整个页面

标签 gulp-livereload

我已经将 livereload 添加到我的 Gulp 任务中。除了当我编辑 CSS 文件时,整个页面都会刷新,而不仅仅是页面 CSS。

 var gulp = require('gulp');
 var uglify = require('gulp-uglify');
 var concat = require('gulp-concat');
 var minifyCss = require('gulp-minify-css');
 var sizereport = require('gulp-sizereport');
 var watch = require('gulp-watch');
 var batch = require('gulp-batch');
 var run = require('run-sequence');

gulp.task('watch-theme-css', ['theme-css'], function () {
  livereload.listen();
  watch(themeFiles.sass, batch(function (events, done) {
    gulp.start('theme-css', done);
  }))
});

var themeFiles = {
  sass: [
    'mysite/base/sass/*.s+(a|c)ss',
    'mysite/content/sass/*.s+(a|c)ss',
    'mysite/custom/sass/*.s+(a|c)ss'
  ],
  out: {
    css: 'mysite/build'
  }
};

gulp.task('theme-css', function () {
  return gulp.src(themeFiles.sass)
    .pipe(gulpif(env === 'development', sourcemaps.init()))
    .pipe(sass().on('error', sass.logError))
    .pipe(minifyCss({
      compatibility: 'ie8'
    }))
    .pipe(gulpif(env === 'dev', sourcemaps.write('.')))
    .pipe(gulp.dest(themeFiles.out.css))
    .pipe(livereload());
});

更新我从下面的链接中尝试了以下代码,但它做同样的事情。 http://www.roelvanlisdonk.nl/?p=4675
gulp.task('watch-theme-css', ['theme-css'], function () {
  livereload.listen();
  watch(themeFiles.sass, batch(function (events, done) {
    gulp.start('theme-css', done);
  }), ["reloadCss"]);
});

与此相同的行为:https://laracasts.com/discuss/channels/tips/proper-way-to-use-livereload-with-laravel-elixir
gulp.task('watch-lr-css', ['theme-css'], function () {
  livereload.changed(themeFiles.sass);
});

我尝试了以下操作,但是当我尝试打开实时重载浏览器插件时,它说找不到实时重载服务器。
gulp: how to update the browser without refresh (for css changes only)
gulp.task('watch-theme-css', ['theme-css'], function () {
  //livereload.listen();
  livereload.changed(themeFiles.sass);
  watch(themeFiles.sass, batch(function (events, done) {
    gulp.start('theme-css', done);
  }));
});

最佳答案

我昨晚花了几个小时,发现了这个:https://github.com/vohof/gulp-livereload/issues/93 .

看起来是因为你的源 map 。 gulp-livereload尝试变得聪明,只有在只有 css 更改时才重新加载 css。否则它会重新加载整个页面,因为它认为有更多文件发生了变化。

因此,在调用 livereload() 之前,您只需将 glob 过滤为仅 css。功能。

所以这样的事情可能会有所帮助:

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

...

gulp.task('theme-css', function () {
  return gulp.src(themeFiles.sass)
    .pipe(gulpif(env === 'development', sourcemaps.init()))
    .pipe(sass().on('error', sass.logError))
    .pipe(minifyCss({
      compatibility: 'ie8'
    }))
    .pipe(gulpif(env === 'dev', sourcemaps.write('.')))
    .pipe(gulp.dest(themeFiles.out.css))

    // Add filter here
    .pipe(filter('**/*.css'))
    .pipe(livereload());
});

关于gulp-livereload - 当只有 CSS 发生变化时,gulp livereload 重新加载整个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34273127/

相关文章:

android - 基于 generator-gulp-angular 在 Ionic 项目上配置 Live Reload

node.js - gulp & nodemon 引起的内存泄漏

python - 是否可以为 Django 配置 Gulp Livereload?

javascript - Gulp-livereload、-webserver 和 -embedlr。如何一起使用它们?

node.js - Gulp 在 Node app.listen() 被调用或移植时观察(livereload、nodejs 和 gulp)

javascript - gulp-live-server 不刷新页面

css - gulpfile.js 帮助添加实时重新加载