Need help. I use gulp-conect and it livereload method. But if I build a few template in time, get a lot of page refresh. Is any solution, I want to build few templates with single page refresh?
4 Answers
So, I reproduce the problem you have and came accross this working solution.
First, lets check gulp plugins you need:
- gulp-jade
- gulp-livereload
- optional: gulp-load-plugins
In case you need some of them go to:
Search for them and install them.
Strategy: I created a gulp task called live that will check your *.jade files, and as you are working on a certain file & saving it, gulp will compile it into html and refresh the browser.
In order to accomplish that, we define a function called compileAndRefresh that will take the file returned by the watcher. It will compile that file into html and the refesh the browser (test with livereload plugin for chrome).
Notes:
- I always use gulp-load-plugin to load plugins, so thats whay I use plugins.jad and plugins.livereload.
- This will only compile files that are saved and while you have the task live exucting on the command line. Will not compile other files that are not in use. In order to accomplish that, you need to define a task that compiles all files, not only the ones that have been changed.
- Assume .jade files in /jade and html output to /html
So, here is the gulpfile.js:
var gulp = require('gulp'),
gulpLoadPlugins = require('gulp-load-plugins'),
plugins = gulpLoadPlugins();
gulp.task('webserver', function() {
gulp.src('./html')
.pipe(plugins.webserver({
livereload: true
}));
gulp.watch('./jade/*.jade', function(event) {
compileAndRefresh(event.path);
});
});
function compileAndRefresh(file) {
gulp.src(file)
.pipe(plugins.jade({
}))
.pipe(gulp.dest('./html'))
}
Post edit notes:
- Removed liveReload call from compileAndRefresh (webserver will do that).
- Use gulp-server plugin insted of gulp-connect, as they suggest on their repository: "New plugin based on connect 3 using the gulp.src() API. Written in plain javascript. https://github.com/schickling/gulp-webserver"

- 9,041
- 2
- 28
- 37
-
What about small static server? – Serhiy Jul 14 '14 at 05:34
-
What about it ? I dont understand... This does exactly what you aked, isnt ? What do you mean with static server ? – avcajaraville Jul 14 '14 at 06:09
-
Can you add static server to this stack, please? – Serhiy Jul 14 '14 at 07:56
-
Ok, will edit the answer and add it. Now the task will be called webserver and will act as server, and also compile .jade files and once compiled, will refresh. – avcajaraville Jul 14 '14 at 09:40
-
Everything is normal, but if I build included file, than have no reload. – Serhiy Jul 15 '14 at 07:58
-
Can you expand your comment ? I dont understand you. What do you mean if you build included file ? – avcajaraville Jul 15 '14 at 07:59
-
This is a watcher, which means it only will compile files that have been changed since you start the task. You have to define a task that compiles all files, and then, you can watch them and have your browser reloaded. – avcajaraville Jul 15 '14 at 08:00
-
Works:) thnk for the help – Serhiy Jul 15 '14 at 08:39
-
One more describe here [issuecomment-49012538](https://github.com/AveVlad/gulp-connect/issues/71#issuecomment-49012538) – Serhiy Jul 15 '14 at 13:57
Something you can do is to watch only files that changes, and then apply a function only to those files that have been changed, something like this:
gulp.task('live', function() {
gulp.watch('templates/folder', function(event) {
refresh_templates(event.path);
});
});
function refresh_templates(file) {
return
gulp.src(file)
.pipe(plugins.embedlr())
.pipe(plugins.livereload());
}
PS: this is not a working example, and I dont know if you are using embedlr, but the point, is that you can watch, and use a callback to call another function with the files that are changing, and the manipulate only those files. Also, I supposed that your goal is to refresh the templates for your browser, but you manipulate as you like, save them on dest or do whatever you want.
Key point here is to show how to manipulate file that changes: callback of watch + custom function.

- 9,041
- 2
- 28
- 37
-
-
It's not work. If I change included jade file, and want to build all dependencies, I got multiple refresh. – Serhiy Jul 11 '14 at 12:51
-
With the info I had, cannot know whats happening, can you post your complete gulpfile please ? – avcajaraville Jul 11 '14 at 13:02
-
var jadeTask = function(path) {
path = path || loc.jade + '/*.jade';
if (/source/.test(path)) {
path = loc.jade + '/**/*.jade';
}
return gulp.src(path)
.pipe(changed(loc.markup, {extension: '.html'}))
.pipe(jade({
locals : json_array,
pretty : true
}))
.pipe(gulp.dest(loc.markup))
.pipe(connect.reload());
}

- 736
- 6
- 10
First install required plugins
- gulp
- express
- gulp-jade
- connect-livereload
- tiny-lr
- connect
then write the code
var gulp = require('gulp');
var express = require('express');
var path = require('path');
var connect = require("connect");
var jade = require('gulp-jade');
var app = express();
gulp.task('express', function() {
app.use(require('connect-livereload')({port: 8002}));
app.use(express.static(path.join(__dirname, '/dist')));
app.listen(8000);
});
var tinylr;
gulp.task('livereload', function() {
tinylr = require('tiny-lr')();
tinylr.listen(8002);
});
function notifyLiveReload(event) {
var fileName = require('path').relative(__dirname, event.path);
tinylr.changed({
body: {
files: [fileName]
}
});
}
gulp.task('jade', function(){
gulp.src('src/*.jade')
.pipe(jade())
.pipe(gulp.dest('dist'))
});
gulp.task('watch', function() {
gulp.watch('dist/*.html', notifyLiveReload);
gulp.watch('src/*.jade', ['jade']);
});
gulp.task('default', ['livereload', 'express', 'watch', 'jade'], function() {
});
find the example here at GitHub

- 4,817
- 5
- 23
- 34