Search code examples
gulptaskgulp-watch

Gulp watch() method is not watching file changes


I am trying to automate some tasks with Gulp for Wordpress theme development. I have managed to get everything to work, except that gulp.watch doesn't watch for changes on my files automatically.

Gulp Version:

  • CLI version 2.0.1
  • Local version 3.9.1

Operating System:

  • Windows 10

Package.json:

{
  "name": "theme-development-boilerplate",
  "version": "1.0.0",
  "description": "A boilerplate for Theme Development automated with Gulp. Includes SASS, Bootstrap, jQuery, Normalize, Mordernizer, among other stacks",
  "main": "gulpfile.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Manuel Abascal",
  "license": "MIT",
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-concat": "^2.6.1",
    "gulp-error-notifier": "^1.1.0",
    "gulp-imagemin": "^5.0.3",
    "gulp-livereload": "^4.0.1",
    "gulp-sass": "^4.0.2",
    "gulp-uglify": "^3.0.1"
  },
  "dependencies": {
    "browser-sync": "^2.26.3",
    "gulp-watch": "^5.0.1"
  }
}

Gulpfile:

var gulp = require('gulp');
var watch = require('gulp-watch');
var imagemin = require('gulp-imagemin');
var uglify = require('gulp-uglify');
var errorNotifier = require('gulp-error-notifier');
var sass = require('gulp-sass');
var concat = require('gulp-concat');

// Logs Message
gulp.task('message', function(){
    return console.log('Gulp is running...');
});

// Optimize Images
gulp.task('imageMin', function (){
    gulp.src('src/images/*')
        .pipe(imagemin())
        .pipe(gulp.dest('dist/images'))
});

// Minify JS
gulp.task('minify', function(){
    gulp.src('src/js/*.js')
        .pipe(uglify())
        .pipe(gulp.dest('dist/js'));
});

// Compile Sass into CSS
gulp.task('sass', function(){
    gulp.src('src/sass/*.scss')
        .pipe(errorNotifier.handleError(sass({outputStyle: 'compressed'})).on('error', sass.logError))
        .pipe(gulp.dest('dist/css'));
});

// Scripts Concatenation
gulp.task('scripts', function(){
    gulp.src('src/js/*.js')
        .pipe(concat('main.js'))
        .pipe(uglify())
        .pipe(gulp.dest('dist/js'));
});

gulp.task('default', ['message', 'imageMin', 'sass', 'scripts']);

gulp.task('watch', function(){
    gulp.watch('src/js/*.js', ['scripts']);
    gulp.watch('src/images/*', ['imageMin']);
    gulp.watch('src/sass/*.scss', ['sass']);
});

When I use the command gulp it works like a charm, however when I run gulp watch I get this output in the console:

Starting 'watch'... Finished 'watch' after 12 ms

But it doesn't watch for anything. Any ideas on how to debug this? I read that Windows Operating System might be the issue or the version. I don't know at this point.

Thanks in advance!


Solution

  • There is no problem with windows, I use gulp on windows on a daily basis.

    And about the version, I don't think that is the culprit here either, but you should update to the latest version anyway (which is 4.0.0).

    Try using a function in your watch instead of a task, like this:

    (This is what's done in the official examples here, and also what I do and it works)

    // Scripts Concatenation
    function transpile_scripts(cb) {
        gulp.src('src/js/*.js')
            .pipe(concat('main.js'))
            .pipe(uglify())
            .pipe(gulp.dest('dist/js'))
            .on('end', cb);
    }
    
    gulp.watch('src/js/*.js', transpile_scripts)
    

    Also note that I passed a callback to my function, and I run it after the pipe is finished. This callback is provided implicitly by gulp, and it sort of keeps track of when a task is finished. In some cases not passing and executing this causes warnings and even errors. Anyway, it won't harm to do it just in case.