2017-11-13 5 views
0

watchifyプラグインをbrowserify bundlerに組み込み、コンパイル時にエラーが発生すると、エラーは記録されません。どのようにして、痛みの中でエラーを処理するかを処理します。 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')); 
} 
+0

引用符に問題があります。 '' main.js '、 '' entries:' main.js ''でなければなりません。 –

+0

@MenzoWijmenga:固定されたおかげです。 – Liero

答えて

0

ノード上にエラーハンドラがないため、ノードがクラッシュしてスタックトレースが出力されます。これは、error eventsが処理されるか、実行が中止されることを保証する安全機構です。

listensを内部論理のためにbundlerのエラーイベントに監視します。これは、ノードのエラー処理要件を満たします。

コードでは、b.bundle()によって返されたストリームのエラーイベントをリスンする必要があります。次のコードはこのトリックを行う必要があります:

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

これはまだ不具合や予期しない動作を引き起こす不十分なデザインのようなものです。https://github.com/browserify/watchify/issues/350 – Liero

+0

あなたのgithubに返信しましたこの問題は予期せぬことではありません。エラー転送は複雑な問題です。詳細はこちらhttps://stackoverflow.com/questions/21771220/error-handling-with-node-js-streams ?answertab = votes#tab-top –

+0

これは、watchifyとは違って動作する理由と説明がない理由を説明していません。なぜbabelifyプラグインは動作を変更せず、watchifyしないのですか?少なくとも – Liero

関連する問題