Gulp Sass with errLogToConsole: true still stoppin

2019-01-26 19:06发布

问题:

I currently have 3 watch tasks as so:

gulp.task('watch', function(){
    gulp.watch(sassDir + '/*.scss', ['sass']);
    gulp.watch(cssDir + '/*.css', ['css']);
    gulp.watch(jsDir + '/*.js', ['js']);
})

My issue right now is that when Sass throws an error all watch tasks stops. I then added .pipe(sass({errLogToConsole: true})) - which seems to keep my sass watch task alive even with an error, however the two other watch tasks doesnt seem to run.

How do I keep all of my watch tasks a live, so that an error in my sass file doesn't stop everything from being compiled?

My setup:

gulp.task('sass', function () {
    gulp.src(sassDir + '/**/*.scss')
        .pipe(sass({errLogToConsole: true}))
        .pipe(gulp.dest(sassTargetDir));
});

gulp.task('css', function() {
  gulp.src(cssDir + '/**/*.css')
    .pipe(autoprefix({
            browsers: ['last 40 versions'],
            cascade: false
        }))
    .pipe(gulp.dest(cssTargetDir))
    .pipe(minify())
    .pipe(rename({suffix: '.min'}))
    .pipe(gulp.dest(cssTargetDir));
});

gulp.task('js', function() {
  gulp.src(jsDir + '/**/*.js')
    .pipe(gulp.dest(jsTargetDir))
    .pipe(uglify())
    .pipe(rename({suffix: '.min'}))
    .pipe(gulp.dest(jsTargetDir))
});

gulp.task('watch', function(){
    gulp.watch(sassDir + '/*.scss', ['sass']);
    gulp.watch(cssDir + '/*.css', ['css']);
    gulp.watch(jsDir + '/*.js', ['js']);
})

gulp.task('default', ['sass','css','js','watch']);

回答1:

errLogToConsole is deprecated (or doesn't work) with Gulp 2.0+. Try using this in place of .pipe(sass({errLogToConsole: true})).

.pipe(sass().on('error', sass.logError))

I use that with the watch command and the errors don't stop Gulp.

I got this from the Gulp sample code and a few more up-to-date Gulp guides. Check here in the future. https://github.com/dlmanning/gulp-sass