【问题标题】:How handle watchify errors in gulp. Watchify prevent error from being reported如何处理 gulp 中的 watchify 错误。 Watchify 防止错误被报告
【发布时间】:2018-04-26 04:33:08
【问题描述】:

当我在 browserify 捆绑器中包含 watchify 插件并且出现编译时错误时,不会记录该错误。

const gulp        = require('gulp');
const browserify  = require('browserify');            //js bundler
const watchify    = require('watchify');              //allows incremental bundling
const hbsfy       = require('hbsfy');                 //precompiles hbs files
const babelify    = require('babelify');              //ES2015 to ES5
const source      = require('vinyl-source-stream');   //Gulp and NodeJS stream interop. Use conventional text streams at the start of your gulp or vinyl pipelines, making for nicer interoperability with the existing npm stream ecosystem.
const buffer      = require('vinyl-buffer');          //Convert streaming vinyl files to use buffers. Usually used with vinyl-source-stream and gulp-sourcemaps
const uglify      = require('gulp-uglify');           //uglifyjs (js minimalizer) plugin for gulp (would be nice to use uglyfyjs directly)
const sourcemaps  = require('gulp-sourcemaps');       //generate source maps when tranforming js or css 
const gutil       = require('gulp-util');             //utlis for gulp, e.g. console logging

gulp.task("watch-js", function(done){
    var b = browserify({
        entries: 'main.js',
        debug: true,
        cache: {},
        packageCache: {},
      });
    b.external(config.vendorJsLibs);
    b.transform(hbsfy);
    b.transform(babelify, { presets: ['es2015'] });

    b.plugin(watchify); //when I comment this, errors are reported
    b.on('error', function(err){
       gutil.log(err.toString());
       this.emit('end');
       done();
    });
    compileJs(b, 'app.js');
    b.on('update', function(evt){
        gutil.log('watchify update '+ evt);
        compileJs(b, 'app.js');
    });    
});     

function compileJs(bundler, bundleFileName){
    return bundler.bundle()
        .pipe(source(bundleFileName))
        .pipe(buffer())
        .pipe(sourcemaps.init({loadMaps: true}))
            // Add transformation tasks to the pipeline here.
            .pipe(uglify())
            .on('error', function(error){
                gutil.log(error);
                this.emit('end');
            })
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest(dest + '/scripts'));
}

【问题讨论】:

  • 您的报价有问题。 entries: "main.js', 应该是 entries: 'main.js',。修复后错误是否仍然存在?
  • @MenzoWijmenga:修复谢谢。这是在这里写问题时的错字。所以是的,它仍然存在。为了清楚起见,您可以删除您的评论

标签: gulp browserify watchify


【解决方案1】:

您的包中没有任何错误处理程序,这就是节点崩溃并打印堆栈跟踪的原因。这是一种安全机制,可确保处理 error events 或中止执行。

监视 listens 以了解捆绑器上的错误事件以获取内部逻辑。这满足节点错误处理要求。

在您的代码中,您应该侦听b.bundle() 返回的流上的错误事件。以下代码应该可以解决问题:

function compileJs(bundler, bundleFileName){
    return bundler.bundle()
        .on('error', gutil.log)
        ...
}

【讨论】:

  • 它仍然听起来像一个错误或至少糟糕的设计导致意外行为。 github.com/browserify/watchify/issues/350
  • 我已经回复了你的 github 问题。在 watchify 的范围内,这种行为并不意外。错误转发是一个复杂的问题。更多信息可以在这里找到:stackoverflow.com/questions/21771220/…
  • 这并没有解释为什么它与 watchify 和没有它的工作方式不同。为什么 babelify 插件不会改变行为而 watchify 会。如果行为发生此类变化,则至少应在文档中进行说明
  • 我知道你来自哪里。我已经更新了文档,添加了一个 PR (github.com/browserify/watchify/pull/351) 来监视。
猜你喜欢
  • 1970-01-01
  • 2014-03-28
  • 1970-01-01
  • 2016-09-14
  • 2016-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-31
相关资源
最近更新 更多