Livereload of index.html with Gulp

Pete Norris picture Pete Norris · Feb 10, 2014 · Viewed 17.3k times · Source

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');
});

Answer

RWAM picture RWAM · Feb 14, 2014

You can try to put put your html files into a subfolder like components/html and use a task similar to the styles task:

gulp.task('html', function() {
    return gulp.src('components/html/**/*.html')
        .pipe(gulp.dest(''))
        .pipe(livereload(server))
        .pipe(notify({ message: 'HTML task complete' }));
});

Your default task should look like:

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

And modify your watch task:

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']);
        // Watch html files
        gulp.watch('components/html/**/*.html', ['html']);
    });
});

Ciao Ralf