Gulp-sass error with notify

核能气质少年 提交于 2019-12-02 20:54:49

After struggling with this myself I found that this worked:

gulp.task('styles', function() {
  return gulp.src('src/scss/style.scss')
    .pipe(sass({
        style: 'compressed',
        errLogToConsole: false,
        onError: function(err) {
            return notify().write(err);
        }
    }))
    .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
    .pipe(gulp.dest(''))
    .pipe(livereload(server))
    .pipe(notify({ message: 'Styles task complete' }));
});

You need to catch the error using the onError option that gulp-sass provides.

Hope that helps!

I'm a little late to the party here but the issue I was having was that the sass would stop compiling if there was an error in the code and I would have to restart gulp. Here is what I ended up doing:

gulp.task('sass', function() {
    return gulp.src('assets/scss/style.scss')
        .pipe(sass({ errLogToConsole: false, }))
        .on('error', function(err) {
            notify().write(err);
            this.emit('end');
        })
        .pipe(gulp.dest('assets/css'))
        .pipe(notify({ message: 'SCSS Compiled' }));
});

In my case I had to add this.emit('end');

With gulp-sass v2.0.4, this works:

.pipe(sass())
.on('error', notify.onError(function (error) {
   return 'An error occurred while compiling sass.\nLook in the console for details.\n' + error;
}))

This is works too:

/* Compile sass, and output error to notif */
    .pipe(sass({}).on('error', function(err) {
        return notify().write(err);
    }))

I think we should also print in the console and this code works in the current version of gulp-sass(4.0.1)

gulp.task('sass', function() {
    gulp.src('src/sass/style.scss')
    .pipe(sass({outputStyle: 'expanded'}).on('error',function(err) {
            sass.logError;  //I think we should also print in the console
            return notify().write(err); //and the notification bar
        }))
    .pipe(autoprefixer({browsers:autoprefixBrowsers}))
    .pipe(gulp.dest('dist/css'))
    .pipe(notify({message: 'sass compiled successfully'}));
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!