Gulp watch executes only one time

163 Views Asked by At

I know there are many questions with new gulp version . I had to change my gulpfile.js and now when I run it , it only executes once . I tried using return on gulp.watch function but it does nothing . Here is my code :

'use strict';

var gulp = require('gulp');
var sass = require('gulp-sass');
var runSequence = require('run-sequence');
var iconfont = require('gulp-iconfont');
var async = require('async');
var consolidate = require('gulp-consolidate');
var sassLint = require('gulp-sass-lint');

gulp.task('sass', function(){
    return gulp.src('app/scss/**/*.scss')
        .pipe(sass())
        .pipe(gulp.dest('app/css'))
});

gulp.task('sass-lint', function () {
    return gulp.src('app/scss/**/*.s+(a|c)ss')
        .pipe(sassLint())
        .pipe(sassLint.format())
        .pipe(sassLint.failOnError())
});

gulp.task('watch', gulp.series('sass'), function (){
    gulp.watch('app/scss/**/*.scss', gulp.series(gulp.parallel('sass' , 'sass-lint' )));
});

gulp.task('build', function (callback) {
    runSequence(['sass'],
        callback
        )
});

gulp.task('iconfont', function(done){
    var iconStream = gulp.src(['app/images/svg/*.svg'])
      .pipe(iconfont({
        fontName: 'icons',
      }));
      async.parallel([
        function handleGlyphs(cb) {
            iconStream.on('glyphs', function(glyphs, options) {
                gulp.src('conf/_iconfont-template.scss')
                  .pipe(consolidate('lodash', {
                    glyphs: glyphs,
                    fontName: 'icons',
                    fontPath: '../fonts/',
                    className: 's'
                  }))
                  .pipe(gulp.dest('app/scss/utilities/'))
                  .on('finish', cb);
              });
            },
            function handleFonts (cb) {
                iconStream
                    .pipe(gulp.dest('app/fonts/'))
                    .on('finish', cb);
            }
          ], done);
});

Here is what i get in a terminal :

enter image description here

1

There are 1 best solutions below

2
On BEST ANSWER

Your problem is mainly here:

// your version
gulp.task('watch', gulp.series('sass'), function (){

// should be:
gulp.task('watch', gulp.series('sass', function (done) {

gulp.task now takes only two arguments.

Other comments:

  1. This part of your 'watch' task is strange:

    gulp.series(gulp.parallel('sass' , 'sass-lint' )));
    

gulp.series is doing nothing there, presumably you want :

gulp.parallel('sass' , 'sass-lint' ));

Final 'watch' task with done callback to signal async completion.

gulp.task('watch', gulp.series('sass', function (done){
    gulp.watch('app/scss/**/*.scss', gulp.parallel('sass' , 'sass-lint'));
    done();
}));
  1. You don't need the run-sequence plugin anymore. You could change the 'build' task to use gulp.series instead.