I\'m having a problem with gulp
. I run gulp-watch
along with gulp-less
and gulp-clean
. Everything is running perfectly.
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');
});
});
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.
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.
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;
}