jordan
jordan

Reputation: 347

How can I fix gulp watch not watching files

I'm using gulp to watch file changes and compile my scss, But watch isn't tracking the file changes.

im using gulp-sass and the version is 4.0

const { src, dest, watch, series } = require('gulp');
const { sass } = require('gulp-sass');

function compileSass() {
 return src('app/assets/scss/main.scss')
 .pipe(sass())
 .pipe(dest('app/css'));
}

function start() {
  //compile and watch
  watch('app/assets/scss/**/*.scss', { events: 'change' }, function(compileSass) {
    // body omitted
    compileSass();
  });

}

exports.default = series(start);

Upvotes: 2

Views: 708

Answers (2)

Mark
Mark

Reputation: 180631

Try:

function start() {
  //compile and watch
  watch('app/assets/scss/**/*.scss', { events: 'change' }, function(cb) {
    // body omitted
    compileSass();
    cb();
  })
}

cb is a callback function - just leave it as cb (it doesn't need to exist anywhere else) and is called last in the task.

Upvotes: 1

Rannie Aguilar Peralta
Rannie Aguilar Peralta

Reputation: 1742

Not sure why your task is not wrapped in gulp.task.

Try to paste this code in your gulpfile.js

gulpfile.js

const gulp = require('gulp');
const sass = require('gulp-sass'); // not { sass }

/** Transpile sass/scss to css */
gulp.task('compile-sass', () => {
  return gulp.src('app/assets/scss/main.scss')
    .pipe(sass())
    .pipe(gulp.dest('app/css'));
});

/** Run the compile-sass task then set a watchers to app/assets/scss/**/*.scss */
gulp.task('watch', gulp.series(
  'compile-sass',
  (done) => {
    gulp.watch('app/assets/scss/**/*.scss')
      .on('change', gulp.series('compile-sass'))
    done()
  },
));

/** Default task */
gulp.task('default', gulp.series('watch'));

Now you can run it using gulp in your terminal.

Upvotes: 0

Related Questions