3

I have an app with a very moderate gulpfile. I've just upgraded Gulp from v3.9.1 to v4.0.2 and adapted the gulpfile.js. All tasks are working fine again, only the watching is not.

There are no errors, but Gulp watch seems not to be checking the defined files for changes:

$  gulp -v
CLI version: 2.2.0
Local version: 4.0.2
$ gulp
[11:39:34] Using gulpfile /var/www/.../my-app/gulpfile.js
[11:39:34] Starting 'default'...
[11:39:34] Starting 'run'...
[11:39:34] Starting 'build-less'...
[11:39:34] Finished 'build-less' after 16 ms
[11:39:34] Starting 'build-vendors'...
[11:39:34] Finished 'build-vendors' after 5.5 ms
[11:39:34] Finished 'run' after 23 ms
[11:39:34] Starting 'watch-files'...
[11:39:34] Starting 'watchFiles'...

How to get Gulp watch working for Gulp v4?


Here is my original gulpfile.js for Gulp v3:

/**
 * Gulp File
 *
 * run `gulp run && gulp watch-files` on the command line
 */

// Include Gulp plugins
var gulp = require('gulp'),
    less = require('gulp-less'),
    watch = require('gulp-watch'),
    prefix = require('gulp-autoprefixer'),
    plumber = require('gulp-plumber'),
    filter = require('gulp-filter'),
    rename = require('gulp-rename'),
    path = require('path')
;

// Compile LESS to CSS
gulp.task('build-less', function() {
    const fileFilter = filter(['**/*', '!**/mixins.less', '!**/variables.less']);
    gulp.src('./public/less/*.less') // path to less file
        .pipe(fileFilter)
        .pipe(plumber())
        .pipe(less())
        .pipe(gulp.dest('./public/css/')) // path to css directory
    ;
});

// Get vendors' code
gulp.task('build-vendors', function() {
    gulp.src(['./public/components/bootstrap/less/theme.less', './public/components/bootstrap/less/bootstrap.less']) // path to less file
        .pipe(plumber())
        .pipe(less())
        .pipe(rename(function (path) {
            //rename all files except 'bootstrap.css'
            if (path.basename + path.extname !== 'bootstrap.css') {
                path.basename = 'bootstrap-' + path.basename;
            }
        }))
        .pipe(gulp.dest('./public/css')) // path to css directory
    ;
});

// Run the build process
gulp.task('run', ['build-less', 'build-vendors']);

// Watch all LESS files, then run build-less
gulp.task('watch', function() {
    gulp.watch('public/less/*.less', ['run'])
});

// Default will run the 'entry' task
gulp.task('default', ['watch', 'run']);

Here is the new variant for Gulp v4:

/**
 * Gulp File
 *
 * run `gulp run && gulp watch-files` on the command line
 */

// Include Gulp plugins
var gulp = require('gulp'),
    less = require('gulp-less'),
    watch = require('gulp-watch'),
    prefix = require('gulp-autoprefixer'),
    plumber = require('gulp-plumber'),
    filter = require('gulp-filter'),
    rename = require('gulp-rename'),
    path = require('path')
;

// Compile LESS to CSS
function buildLess(done) {
    const fileFilter = filter(['**/*', '!**/mixins.less', '!**/variables.less']);
    gulp.src('./public/less/*.less') // path to less file
        .pipe(fileFilter)
        .pipe(plumber())
        .pipe(less())
        .pipe(gulp.dest('./public/css/')) // path to css directory
    ;
    done();
};

// Get vendors' code
function buildVendors(done) {
    gulp.src(['./public/components/bootstrap/less/theme.less', './public/components/bootstrap/less/bootstrap.less']) // path to less file
        .pipe(plumber())
        .pipe(less())
        .pipe(rename(function (path) {
            //rename all files except 'bootstrap.css'
            if (path.basename + path.extname !== 'bootstrap.css') {
                path.basename = 'bootstrap-' + path.basename;
            }
        }))
        .pipe(gulp.dest('./public/css')) // path to css directory
    ;
    done();
};

// Watch all LESS files, then run build-less
function watchFiles() {
    return gulp.watch(['public/less/*.less'], gulp.series('build-less'));
};

gulp.task('build-less', buildLess);
gulp.task('build-vendors', buildVendors);
gulp.task('run', gulp.series('build-less', 'build-vendors'));
gulp.task('watch-files', gulp.series(watchFiles));
gulp.task('default', gulp.series('run', 'watch-files'));
automatix
  • 14,018
  • 26
  • 105
  • 230

1 Answers1

3

it works very well i just tested

i did installed the last version of gulp in global

npm install gulp -g

gulp -v CLI version: 2.2.0 Local version: 4.0.2

then

gulp run watch-files

it update the css file after any changes

So i suggest to use path package instead of writing statically the folder's path.

Example:

path.resolve(__dirname, "foo/bar/less/");

enter image description here

Aziz.G
  • 3,599
  • 2
  • 17
  • 35
  • Thank you for your answer! I've just tried this out, but the result is the same: No errors, but also now `wath`ing. Changes on files are not triggering rebuilding CSS. – automatix Aug 20 '19 at 14:28
  • sorry i didn't understand can you be more clear what's your issue now ? – Aziz.G Aug 20 '19 at 14:31
  • 1
    The issue is, that the "`watch`ing" is not working, meaning: When I change a LESS file (that is matching the path pattern `./public/less/*.less`), I'm expecting, that it triggers rebuilding the corresponding CSS file. It was working before the migration to Gulp 4 and now it's not -- CSS files aren't got updated on changes. – automatix Aug 20 '19 at 15:59
  • ok i will test this, the first i just read your code and i thought it's the return is the issue, i am goin to test and let you know – Aziz.G Aug 20 '19 at 17:58
  • Hm... I don't see any differences between the code on the screenshot and mine (just some line breaks -- formatting). (If you though made some changes, please add the code as text to the answer.) So that means, the same code is working on your machine and not working on mine. – automatix Aug 20 '19 at 19:22
  • this is the same code i am using node version `10.15.3` – Aziz.G Aug 20 '19 at 19:29
  • I'm using `node -v -> v6.9.0`, `nodejs -v -> v8.10.0`. But it should not matter, since before th emigration to Gulp 4 it was working. – automatix Aug 20 '19 at 19:46
  • I have no idea I am just telling you what I have I am not working on the same project so I cannot guess what's wrong cause it works for me when I tested without any additional changes to your code – Aziz.G Aug 20 '19 at 20:48
  • Yes, sure, I understand. Btw. I upgraded NodeJS to the `v10.16.3`, but no changes. – automatix Aug 20 '19 at 21:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/198243/discussion-between-g-aziz-and-automatix). – Aziz.G Aug 21 '19 at 13:55
  • and could you please try to change your static path with ```path.resolve(__dirname, "foo/bar/less/")``` so we can be sure of path problems – Aziz.G Aug 21 '19 at 13:58
  • Just tried this out: `gulp.watch(path.resolve(__dirname, 'public/less/') + '*.less', gulp.series('build-less'));` No changes. – automatix Aug 21 '19 at 14:11
  • The environment, where it's not working, is my Ubuntu VirtualBox VM. I've just tried this out on my Windows 7 host system. There it's working. – automatix Aug 21 '19 at 15:16
  • i know it there is something wrong with the environment, good that you find why – Aziz.G Aug 21 '19 at 18:07