How to Gulp-Watch Multiple files?

CssGulpGulp Watch

Css Problem Overview


I have something like this:

gulp.task('default', ['css', 'browser-sync'] , function() {

	gulp.watch(['sass/**/*.scss', 'layouts/*.css'], function() {
		gulp.run('css');
	});

});

but it does not work, because it watches two directories, the sass and the layouts directory for changes.

How do I make it work, so that gulp watches anything that happens inside those directories?

Css Solutions


Solution 1 - Css

gulp.task('default', ['css', 'browser-sync'] , function() {

    gulp.watch(['sass/**/*.scss', 'layouts/**/*.css'], ['css']);

});

sass/**/*.scss and layouts/**/*.css will watch every directory and subdirectory for any changes to .scssand .css files that change. If you want to change that to any file make the last bit *.*

Solution 2 - Css

You can write a watch like this.

gulp.task('watch', function() {
    gulp.watch('path/to/file', ['gulp task name for css/scss']);
    gulp.watch('path/to/file', ['gulp task name for js']);
});

This way you can set up as many tasks as you want via the file path of what you want to watch followed by the name of the task you created. Then you can write your default like this:

gulp.task('default', ['gulp task name for css/scss', 'gulp task name for js']);

If you want to simply watch for various file changes, then just watch files using glob like *.css in your task.

Solution 3 - Css

One problem that has arisen for multiple people (including me) is that adding a gulp.filter outside of the task causes gulp.watch to fail after the first pass. So if you have something like this:

var filter = gulpFilter(['fileToExclude.js'])
gulp.task('newTask', function(){ ...

Then you need to change it to:

gulp.task('newTask', function(){
  var filter = gulpFilter(['fileToExclude.js'])

The filter has to be included in the task function. Hope that helps someone.

Solution 4 - Css

This works for me (Gulp 4):

function watchSass() {
  return gulp.watch(sassGlob, { ignoreInitial: false }, buildCss)
}

function watchImages() {
  return gulp.watch(imagesGlob, copyImages)
}

exports.watch = gulp.parallel(watchSass, watchImages)

Solution 5 - Css

@A.J Alger's answer worked for me when using Gulp v3.x.

But starting with Gulp 4, The following appears to work for me.

Notice that each task has to return a value or call "done()". The main task in this example is 'watchSrc' which in parallel calls the other tasks.

gulp.task('watchHtml', function(){
    return watch('src/**/*.html', function () {
        gulp.src('src/**/*')
            .pipe(gulp.dest(BUILD_DIR))
    })

})
gulp.task('watchJS', function(){
    return watch('src/**/*.js', 'devJS')
})

gulp.task('watchCSS', function(){
    return watch(['src/**/*.css', 'src/**/*.scss'], 'buildStyles')
})


gulp.task('watchSrc', gulp.parallel('watchHtml', 'watchJS', 'watchCSS'), function(done)
{
    done()
})

Solution 6 - Css

As of gulp 4, this is another option:

const { watch, series, parallel } = require('gulp');

function watchTask(cb) {
  // this will execute all task on any changes
  watch(['src/**/*'],
    series(parallel(jsTask, htmlTask, assetTask),
  ));
  // this will run specific task based on file type or folder
  watch(['src/**/*.js'], series(jsTask));
  watch(['src/**/*.html'], series(htmlTask));
  watch(['assets/**/*'], series(assetTask));
}

exports.default = series(parallel(jsTask, htmlTask, assetTask), watchTask);

Solution 7 - Css

If you convert your tasks into functions

 function task1(){
   return gulp...
   ...
 }

There are then 2 useful methods you can use:

GULP.SERIES will run the tasks synchronously

gulp.task('default', gulp.series(task1,task2));

GULP.PARALLEL will run them asynchronously

gulp.task('default', gulp.parallel(task1,task2));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLoveAndHappinessView Question on Stackoverflow
Solution 1 - CssKelly J AndrewsView Answer on Stackoverflow
Solution 2 - CssDr G.View Answer on Stackoverflow
Solution 3 - CssLyraView Answer on Stackoverflow
Solution 4 - Csssad comradeView Answer on Stackoverflow
Solution 5 - Cssuser6123723View Answer on Stackoverflow
Solution 6 - CssAlexander DischbergView Answer on Stackoverflow
Solution 7 - CssAlessandro CataniaView Answer on Stackoverflow