0

When I include watchify plugin in a browserify bundler and there is an compile time error, the error is not logged.

const gulp        = require('gulp');
const browserify  = require('browserify');            //js bundler
const watchify    = require('watchify');              //allows incremental bundling
const hbsfy       = require('hbsfy');                 //precompiles hbs files
const babelify    = require('babelify');              //ES2015 to ES5
const source      = require('vinyl-source-stream');   //Gulp and NodeJS stream interop. Use conventional text streams at the start of your gulp or vinyl pipelines, making for nicer interoperability with the existing npm stream ecosystem.
const buffer      = require('vinyl-buffer');          //Convert streaming vinyl files to use buffers. Usually used with vinyl-source-stream and gulp-sourcemaps
const uglify      = require('gulp-uglify');           //uglifyjs (js minimalizer) plugin for gulp (would be nice to use uglyfyjs directly)
const sourcemaps  = require('gulp-sourcemaps');       //generate source maps when tranforming js or css 
const gutil       = require('gulp-util');             //utlis for gulp, e.g. console logging

gulp.task("watch-js", function(done){
    var b = browserify({
        entries: 'main.js',
        debug: true,
        cache: {},
        packageCache: {},
      });
    b.external(config.vendorJsLibs);
    b.transform(hbsfy);
    b.transform(babelify, { presets: ['es2015'] });

    b.plugin(watchify); //when I comment this, errors are reported
    b.on('error', function(err){
       gutil.log(err.toString());
       this.emit('end');
       done();
    });
    compileJs(b, 'app.js');
    b.on('update', function(evt){
        gutil.log('watchify update '+ evt);
        compileJs(b, 'app.js');
    });    
});     

function compileJs(bundler, bundleFileName){
    return bundler.bundle()
        .pipe(source(bundleFileName))
        .pipe(buffer())
        .pipe(sourcemaps.init({loadMaps: true}))
            // Add transformation tasks to the pipeline here.
            .pipe(uglify())
            .on('error', function(error){
                gutil.log(error);
                this.emit('end');
            })
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest(dest + '/scripts'));
}
Liero
  • 25,216
  • 29
  • 151
  • 297
  • There's an issue with your quotes. `entries: "main.js',` should be `entries: 'main.js',`. Does the error still persist after fixing that? – Menzo Wijmenga Nov 13 '17 at 13:19
  • @MenzoWijmenga: fixed thanks. this was typo when writing question here. So yes, it persists. You may remove you comment for clarity – Liero Nov 13 '17 at 13:45

1 Answers1

0

You don't have any error handlers on your bundle, that's why node crashes and prints a stack trace. This is a safety mechanism that ensures error events are either handled or execution is aborted.

Watchify listens to error events on bundler for internal logic. This satisfies nodes error handling requirement.

In your code you should listen for error events on the stream returned by b.bundle(). The following code should do the trick:

function compileJs(bundler, bundleFileName){
    return bundler.bundle()
        .on('error', gutil.log)
        ...
}
Menzo Wijmenga
  • 1,011
  • 6
  • 13
  • It stills sounds like a bug or at least poor design resulting in unexpected behavior. https://github.com/browserify/watchify/issues/350 – Liero Nov 15 '17 at 16:11
  • I've replied to your github issue. Within the scope of watchify this behaviour is not unexpected. Error forwarding is a complicated issue. More info can be found here: https://stackoverflow.com/questions/21771220/error-handling-with-node-js-streams?answertab=votes#tab-top – Menzo Wijmenga Nov 15 '17 at 16:42
  • This does not explain, why it works differently with watchify and without it. Why babelify plugin does not change the behavior and watchify does. If there is such change in behavior it should be explained in the documentation at least – Liero Nov 15 '17 at 21:12
  • 1
    I see where you're coming from. I've updated the docs added a PR (https://github.com/browserify/watchify/pull/351) to watchify. – Menzo Wijmenga Nov 17 '17 at 22:57