Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp.js - watch and compile swig.js template files on change

I'm stucked with the problem of watching my Swig.js html templates and compiling them on every change, I use Gulp.js task runner for that purpose. Here is my gulpfile.js:

var gulp = require('gulp');
var myth = require('gulp-myth');
var swig = require('gulp-swig');
var nodemon = require('gulp-nodemon');

gulp.task('styles', function () {
  gulp.src('./myth/*.css')
    .pipe(myth())
    .pipe(gulp.dest('./client/css'));
});

gulp.task('templates', function () {
  gulp.src('./views/*.html')
    .pipe(swig({ load_json: true }))
    .pipe(gulp.dest('./client'));
});

gulp.task('server', function () {
  gulp.run('styles');
  gulp.run('templates');

  gulp.src('./server.js').pipe(nodemon());

  gulp.watch('./myth/*.css', function () {
     gulp.run('styles');
  });

  gulp.watch('./views/*.html', function () {
     gulp.run('templates');
  }); 
});

Myth task styles runs fine - on every change of myth/*.css I got updated file in client/css/..

But task called templates gives result only once.. When I change views/index.html file in client/index.html stays the same. In my console I see that templates task was ran but results still the same..

MacBook-Pro-Dmitri:slick-grid dmitri$ gulp server
[gulp] Using file /Users/dmitri/github/slick-grid/gulpfile.js
[gulp] Working directory changed to /Users/dmitri/github/slick-grid
[gulp] Running 'server'...
[gulp] Running 'styles'...
[gulp] Finished 'styles' in 4.02 ms
[gulp] Running 'templates'...
[gulp] Finished 'templates' in 1.29 ms
[gulp] Running 'watch'...
[gulp] Finished 'watch' in 7.23 ms
[gulp] Finished 'server' in 16 ms
>> Strata web server version 0.20.1 running on node 0.10.21
>> Listening on 0.0.0.0:9000, CTRL+C to stop

// Here I changed my views/index.html, task is ran as you see (but dest file won't be updated):

[gulp] Running 'templates'...
[gulp] Finished 'templates' in 668 μs 
127.0.0.1 - - [11/Jan/2014:15:43:20 +-200] "GET / HTTP/1.1" 200 511
127.0.0.1 - - [11/Jan/2014:15:43:20 +-200] "GET /css/reset.css HTTP/1.1" 200 1419
127.0.0.1 - - [11/Jan/2014:15:43:20 +-200] "GET /css/app.css HTTP/1.1" 200 865
127.0.0.1 - - [11/Jan/2014:15:43:20 +-200] "GET /fonts/likeastore-icon-font.ttf HTTP/1.1" 200 15052

Maybe I'm missing some cache options or similar?!

like image 764
Kosmetika Avatar asked Dec 07 '22 03:12

Kosmetika


2 Answers

Gulp-Swig has been updated to allow disabling of the cache option - as well as setting other swig defaults, as shown below.

gulp.task('templates', function() {
  gulp.src('./views/*.html')
    .pipe(swig({
      load_json: true,
      defaults: {
        cache: false,
        locals: {
          site_name: "My Blog"
        }
      }
    }))
    .pipe(gulp.dest('./client'));
});
like image 52
Colyn Brown Avatar answered Jan 21 '23 03:01

Colyn Brown


Edit: Configuration may no longer work as per Mike Aski's comment

I had a similar issue where gulp-livereload and tiny-lr was refreshing the page but not showing an updated template. I'm not using gulp-swig (although that looks like a good solution), so I just changed app.js:

swig = require("swig");

swig.setDefaults({
  cache: false
});

app = express();

app.set('view cache', false);
like image 45
kwcto Avatar answered Jan 21 '23 04:01

kwcto