shannonmoeller / gulp-hb

A sane Gulp plugin to compile Handlebars templates. Useful as a static site generator.
http://npm.im/gulp-hb
MIT License
147 stars 14 forks source link

Add option to continue streaming on error #12

Closed backflip closed 9 years ago

backflip commented 9 years ago

Currently, the stream ends when an error occurs. This is undesirable when having a watch process running, e.g.

A solution is to use the failures option of map-stream which was added for this case: https://github.com/dominictarr/map-stream/pull/7

What do you think?

coveralls commented 9 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling 0e7d705fc913306c4a0f4d3e15d95f6f3dfb46e3 on unic:master into 628b570339bd68bb02be429ba08b9888dfd20cbb on shannonmoeller:master.

shannonmoeller commented 9 years ago

I've actually been working on replacing map-stream with through2.obj. That change plus gulp-plumber provides a Gulp 3 solution until Gulp 4 (and its improved error handling) is released.

Does that sound reasonable?

backflip commented 9 years ago

Even better. Any helping hands appreciated/wanted? Or are you almost through with it anyway?

shannonmoeller commented 9 years ago

Thanks! I've got the switch to through2 made, but I want to get a fix for #15 in before release, as you'll likely need that too.

shannonmoeller commented 9 years ago

Released switch to through2 as v2.4.0.

backflip commented 9 years ago

This still crashes when throwing an error. I guess instead of calling the template function directly, it should be wrapped with a try/catch:

try {
    file.contents = new Buffer(template(context));
} catch (err) {
    this.emit('error', err);
}
shannonmoeller commented 9 years ago

@backflip I'll get the error handling updated, but even so, you'll need to use gulp-plumber until Gulp 4 is released. There was a patch to make gulp.watch in Gulp 3 work a little better, but it's still broken.

var gulp = require('gulp'),
    plumber = require('gulp-plumber'),
    hb = require('gulp-hb');

gulp.task('html', function () {
    return gulp
        .src('./src/**/*.hbs')
        .pipe(plumber())
        .pipe(hb())
        .pipe(gulp.dest('./web'));
});

gulp.task('watch', function () {
    gulp.watch('./src/**/*.hbs', ['html']);
});
shannonmoeller commented 9 years ago

Improved error handling published as v2.4.2

backflip commented 9 years ago

Perfect, thanks!