Gulp.js stops compiling LESS when watched after there has been an error in the LESS files

前端 未结 4 1490
孤城傲影
孤城傲影 2021-02-08 02:06

I\'m having a problem with gulp. I run gulp-watch along with gulp-less and gulp-clean. Everything is running perfectly.

相关标签:
4条回答
  • 2021-02-08 02:24

    I always use gulp-plumber for my error catching. Works really easily and logs the error to the console.

    Example:

    gulp.task('less', function() {
        return gulp.src(['src/main/webapp/styles/main.less'], {base: 'src/main/webapp/styles/'})
        .pipe(plumber())
        .pipe(less().on('error', gutil.log))
        .pipe(gulp.dest('src/main/webapp/styles/build'))
        .on('error', function(err) {
            gutil.log(err);
            this.emit('end');
        });
    });
    
    0 讨论(0)
  • 2021-02-08 02:36

    It works now! Here is my final, and working, gulp-less task:

    gulp.task('less', function() {
        return gulp.src(['src/main/webapp/styles/main.less'], {base: 'src/main/webapp/styles/'})
        .pipe(less().on('error', function(err){
            gutil.log(err);
            this.emit('end');
        }))
        .pipe(gulp.dest('src/main/webapp/styles/build'))
    });
    

    The problem was that, when there was an error in the LESS the task still went on and built the destination file. On top of that I placed the error logging function and the emit('end') as a callback to gulp.dest.

    Now, when the callback of less() is the error log and emit('end') everything works perfectly.

    0 讨论(0)
  • 2021-02-08 02:38

    I have just set this up for my own personal project. According to the Gulp docs, you can just use gulp.watch:

    gulp.task('watch', function() {
        gulp.watch('src/main/webapp/styles/**/*.{less, css}', ['less'])
            .on('error', gutil.log);
    });
    

    EDIT: If this doesn't help, change your less task to:

    gulp.task('less', function() {
        return gulp.src(['src/main/webapp/styles/main.less'], {base: 'src/main/webapp/styles/'})
        .pipe(less())
        .on('error', function (err) {
            gutil.log(err);
            this.emit('end');
        })
        .pipe(gulp.dest('src/main/webapp/styles/build'))
        .on('error', gutil.log);
    });
    

    Adapted from this comment.

    0 讨论(0)
  • 2021-02-08 02:39

    the best solution I have tried; https://github.com/gulpjs/gulp/blob/master/docs/recipes/combining-streams-to-handle-errors.md

    var combiner = require('stream-combiner2');
    gulp.task('multi:less', function(done) {
        var combined = combiner.obj([
            gulp.src(srcs),
            less(),
            autoprefixer({
                browsers: ['last 6 versions'],
                cascade: false
            }),
            isDev ? null : cleanCss(),
            gulp.dest(targetDir + 'css/multi/'),
        ].filter(v => v));
    
        // any errors in the above streams will get caught
        // by this listener, instead of being thrown:
        combined.on('error', console.error.bind(console));
        combined.on('end', () => {}); //done have been call when return combined;
        return combined;
    
    }
    
    0 讨论(0)
提交回复
热议问题