Browserify错误结束gulp watch任务

时间:2022-08-10 17:08:47

I've got the following setup in my gulpfile.js:

我在gulpfile.js中有以下设置:

gulp.task('browserify', function() {
    browserify(config.paths.browserifyEntry)
        .transform(reactify)
        .bundle()
        .pipe(source('master.js'))
        .pipe(gulp.dest(config.paths.dist))
        //.pipe(connect.reload());
});

gulp.task('watch', function () {
    gulp.watch(config.paths.components, ['browserify']);
    gulp.watch(config.paths.sassSource, ['sass']);
});

This all works perfectly until I'm writing code that results in a browserify error. This happens frequently as I'm editing code in one file that is dependent on a change I haven't yet made in another file, browserify errors out.

这一切都完美无缺,直到我编写的代码导致浏览器出错。这种情况经常发生,因为我在一个文件中编辑代码,这取决于我在另一个文件中尚未进行的更改,浏览器出错。

The problem is that when browserify errors out it ends the watch task. I would expect that when browserify errors, it simply doesn't finish it's task but that the watch would continue so that when I make other changes that would allow browserify to run successfully it will do so. It is problematic to be making changes and then to reload the browser and find that it's not working only to find that the watch process ended due to a browserify error when your code was in an erroneous state.

问题是当浏览器出错时,它结束了监视任务。我希望当浏览器出错时,它根本没有完成它的任务,但是手表会继续,以便当我做出其他允许browserify成功运行的更改时,它会这样做。有问题的是进行更改然后重新加载浏览器并发现当代码处于错误状态时,由于浏览器化错误而导致监视进程结束时,它不起作用。

Is there a way to swallow that error so that it doesn't stop the watch process? Or is there another npm package that watches files and doesn't get tripped up by the browserify error?

有没有办法吞下这个错误,这样它就不会停止监视过程?或者是否有另一个npm包来监视文件并且不会因browserify错误而被绊倒?

2 个解决方案

#1


8  

If you don't handle the error event, the stream will throw. You could handle the event directly:

如果您不处理错误事件,则流将抛出。您可以直接处理该事件:

gulp.task('browserify', function() {
    browserify(config.paths.browserifyEntry)
        .transform(reactify)
        .bundle()
        .on('error', console.error.bind(console))
        .pipe(source('master.js'))
        .pipe(gulp.dest(config.paths.dist));
});

Or you could catch the error after it throws:

或者你可以在它抛出后捕获错误:

process.on('uncaughtException', console.error.bind(console));

#2


0  

For Browserify streams you need to call emit in error handler:

对于Browserify流,您需要在错误处理程序中调用emit:

gulp.task('browserify', function() {
  browserify(config.paths.browserifyEntry)
  .transform(reactify)
  .bundle()
  .on('error', function (error) {
    console.error(error.toString())
    this.emit('end')
  })
  .pipe(source('master.js'))
  .pipe(gulp.dest(config.paths.dist));
});

#1


8  

If you don't handle the error event, the stream will throw. You could handle the event directly:

如果您不处理错误事件,则流将抛出。您可以直接处理该事件:

gulp.task('browserify', function() {
    browserify(config.paths.browserifyEntry)
        .transform(reactify)
        .bundle()
        .on('error', console.error.bind(console))
        .pipe(source('master.js'))
        .pipe(gulp.dest(config.paths.dist));
});

Or you could catch the error after it throws:

或者你可以在它抛出后捕获错误:

process.on('uncaughtException', console.error.bind(console));

#2


0  

For Browserify streams you need to call emit in error handler:

对于Browserify流,您需要在错误处理程序中调用emit:

gulp.task('browserify', function() {
  browserify(config.paths.browserifyEntry)
  .transform(reactify)
  .bundle()
  .on('error', function (error) {
    console.error(error.toString())
    this.emit('end')
  })
  .pipe(source('master.js'))
  .pipe(gulp.dest(config.paths.dist));
});