Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set gulp livereload to run after all files are compiled?

I've got a gulp set up to work with Stylus, Jade and tiny-lr. My problem is that when I save one jade file, it start's compiling them all, therefore live reloading fires on the first file copied to the destination, before the file I am working on currently is compiled, resulting in me having to refresh manually. I have fixing this issue using "gulp-changed" but I don't seem to be able to configure it or something. Anyone had this problem before? I am posting my Gulp file so you can take a look.

A timeline diagram of the problem can be found here: https://www.dropbox.com/s/3g37oy25s9mq969/jade_compile_frefresh_problem.png?dl=0

Any help is appreciated!

    'use strict';

    var gulp = require('gulp');
    var jade = require('gulp-jade');
    var gutil = require('gulp-util');
    var stylus = require('gulp-stylus');
    var jeet = require('jeet');
    var nib = require('nib');
    var uglify = require('gulp-uglify');
    var lr = require('tiny-lr')();
    // var mainBowerFiles = require('main-bower-files');

    // Define sources object
    var sources = {
      jade: "jade/**/*.jade",
      partials: "partials/**/*.jade",
      stylus: "styl/**/*.styl",
      scripts: "js/**/*.js"
    };

    // Define destinations object

    var destinations = {
      html: "dist/",
      css: "dist/css",
      js: "dist/js"
    };

    // Compile and copy Jade
    gulp.task("jade", function(event) {
      return gulp.src(sources.jade)
      .pipe(jade({
        pretty: true
      })).pipe(gulp.dest(destinations.html));
    });

    // Compile and copy Stylus
    gulp.task("stylus", function(event) {
      return gulp.src(sources.stylus).pipe(stylus({
        use: [nib(), jeet()],
        import: [
          'nib',
          'jeet'
        ],
        style: "compressed"
      })).pipe(gulp.dest(destinations.css));
    });

    // Minify and copy all JavaScript
    gulp.task('scripts', function() {
      gulp.src(sources.scripts)
        .pipe(uglify())
        .pipe(gulp.dest(destinations.js));
    });

    // Consolidate Bower Files and copy to /dist/js/
    // gulp.task('bower-files', function() {
    //   return gulp.src(mainBowerFiles(/* options */), {})
    //     .pipe(gulp.dest(destinations.js));
    // });

    // Watch for file changes and execute tasks
    gulp.task("watch", function() {
      gulp.watch(sources.jade, ["jade"]);
      gulp.watch(sources.partials, ["jade"]);
      gulp.watch(sources.stylus, ["stylus"]);
      gulp.watch(sources.scripts, ["scripts"]);
      gulp.watch('dist/**/*', refresh);
    });

    // Live Reload
    gulp.task('serve', function () {
      var express = require('express');
      var app = express();
      app.use(require('connect-livereload')());
      app.use(express.static(__dirname+'/dist/'));
      app.listen(4000, '0.0.0.0');
      lr.listen(35729);
    });

    // Define default task
    gulp.task("default", ["jade", "stylus", "scripts", "serve", "watch"]);

    // Refresh function
    var refresh = function(event) {
      var fileName = require('path').relative(__dirname, event.path);
      gutil.log.apply(gutil, [gutil.colors.magenta(fileName), gutil.colors.cyan('built')]);
      lr.changed({
        body: { files: [fileName] }
      });
    };
like image 711
bitstream Avatar asked Dec 25 '22 05:12

bitstream


1 Answers

I have achieve this by writing a simple javascript function in my gulpfile.js

I did this because when I compile my sass files, livereload will run around 10 times, this method will make it only run once.

My gulpfile.js

gulp.task('watch', function() {
    $.livereload.listen();

    gulp.watch([
        path.join(config.path.app, 'media/js/**/*.js'),
        path.join(config.path.app, 'media/css/**/*.css'),
        path.join(config.path.app, 'templates/**/*.html')
    ]).on('change', stackReload);

    // a timeout variable
    var timer = null;

    // actual reload function
    function stackReload() {
        var reload_args = arguments;

        // Stop timeout function to run livereload if this function is ran within the last 250ms
        if (timer) clearTimeout(timer);

        // Check if any gulp task is still running
        if (!gulp.isRunning) {
            timer = setTimeout(function() {
                $.livereload.changed.apply(null, reload_args);
            }, 250);
        }
    }

});
like image 192
AlexCheuk Avatar answered Dec 27 '22 18:12

AlexCheuk