Search code examples
livereloadgulp

Livereload of index.html with Gulp


I am attempting to have my browser update when I make a change to index.html in the root of my project. Livereloading is working fine when updating the CSS. I have removed the efforts I made to get this working in my gulpfile.js, below...

Thanks!

var gulp = require('gulp'),
sass = require('gulp-ruby-sass'),
autoprefixer = require('gulp-autoprefixer'),
minifycss = require('gulp-minify-css'),
jshint = require('gulp-jshint'),
uglify = require('gulp-uglify'),
imagemin = require('gulp-imagemin'),
rename = require('gulp-rename'),
clean = require('gulp-clean'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
cache = require('gulp-cache'),
livereload = require('gulp-livereload'),
lr = require('tiny-lr'),
server = lr();

gulp.task('styles', function() {
return gulp.src('components/sass/style.scss')
.pipe(sass({ style: 'expanded' }))
.pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6',         'android 4'))
.pipe(gulp.dest(''))
.pipe(rename({suffix: '.min'}))
.pipe(minifycss())
.pipe(gulp.dest(''))
.pipe(livereload(server))
.pipe(notify({ message: 'Styles task complete' }));
});

gulp.task('scripts', function() {
return gulp.src('components/js/*.js')
// .pipe(jshint('.jshintrc'))
.pipe(jshint.reporter('default'))
.pipe(concat('main.js'))
.pipe(gulp.dest('js'))
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest('js'))
.pipe(livereload(server))
.pipe(notify({ message: 'Scripts task complete' }));

});

gulp.task('images', function() {
return gulp.src('components/img/*')
.pipe(imagemin({ optimizationLevel: 5, progressive: true, interlaced: true }))
.pipe(gulp.dest('img'))
.pipe(livereload(server))
.pipe(notify({ message: 'Images task complete' }));
});

// gulp.task('clean', function() {
//   return gulp.src(['css', 'js', 'img'], {read: false})
//     .pipe(clean());
// });

gulp.task('watch', function() {

server.listen(35729, function (err) {
if (err) {
return console.log(err)
};
// Watch .scss files
gulp.watch('components/sass/*.scss', ['styles']);
// Watch .js files
gulp.watch('components/js/*.js', ['scripts']);
// Watch image files
gulp.watch('components/img/*', ['images']);
});    
});

// gulp.task('default', ['clean'], function() {
gulp.task('default', function() {
gulp.start('styles', 'scripts', 'images');
});

Solution

  • There may be a better way to do what you want to do.

    Have you thought about maybe using an instance of express with livereload?

    The alternative is to use gulp-connect which will make what you're trying to do much easier. In either case, instead of invoking livereload within tasks, handle livereloading within the task that sets up your server. The general idea is that you set a watch task on your files that you want livereloading and then reload on changes.

    gulp.task('serve', function(event) {
        connect.server({
            root: destinations.docs,
            port: 1987,
            livereload: true
        });
        // sets up a livereload that watches for any changes in the root
        watch({glob: [sources.html, sources.styles]})
            .pipe(connect.reload());
    });
    

    I've experimented with both ways and gulp-connect is a much easier option. Regardless, I've written two gulp boilerplates which you can see at https://github.com/jh3y/gulp-boilerplate-v2 and https://github.com/jh3y/gulp-boilerplate .

    Hope that helps!