简体   繁体   English

Gulp Browserify中的标准错误日志

[英]Standard error log in Gulp Browserify

With this task: 有了这个任务:

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

I get this kind of error log: 我得到这种错误日志:

在此输入图像描述

It's clear and pretty, I like it. 它很清晰漂亮,我喜欢它。

But in order to keep my watch running, I need to handle the error instead of letting it pass, something like 但是为了让我的手表保持运行,我需要处理错误而不是让它通过,比如说

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

How can I reproduce the same error log here? 如何在此处重现相同的错误日志?

I'd prefer to avoid painfully reproducing it by combining chalk, gutil and reading the errorring file but to use the same function somehow. 我宁愿通过组合chalk,gutil和读取错误文件来避免痛苦地再现它,但是以某种方式使用相同的函数。

It turns out that browserify uses the syntax-error module and thus throws rich error objects containing a console-ready codeFrame property. 事实证明,browserify使用语法错误模块 ,因此会抛出包含控制台就绪codeFrame属性的丰富错误对象。

I can intercept the error like this: 我可以像这样截取错误:

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

where gutil is gulp-util 其中gutil是gulp -util

for this result: 为此结果:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM