使用babel和browserify +捕获错误的Gulp任务

时间:2015-04-24 08:42:53

标签: javascript gulp browserify babeljs

我的gulpfile:

var gulp = require('gulp');
var babel = require('gulp-babel');
var browserify = require('gulp-browserify');

gulp.task('precompile', function () {
  return gulp.src('app/assets/react/app.react.js')
    .pipe(babel())
    .pipe(gulp.dest('app/assets/javascripts/'))
});

gulp.task('browserify', ['precompile'], function () {
  return gulp.src(['app/assets/javascripts/app.react.js'])
    .pipe(browserify())
    .pipe(gulp.dest('app/assets/javascripts/'));
});

gulp.task('watch', function() {
  gulp.watch('app/assets/react/app.react.js', ['browserify']);
});

如果有可能,我该如何改进?也许我只能用browserify + transform(babelify)+ console.log来编写一个错误的任务? 此时,如果我有解析错误 - 吞咽看着坠毁。

1 个答案:

答案 0 :(得分:2)

尝试水管工

var plumber = require('gulp-plumber');

/**
 * Helper function to amend the pipe when a build task fails
 * see https://github.com/hughsk/vinyl-transform/issues/1
 * and: https://github.com/gulpjs/gulp/issues/259
 * @param {string} err  the error string
 */
function onError (err) {
    gutil.beep();
    console.log(err);
    this.emit('end');
}

return gulp.src('app/assets/react/app.react.js')
    .pipe(plumber({
            errorHandler: onError
        }))
    .pipe(babel());

gulp-browserify也被列入黑名单。您可以按照本教程进行更多的设置:https://medium.com/@sogko/gulp-browserify-the-gulp-y-way-bb359b3f9623