Gulp: G4 - Watch only running once

Created on 30 Apr 2016  路  5Comments  路  Source: gulpjs/gulp

Gulp 4 newbie here. I'm having trouble firing off a series of tasks using watch in Gulp 4. Anyone see an issue with this?

gulp.watch(['scripts/**/*.js','!scripts/main.min.js'], gulp.series(buildScripts,reload));

The darn thing only runs once.

Most helpful comment

Wrap the browserSync.reload() in a function with a done callback :

function reload(done) {
  browserSync.reload();
  done();
}

gulp.watch(['scripts/**/*.js','!scripts/main.min.js'], gulp.series(buildScripts, reload));

All 5 comments

More specifically, an exact duplicate of https://github.com/gulpjs/gulp/issues/1623 - please see my response at https://github.com/gulpjs/gulp/issues/1623#issuecomment-215847959

Your reload function is most likely a method pulled directly from BrowserSync but it isn't an async function. The reason this was working at all (though incorrectly) was due to inconsistencies in our API.

Docs for gulp.watch in the 4.0 branch have been updated for more clarity.

@phated @TrySound Thanks very much for the help! ;)

Here is what I ended up doing to solve the issue w/Browsersync and Gulp 4.

Removed the reload from the watch:

gulp.watch(['scripts/**/*.js','!scripts/main.min.js'], buildScripts);

Added .pipe(browserSync.stream()) to my task:

function buildScripts(){
      return gulp.src(appDefaults.mainJsFiles)
        .pipe(concat(appDefaults.jsMin))
        .pipe(uglify())
        .pipe(gulp.dest(appDefaults.jsDir))
        .pipe(browserSync.stream()) // <!-- this is the line added
        .pipe(notify({message: 'JS Compiled'}));
    }

Wrap the browserSync.reload() in a function with a done callback :

function reload(done) {
  browserSync.reload();
  done();
}

gulp.watch(['scripts/**/*.js','!scripts/main.min.js'], gulp.series(buildScripts, reload));
Was this page helpful?
0 / 5 - 0 ratings