Search code examples
gulp-uglify

handle error in gulp with plumber and utils


using gulp with plumber module but stuck into the problem; below is my main gulpfile.js

var gulp = require('gulp');
var $ = require("gulp-load-plugins")({
  pattern: ['gulp-*', 'gulp.*', 'browserify**'],
  replaceString: /\bgulp[\-.]/
});

var src = './app/';
var dest = './env/';

gulp.task('scripts', function(){
    return gulp.src([src + '**/*.js'])
            .pipe($.plumber(function(error) {
                  // Output an error message
                  $.util.log($.util.colors.red('Error (' + error.plugin + '): ' + error.message));
                  // emit the end event, to properly end the task
                  this.emit('end');
             }))
            .pipe($.uglify().on('error', $.util.log))
            .pipe($.concat('vendor.min.js'))
            .pipe(gulp.dest(dest));
});

running gulp scripts gives error two times see as there is $.plumber() and $.uglify().on method; I want to show into one, so that error comes in handy way with red color in console. If i emit .on('error', $.util.log) than execution stops and crash the app.


Solution

  • it's like magic; most of the times whenever I ask questions on SO , I found the solution by myself within few trials.

    tried this and succeed

    var onError = function (err) {
        $.util.log($.util.colors.red('Error (' + err.plugin + '): ' + err.message));
    };
    

    and later

    gulp.task('scripts', function(){
            return gulp.src([src + '**/*.js'])
                    .pipe($.plumber())
                    .pipe($.uglify().on('error', onError))
                    .pipe($.plumber.stop())
                    .pipe($.concat('vendor.min.js'))
                    .pipe(gulp.dest(dest));
    });
    

    hope it helps someone