Prevent errors from breaking / crashing gulp watch

后端 未结 8 886
我在风中等你
我在风中等你 2020-12-12 09:17

I\'m running gulp 3.6.2 and have the following task that was set up from a sample online

gulp.task(\'watch\', [\'default\'], function () {
  gulp.watch([
            


        
8条回答
  •  暖寄归人
    2020-12-12 09:37

    Typescript

    This is what worked for me. I work with Typescript and separated the function (to aovid confusion with this keyword) to handle less. This works with Javascript as well.

    var gulp = require('gulp');
    var less = require('gulp-less');
    
    gulp.task('less', function() {
        // writing a function to avoid confusion of 'this'
        var l = less({});
        l.on('error', function(err) {
            // *****
            // Handle the error as you like
            // *****
            l.emit('end');
        });
    
        return gulp
            .src('path/to/.less')
            .pipe(l)
            .pipe(gulp.dest('path/to/css/output/dir'))
    })
    

    Now, when you watch .less files, and an error occurs, the watch will not stop and new changes will processed as per your less task.

    NOTE : I tried with l.end();; however, it did not work. However, l.emit('end'); totally works.

    Hope this help. Good Luck.

提交回复
热议问题