捕获 Browserify 解析错误(独立选项) [英] Catching Browserify parse error (standalone option)

查看:9
本文介绍了捕获 Browserify 解析错误(独立选项)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 gulp + browserify 来构建和打包我的 javascript 库.现在有一件事情困扰着我:我正在运行一个带有 livereload 的简单服务器,用于通过 gulp 进行开发.这很好用,但是每当我的 javascript 包含语法错误时,browserify 就会抛出一个错误,导致服务器停止.

I'm using gulp + browserify to build and package my javascript library. Now there is one thing that bothers me: I'm running a simple server with livereload for development via gulp. This works fine, but whenever my javascript contains a syntax error, browserify throws an error causing the server to stop.

我使用的 browserify 代码(注意我添加了一个错误处理程序):

The browserify code I use (note that I've added an error handler):

browserify("./src/main.js")
   .bundle({standalone: "SomeName", debug: false}).on('error', notify.onError({
            message: "Error: <%= error.message %>",
            title: "Failed running browserify"
    })
);

现在有趣的部分来了:当我删除独立设置(并且我的 js 在语法上不正确)时,错误处理程序会触发.但是,当我使用此独立设置时,错误处理程序不会触发(导致 gulp 停止启动的服务器)

Now comes the interesting part: When I remove the standalone setting (and my js is syntacticaly incorrect), the error handler fires. However, when I use this standalone setting, the error handler does not fire (resulting in the server launched by gulp stopping)

有人知道如何处理这个问题吗?我总是可以在 gulp 中手动验证我的 js 文件,但想避免这种解决方法

Does anybody know how to deal with this issue? I could always manually validate my js files in gulp, but would like to avoid this workaround

推荐答案

on('error') 事件仍然被触发.然而,browserify 流与其他 Gulp 流有点不同.在browserify的错误处理函数中,需要显式调用this.emit("end")

The on('error') event is still fired. However, browserify stream is a bit different from other Gulp stream. In the error handler function of browserify, you need to explicitly call this.emit("end")

一个 gulp 任务示例

An example gulp task

var browserify = require('browserify');
var gulp = require('gulp');
var source = require("vinyl-source-stream");
var reactify = require('reactify');

gulp.task('browserify', function(){
  // create new bundle
  var b = browserify();
  // add transform if you want
  b.transform(reactify);
  // add file to browserify
  b.add("./src/main.js");
  // start bundling
  return b.bundle()
    .on('error', function(err){
      // print the error (can replace with gulp-util)
      console.log(err.message);
      // end this stream
      this.emit('end');
    })
    .pipe(source('main.js'))
    // pipe other plugin here if you want
    .pipe(gulp.dest('./dist'));
});

错误函数处理程序防止 gulp 崩溃,this.emit("end") 停止当前流,不让它运行到下一个管道.事件处理程序还可以在转换插件中捕获错误.

the error function handler prevents gulp from crashing, this.emit("end") stops the current stream, not let it run to the next pipes. The event handler can also catch error in the transform plugin.

有关更多信息,您可以在此处阅读 http://truongtx.我/2014/07/15/handle-errors-while-using-gulp-watch/

For more information, you can read here http://truongtx.me/2014/07/15/handle-errors-while-using-gulp-watch/

这篇关于捕获 Browserify 解析错误(独立选项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆