Search code examples
javascriptgulpgulp-watchgulp-4

Gulp 4 watch runs only 1 time


As I mentioned in the title there is a problem running gulp.watch. It runs watch only after first change in the file, when I changing second, third and etc it doesn't run task.
Below is my gulpfile.js:

var gulp = require('gulp');
var babel = require('gulp-babel');
var rename = require("gulp-rename");
var del = require('del');
var less = require('gulp-less');

gulp.task('es6', function () {
   return gulp.src('./test.js')
       .pipe(rename(function (path) {
           path.basename += "-es6";
           return path;
       }))
       .pipe(babel())
       .pipe(gulp.dest('./'))
});

gulp.task('clean', function () {
    return del('./test-es6.js');
});

gulp.task('watch', function () {
   gulp.watch( './test.js', gulp.series('es6') );
    console.log('Running watch...');
});

gulp.task('default', gulp.series('clean', 'es6', gulp.parallel('watch') ));

And some logs :

$: gulp
[14:22:40] Using gulpfile /var/www/html/es2015/gulpfile.js
[14:22:40] Starting 'default'...
[14:22:40] Starting 'clean'...
[14:22:40] Finished 'clean' after 11 ms
[14:22:40] Starting 'es6'...
[14:22:43] Finished 'es6' after 2.11 s
[14:22:43] Starting '<parallel>'...
[14:22:43] Starting 'watch'...
Running watch...
[14:22:55] Starting '<series>'...
[14:22:55] Starting 'es6'...
[14:22:55] Finished 'es6' after 42 ms
[14:22:55] Finished '<series>' after 43 ms  << first change, but no second third and etc.

I used similar configuration in couple of projects and it was fine, everything worked.
I don't know if this information is important, but I'm using Ubuntu 14.04


Solution

  • After long trying I found a solution for this problem.

    I don't know why but this configuration works on Windows, but as appeared on Ubuntu I had to add ** to path of the watched file this part of code:

    gulp.task('watch', function () {
        gulp.watch( './**/test.js', gulp.series('es6') );
        console.log('Running watch...');
    });