0

I've used Gulp a number of times but this problem has me totally stumped. I have a render task to save an HTML template piped through gulp-swig. It works perfectly fine when invoked manually via gulp render but will not save when the task is triggered by gulp.watch. So, to clarify, the watcher does trigger the task but the rendered file doesn't actually get saved to its destination.

I've boiled the problem down to the following, removing all other tasks from my gulpfile, axed plumber, context data, etc. Any help appreciated!

var gulp        = require( 'gulp' ),
    browserSync = require( 'browser-sync' ).create(),
    rename      = require( 'gulp-rename' ),
    swig        = require( 'gulp-swig' ),
    reload      = browserSync.reload;

gulp.task( 'render', function() {
    return gulp.src( './dev/template.html' )
        .pipe( swig() )
        .pipe( rename( 'index.html' ) )
        .pipe( gulp.dest( './preview' ) )
        .on( 'end', reload );
});

gulp.task( 'serve', function() {
    browserSync.init( { server: './preview' } );

    gulp.watch( './dev/*.html',  [ 'render' ] );
});

gulp.task( 'default', [ 'serve' ] );
pete
  • 2,739
  • 4
  • 32
  • 46

1 Answers1

1

Duh, got it working. It appears that swig caches the template by default (?) so it was in fact saving the file but it was identical. I noticed a 'cache' option in another (unrelated) post about swig which did the trick. So, I needed to pass in an options object, like so:

        .pipe( swig({ 
            defaults: { cache: false }
        }) )
pete
  • 2,739
  • 4
  • 32
  • 46