Gulp Browserify 中的标准错误日志

Standard error log in Gulp Browserify

完成此任务:

gulp.task("es6", function () {
      return browserify({entries: 'src/main/es6/main.js', extensions: ['.js'], debug: true})
      .transform(babelify)
      .bundle()
      .pipe(source('superpos.js'))
      .pipe(streamify(uglify()))
      .pipe(gulp.dest('src/main/webapp'));
});

我收到这种错误日志:

清晰漂亮,喜欢

但是为了保住手表运行,我需要处理错误而不是让它过去,比如

  ...
  .transform(babelify)
  .bundle()
  .on('error', function(error){
        // pretty error print
        this.emit('end');
  })
  ...

如何在此处重现相同的错误日志?

我宁愿通过组合 chalk、gutil 和读取错误文件来避免痛苦地重现它,而是以某种方式使用相同的功能。

事实证明,browserify 使用 syntax-error module 并因此抛出包含控制台就绪 codeFrame 属性.

的丰富错误对象

我可以这样拦截错误:

gulp.task("es6", function () {
      return browserify({entries: 'src/main/es6/main.js', extensions: ['.js'], debug: true})
      .transform(babelify)
      .bundle()
      .on('error', function(err){
            if (err instanceof SyntaxError) {
                  gutil.log(gutil.colors.red('Syntax Error'));
                  console.log(err.message);
                  // console.log(err.filename+":"+err.loc.line);
                  console.log(err.codeFrame);
            } else {
                  gutil.log(gutil.colors.red('Error'), err.message);
            }
            this.emit('end');
      })
      .pipe(source('superpos.js'))
      .pipe(streamify(uglify()))
      .pipe(gulp.dest('src/main/webapp'));
});

gutil 是 gulp-util

对于这个结果: