I just upgraded to Gulp v4 and was wondering why my gulpfile isn't working anymore.
I tried the new code of the new documentation but it didn't worked out as planned because I'm using "pure" postcss.
So I googled for my issue and found this question: Gulp error: watch task has to be a function
However, this also wasnt a solution for my problem, although I get the same error message Error: watching src/styles/**/*.scss: watch task has to be a function
I currently have this code
var gulp = require('gulp');
var sass = require('gulp-sass');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var cssnano = require('cssnano');
gulp.task('default', function () {
gulp.watch('src/styles/**/*.scss', ['styles']);
});
gulp.task('styles', function () {
var processors = [
autoprefixer({
browsers: ['last 3 versions', 'ie > 9']
}),
cssnano()
];
gulp.src('src/styles/main.scss')
.pipe(sass().on('error', sass.logError))
.pipe(postcss(processors))
.pipe(gulp.dest('dist/files/styles/'));
});
and when I changegulp.watch('src/styles/**/*.scss', ['styles']);
togulp.watch('src/styles/**/*.scss', gulp.series('styles'));
it just gives me a Starting 'default'...
and after changing a file Starting 'styles'...
with Gulp 3.9 it was
Starting 'default'...
Finished 'default' after 174 ms
and after changing a file
Starting 'styles'...
Finished 'styles' after 561 μs
I've now tried many different things but I just dont get it to work like it did before. I'm really thinking of switching over to webpack like the cool kids now. But Gulp always worked fine.
Can someone explain to me what I'm doing wrong?
The Watch method is used to monitor your source files. When any changes to the source file is made, the watch will run an appropriate task. You can use the ‘default’ task to watch for changes to HTML, CSS, and JavaScript files. In the previous chapter you have learnt how to gulp combining tasks using default task.
You can use the ‘default’ task to watch for changes to HTML, CSS, and JavaScript files. In the previous chapter you have learnt how to gulp combining tasks using default task. We used gulp-minify-css, gulp-autoprefixer and gulp-concatplugins, and created styles task to minify CSS files.
and then you can watch by just running gulp in CLI (which causes it to run gulp default, which in turn runs gulp watch, which watches for any changes in any scss files in the given directory and runs sass task for every change) Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.
Now that Gulp v4 has been out for nearly over a year (released at the end of 2017) it offers some great ways to sequence tasks using their library called bach. Note: I would recommend moving to Gulp v4 since v3 had some security vulnerabilities [I'm not sure if those have been patched now].
Just in case anyone else stumbles on this (like I did), trying to solve Gulp not working after upgrading, I found there were really only two things that mattered for me (after trying hundreds) -
gulp.task('js', (done) => {
gulp.src(src)
.pipe(task())
.pipe(task(etc))
//etc
.pipe(gulp.dest(dst));
done();
});
I'm specifically using the old style in my example above to demonstrate that it really doesn't matter how you do it. Gulp is very forgiving, but it is a stickler about the callbacks and the paths. I think the paths thing is actually related to chokidar or some sub module that gulp is using.
Hope this saves someone some pain.
I struggled with this one myself..
took me hours of headache only to find out that basically everything changes with v4.0
I've run my code like this and it works perfectly...
//Do everything once!
gulp.task('default', function(){
gulp.watch('src/styles/*.css', gulp.series('css')),
gulp.watch('src/html/*.html', gulp.series('copyHTML')),
gulp.watch('src/js/*.js', gulp.series('scripts')),
gulp.watch('src/images/*', gulp.series('imageMIN'));
return
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With