0

I followed this tutorial to set up a Gruntfile for a little project I'm working on. everything seems to be working great, except for the live reload part. I don't get any errors, but no live reloading either. Here is the code:

var browserify = require('browserify'),
    buffer = require('vinyl-buffer'),
    concat = require('gulp-concat'),
    del = require('del'),
    embedlr = require('gulp-embedlr'),
    express = require('express'),
    gulp = require('gulp'),
    gutil = require('gulp-util'),
    jshint = require('gulp-jshint'),
    livereload = require('connect-livereload'),
    lrserver = require('tiny-lr')(),
    refresh = require('gulp-livereload'),
    replace = require('gulp-replace'),
    source = require('vinyl-source-stream'),
    sourcemaps = require('gulp-sourcemaps'),
    uglify = require('gulp-uglify');

// clean
gulp.task('clean', function(callback) {
  del('./dist/*', callback);
});

// img
gulp.task('img', function() {
  return gulp.src('./src/img/*')
        .pipe(gulp.dest('./dist/img/'));
});

// index
gulp.task('index', function() {
  return gulp.src('./src/index.html')
        .pipe(replace('__now__', new Date().toDateString()))
        .pipe(gulp.dest('./dist/'));
});

gulp.task('index-dev', function() {
  return gulp.src('./src/index.html')
        .pipe(replace('__now__', new Date().toDateString()))
        .pipe(gulp.dest('./dist/'))
        .pipe(refresh(lrserver));
});

// script
gulp.task('lint', function() {
  return gulp.src('./src/script/*')
        .pipe(jshint())
        .pipe(jshint.reporter('default'));
});

gulp.task('script', [ 'lint' ], function() {
  browserify({ entries: './src/script/dforbes.js', debug: true })
  .bundle()
  .pipe(source('dforbes.js'))
  .pipe(buffer())
  .pipe(uglify({ mangle: false }))
  .on('error', gutil.log)
  .pipe(gulp.dest('./dist/script/'));
});

gulp.task('script-dev', [ 'lint' ], function() {
  browserify({ entries: './src/script/dforbes.js', debug: true })
  .bundle()
  .pipe(source('dforbes.js'))
  .pipe(buffer())
  .pipe(sourcemaps.init({ loadMaps: true }))
  .pipe(uglify({ mangle: false }))
  .on('error', gutil.log)
  .pipe(sourcemaps.write('./'))
  .pipe(gulp.dest('./dist/script/'));
});

// style
gulp.task('style', function() {
  return gulp.src('./src/style/*')
        .pipe(gulp.dest('./dist/style/'));
});

// tmp
gulp.task('tmp', function() {
  return gulp.src('./src/tmp/*')
        .pipe(gulp.dest('./dist/tmp/'));
});

gulp.task('tmp-dev', function() {
  return gulp.src('./src/tmp/*')
        .pipe(gulp.dest('./dist/tmp/'))
        .pipe(refresh(lrserver));
});

// watch
gulp.task('watch', function() {
  gulp.watch([ './src/img/*' ], [ 'img' ]);
  gulp.watch([ './src/index.html' ], [ 'index-dev' ]);
  gulp.watch([ './src/script/*' ], [ 'script-dev' ]);
  gulp.watch([ './src/style/*' ], [ 'style' ]);
  gulp.watch([ './src/tmp/*' ], [ 'tmp-dev' ]);
});

// dev
gulp.task('dev', [ 'clean' ], function() {
  gulp.start([ 'img', 'index', 'script-dev', 'style', 'tmp' ]);

  var livereloadPort = 35729;
  var serverPort = 8080;

  var server = express();
  server.use(livereload({ port: livereloadPort, start: true }));
  server.use(express.static('./dist'));
  server.all('/*', function(req, res) {
    res.sendFile('index.html', { root: 'dist' });
  });

  server.listen(serverPort);
  lrserver.listen(livereloadPort);

  gulp.start('watch');
});

// default
gulp.task('default', [ 'clean' ], function() {
  gulp.start([ 'img', 'index', 'script', 'style', 'tmp' ]);
});
Dan Forbes
  • 2,734
  • 3
  • 30
  • 60

1 Answers1

1

You miss the refresh call to let LiveReload know that something has changed on some parts. Here's the change for your style task:

gulp.task('style', function() {
   return gulp.src('./src/style/*')
       .pipe(gulp.dest('./dist/style/'))
       .pipe(refresh());
});

Just add the last pipe everywhere you want the refresh to happen.

Update

Also, there are some issues in your Gulpfile. Please return every gulp.src statement you find, also don't put two gulp.src statements into one gulp.task if you don't merge them.

// wrong
gulp.task('tmp-dev', function() {
  gulp.src('./src/index.html')
  .pipe(gulp.dest('./dist/'))
  .pipe(refresh(lrserver));

  gulp.src('./src/tmp/*')
  .pipe(gulp.dest('./dist/tmp/'))
  .pipe(refresh(lrserver));
});

// correct
gulp.task('tmp-dev', function() {
  return gulp.src(['./src/index.html','./src/tmp/*'])
    .pipe(gulp.dest('./dist/'))
    .pipe(refresh(lrserver));
});

Another solution would be to create a watcher pointed to the dist directory, refreshing everytime something has changed in there:

 gulp.watch('./dist/**/*', refresh);

The other way would be preferred though, as it gives you more control Edit: This one's bogus

ddprrt
  • 7,424
  • 3
  • 29
  • 25
  • Right now, I'm only attempting to invoke this through the tmp-dev task, which has two calls to ````refresh(lrserver)````. Am I misusing it? – Dan Forbes May 03 '15 at 13:03
  • I don't know what's inside the `tmp` folder, but, actually this would just trigger a reload when the `index.html` file changes, it doesn't listen to your assets (scripts, styles, images ...). This call has to be put everywhere things are created that you want to have refreshed – ddprrt May 03 '15 at 13:06
  • Right, so, per your comment above, I'm only trying to get this to work on changes to my HTML files right now...once I get that working, I'll move the feature over to the other copy tasks. Why do I need to return from my gulp tasks? I don't notice any difference when I do that. I tried both of your suggestions, and live reload is still not working. – Dan Forbes May 03 '15 at 15:47
  • 1
    You have to return that stream to let Gulp know when this task is finished. Otherwise you are just starting tasks without letting the task manager know what's going on. See http://stackoverflow.com/questions/21699146/gulp-js-task-return-on-src for more details and the docs. The last solution won't work for me either, I tried it with https://github.com/Hyra/angular-gulp-browserify-livereload-boilerplate/blob/master/Gulpfile.js -- there seem to be other issues as well. How does your `tmp-dev` task look now? – ddprrt May 03 '15 at 16:24
  • I've updated my question to reflect the current state of my Gruntfile.js. – Dan Forbes May 03 '15 at 16:32