Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp livereload reloads the entire page when only CSS has changed

Ive added livereload to my Gulp task. Its working except when I edit a CSS file the entire page is refreshed, not just the pages 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());
});

Update Ive tried the following code from the link below but it does the same thing. 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"]);
});

Same behaviour from this: 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);
});

I tried the following but when I try and turn on the live reload browser plugin it says it cannot find the live reload server. 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);
  }));
});
like image 601
Evanss Avatar asked Dec 14 '15 17:12

Evanss


1 Answers

I spent a few hours on this last night and came across this: https://github.com/vohof/gulp-livereload/issues/93.

Looks like it's because of your sourcemaps. gulp-livereload tries to be smart and only reloads the css if there are only css changes. Otherwise it reloads the whole page because it thinks there are more files that changed.

So you just filter your glob down to only css before you call the livereload() function.

So something like this might help:

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());
});
like image 160
Tyler Graf Avatar answered Oct 21 '22 20:10

Tyler Graf