Typescript: Watchify doesn't report TypeScript errors

Created on 26 Jan 2017  路  6Comments  路  Source: microsoft/TypeScript

I was following the TypeScript tutorial on setting up Gulp with Watchify.
Watchify does properly kick off compilation on each code change.
However, whenever it's used, the TypeScript compilation errors are not printed to the console.
If there are errors, the compilation seems to fail silently.
(Note: the error was added on purpose into the greet.ts example code)

Output when running WITHOUT Watchify:

[10:25:11] Starting 'copy-html'...
[10:25:11] Finished 'copy-html' after 17 ms
[10:25:11] Starting 'default'...
events.js:141
      throw er; // Unhandled 'error' event
      ^
TypeScript error: src/greet.ts(2,12): Error TS1005: ';' expected.

Output when running WITH Watchify:

[02:33:45] Starting 'copy-html'...
[02:33:45] Finished 'copy-html' after 19 ms
[02:33:45] Starting 'default'...

I'm referring to this gulpfile.js from the TypeScript tutorials:

var gulp = require("gulp");
var browserify = require("browserify");
var source = require('vinyl-source-stream');
var watchify = require("watchify");
var tsify = require("tsify");
var gutil = require("gulp-util");
var paths = {
    pages: ['src/*.html']
};

var watchedBrowserify = watchify(browserify({
    basedir: '.',
    debug: true,
    entries: ['src/main.ts'],
    cache: {},
    packageCache: {}
}).plugin(tsify));

gulp.task("copy-html", function () {
    return gulp.src(paths.pages)
        .pipe(gulp.dest("dist"));
});

function bundle() {
    return watchedBrowserify
        .bundle()
        .pipe(source('bundle.js'))
        .pipe(gulp.dest("dist"));
}

gulp.task("default", ["copy-html"], bundle);
watchedBrowserify.on("update", bundle);
watchedBrowserify.on("log", gutil.log);
Docs

Most helpful comment

Yes, that worked! Thanks a lot.

return watchedBrowserify
    .bundle()
    .on("error", gutil.log)
    ...

All 6 comments

@sandersn and @DanielRosenwasser any recommendations here?

@branko-boston when you say that compilation fails silently, does watchify fail to produce an updated bundle when there are compilation errors? Or does it just fail to report the errors?

@branko-boston Have you tried listening to the error event yourself? Currently you're trying to rely on the process crashing because of an "Unhandled error" (which occurs when no one is watching the error event), but internally watchify is probably listening to those error events.

@sandersn : Yes, watchify does create a new bundle.js, just the errors are not reported anywhere.

@blakeembrey : I did try this, but it didn't work:

watchedBrowserify.on("error", gutil.log);

Try listening to the result of .bundle(), which would be the stream. Otherwise, if you have an example project someone can look at it'd be helpful or I can quickly put one together to test myself.

Yes, that worked! Thanks a lot.

return watchedBrowserify
    .bundle()
    .on("error", gutil.log)
    ...
Was this page helpful?
0 / 5 - 0 ratings