I have a simple gulpfile.js, that defines only two tasks, buildLess and watchFiles:
var gulp = require('gulp');
var less = require('gulp-less');
var watch = require('gulp-watch');
var plumber = require('gulp-plumber');
var filter = require('gulp-filter');
function buildLess(done) {
const fileFilter = filter(['**/*', '!**/mixins.less', '!**/variables.less']);
gulp.src('./public/less/*.less')
.pipe(fileFilter)
.pipe(plumber())
.pipe(less())
.pipe(gulp.dest('./public/css/'))
;
done();
};
function watchFiles() {
gulp.watch(['public/less/*.less'], gulp.series('build-less'));
// gulp.watch(['./public/less/*.less'], gulp.series(buildLess));
};
gulp.task('build-less', buildLess);
gulp.task('watch-files', watchFiles);
The first one ($ gulp build-less) is working fine. The watchFiles ($ gulp watch-files) can be started and doesn't cause any errors, but changes on the public/less/style.less are ignored.
What is wrong at this gulpfile.js and how to get the watch-files task working?
The
gulp.seriesAPI allows you to pass a string of a previously registered task. In your code, you haven't registeredbuild-lessyet.I would note that Gulp does not recommend using the
gulp.taskAPI anymore to register tasks, but instead to useexports.Secondly, you don't need
gulp-watch, as gulp now comes with its owngulp.watchmethod (which you are already using).Lastly, you should make sure to your correctly signaling async completion in your
buildLessfunction. Below, I've changed that function to return a Stream, rather than calling adone()callback since as you have it written, you have a race condition wheredone()may be called before the Less compilation has finished.Overall, I'd go through Gulp's documentation. They recently updated their website, and updated their documentation along with it. Going through that might clear up some other questions you may be having.