Search code examples
javascriptangularjsangularjs-directivegulpgulp-watch

Why watch task terminate in gulp already use gulp-plumber plugin?


I try to use this plugin in gulp (gulp-plumber).I studied about this plugin that it gives you error but it watching state not stop.In other words it gives your file error on command line.But it not stop watching . https://www.npmjs.com/package/gulp-plumber

But when I changed in sass file try to create error on saas file ,then it stop watch then Again i need to give command why ?

here id my gulp file.

var gulp = require('gulp');
//require sass package
var sass = require('gulp-sass');
var rename = require('gulp-rename');
var minifyCss = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');
var plumber = require('gulp-plumber');




// Compile Our Sass
gulp.task('sass', function () {
    return gulp.src('scss/*.scss')
        .pipe(sass())
        .pipe(plumber())
        .pipe(gulp.dest('css'));
});
// css miifilcation
gulp.task('minify-css',['sass'], function () {
    return gulp.src('css/*.css')
        .pipe(minifyCss())
        .pipe(plumber())
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest('dist'));
});

// js minifiction
gulp.task('compress', function() {
    return gulp.src('app/**/*.js')
        .pipe(uglify())
        .pipe(gulp.dest('jdist'));
});

//concatination of js files

gulp.task('scripts', function() {
    return gulp.src('jdist/**/*.js')
        .pipe(concat('all.js'))
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest('dist/'));
});


// Watch Files For Changes
gulp.task('watch', function () {
    gulp.watch('scss/*.scss', ['sass','minify-css']);
});

gulp.task('default', ['sass', 'watch', 'minify-css','scripts']);

these are following steps I did.

  • Write gulp in command line
  • change in sass file .remove one '{ in saas file then it give the error and terminate why ?

here is my log

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

D:\Webapplication\DemoApp>gulp
[09:11:47] Using gulpfile D:\Webapplication\DemoApp\gulpfile.js
[09:11:47] Starting 'sass'...
[09:11:47] Starting 'watch'...
[09:11:47] Finished 'watch' after 9.13 ms
[09:11:47] Starting 'scripts'...
[09:11:47] Finished 'sass' after 53 ms
[09:11:47] Starting 'minify-css'...
[09:11:47] Finished 'minify-css' after 27 ms
[09:11:47] Finished 'scripts' after 61 ms
[09:11:47] Starting 'default'...
[09:11:47] Finished 'default' after 7.5 µs
[09:13:23] Starting 'sass'...

events.js:141
      throw er; // Unhandled 'error' event
      ^
 Error: scss\home.scss
Error: Invalid CSS after "}": expected "}", was ""
        on line 28 of stdin
>> }
   ^

    at options.error (D:\Webapplication\DemoApp\node_modules\gulp-sass\node_modules\node-sass\lib\index.js:277:32)

D:\Webapplication\DemoApp>

update Question after answer.I apply your answer .it is not terminating but after it is also not watching way?

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

D:\Webapplication\DemoApp>gulp
[10:43:28] Using gulpfile D:\Webapplication\DemoApp\gulpfile.js
[10:43:28] Starting 'watch'...
[10:43:28] Finished 'watch' after 20 ms
[10:43:28] Starting 'default'...
[10:43:28] Finished 'default' after 7.11 µs
[10:43:42] Starting 'sass'...
[10:43:42] Finished 'sass' after 32 ms
[10:43:42] Starting 'minify-css'...
[10:43:42] Finished 'minify-css' after 30 ms
[10:43:49] Starting 'sass'...
[10:43:49] Plumber found unhandled error:
 Error in plugin 'gulp-sass'
Message:
    scss\home.scss
Error: Invalid CSS after "}": expected "}", was ""
        on line 28 of stdin
>> }
   ^

Details:
    formatted: Error: Invalid CSS after "}": expected "}", was ""
        on line 28 of stdin
>> }
   ^

    column: 1
    line: 28
    file: stdin
    status: 1
    messageFormatted: scss\home.scss
Error: Invalid CSS after "}": expected "}", was ""
        on line 28 of stdin
>> }
   ^

Solution

  • Try swaping sass and plubmber in your sass task as it is shown in the example on gulp plumber page. Your task should look like this

    // Compile Our Sass
    gulp.task('sass', function () {
        return gulp.src('scss/*.scss')
            .pipe(plumber({
                  handleError: function (err) {
                                   console.log(err);
                                   this.emit('end');
                               }
             }))
            .pipe(sass())
            .pipe(gulp.dest('css'));
    });
    

    Update

    I took out the js tasks for simplicity of my answer. Now you are compiling your sass, minifying it and adding min to name in a single task.

    // SASS 
    gulp.task('sass', function () {
        return gulp.src('scss/*.scss')
            .pipe(plumber())
            .pipe(sass({outputStyle: 'compressed'}))
            .pipe(rename({suffix: '.min'}))
            .pipe(gulp.dest('dist'));
    });
    
    
    // Watch Files For Changes
    gulp.task('watch', function () {
        gulp.watch('scss/*.scss', ['sass']);
    });
    
    gulp.task('default', ['sass', 'watch']);