5

Today I migrated to Gulp 4, but I'm not able to get my watch function to work.

// Watch
gulp.task('watch', gulp.series('typescript', 'sass', 'browserSync', function(){
  gulp.watch('./app/styles/**/*.scss', gulp.series('sass'));
  gulp.watch('app/scripts/**/*.ts', gulp.series('typescript'));
  gulp.watch('./app/*.html', browserSync.reload);
}));

typescript, sass, browserSync will run but watch does not react to file changes.

vsync
  • 118,978
  • 58
  • 307
  • 400
  • 2
    Helped me: [API - Watching Files](https://gulpjs.com/docs/en/getting-started/watching-files) – vsync Dec 17 '18 at 14:06
  • @vsync That seems to be the key to my issue right now, but how do I implement it? What is the `cb` in the API example `function css(cb) { // body omitted cb();`? } – YCode Feb 13 '19 at 16:04
  • [This article](https://css-tricks.com/gulp-for-wordpress-initial-setup/) explains `cb` and other related issues. – YCode Feb 13 '19 at 19:46

3 Answers3

4

I just had the same problem. You don't actually have a reference to the initialized browserSync inside your watch gulp task. Instead of having your browserSync.init function in a separate browserSync gulp task, move it to inside your watch task, and it should work. Hope this helps!

Example:

gulp.task('watch', gulp.series(function (){
  browserSync.init({
    proxy:"http://localhost:8888",
    injectChanges: true,
    plugins: ["bs-html-injector?files[]=*.html"]
  });
  var html = gulp.watch('development/index.html');
  html.on('change', function(path, stats) {
    console.log('you changed the html');
    browserSync.notify("Compiling, please wait!");
    browserSync.reload("index.html");
  })
  var js = gulp.watch('development/**/*.js');
  js.on('change', function(path, stats) {
    console.log('you changed the js');
    browserSync.notify("Compiling, please wait!");
    browserSync.reload();
  })
  var css = gulp.watch('development/**/*.css');
  css.on('change', function(path, stats) {
    console.log('you changed the css');
    browserSync.notify("Injecting CSS!");
    browserSync.reload("*.css");
  })
}));
Jesse
  • 3,522
  • 6
  • 25
  • 40
Neil VanLandingham
  • 1,016
  • 8
  • 15
  • This could benefit from putting the actual compiling task calls in here for clarity/completion, in other words your example watches but does nothing :) – Bung May 26 '19 at 23:14
  • @neilVanLandigham your answer save my day.but I have update it for gulp-sass – Owaiz Yusufi Nov 27 '19 at 06:25
1

Change gulp.watch('app/scripts/**/*.ts', gulp.series('typescript')); to a absolute gulp.watch('./app/scripts/**/*.ts', gulp.series('typescript'));

Also i normally stick this syntax, per task.

var watcher = gulp.watch('js/**/*.js', gulp.parallel('concat', 'uglify'));
watcher.on('change', function(path, stats) {
  console.log('File ' + path + ' was changed');
});
Remario
  • 3,813
  • 2
  • 18
  • 25
0

I had some issues too, and i found this tutorial of gulp 4, this is my gulpfile to compile scss and watch the Scss files, concat and compile to a main.min.css with autoprefix.

var gulp = require('gulp'),
    concat  = require('gulp-concat'),
    autoprefixer    = require('gulp-autoprefixer'),
    sass = require('gulp-sass');

//task para o sass

var paths = {
  styles: {
    src: 'scss/**/*.scss',
    dest: 'css'
  }
};

function styles() {
  return gulp
    .src(paths.styles.src, {
      sourcemaps: true
    })
  .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
  .pipe(concat('main.min.css'))
  .pipe(autoprefixer({
    browser: ['last 2 version', '> 5%'],
    cascade: false
  }))
  .pipe(gulp.dest(paths.styles.dest));
}

function watch() {
  gulp.watch(paths.styles.src, styles);
}

var build = gulp.parallel(styles, watch);
gulp.task(build);
gulp.task('default', build);

I think that on the gulp v4 you need to use the gulp.parallel. I'm digging to learn more about the new version.

LucasTelesx
  • 305
  • 2
  • 10