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

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

问题描述

我使用gulp + browserify来构建和打包我的JavaScript库。现在有一件事情让我困扰:我正在运行一个简单的服务器,通过grep进行livereload开发。这工作正常,但只要我的JavaScript包含语法错误,browserify会引发错误,导致服务器停止。



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

  browserify(./ src / main.js)
.bundle({standalone :SomeName,debug:false})。on('error',notify.onError({
message:Error:<%= error.message%>,
title:无法运行browserify
})
);

现在出现一个有趣的部分:
当我删除独立设置(并且我的js是语法错误),错误处理程序触发。但是,当我使用这个独立设置时,错误处理程序不会触发(导致服务器由gulp停止启动)。

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

解决方案 on('error')事件仍然被触发。但是,bro​​wserify流与其他Gulp流有点不同。在browserify的错误处理函数中,您需要显式调用 this.emit(end)



示例gulp任务

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

gulp.task('browserify',function(){
//创建新包
var b = browserify();
//添加变换想要
b.transform(reactify);
//将文件添加到browserify
b.add(./ src / main.js);
//开始捆绑
返回b.bundle()
.on('error',function(err){
//打印错误(可以用gulp-util替换)
console.log err.message);
//结束此流
this.emit('end');
})
.pipe(source('main.js'))
//在此处输入其他插件,如果您需要
.pipe(gulp.dest('./ dist'));
});

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



有关更多信息,请阅读此处 http://truongtx.me/2014/07/15/handle-errors-while-using-gulp-watch/


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.

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"
    })
);

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)

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

解决方案

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")

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'));
});

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.

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

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

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