最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Gulp 4 - watch not watching changes - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Dec 17, 2018 at 13:48 vsync 131k59 gold badges340 silver badges423 bronze badges asked Jan 7, 2018 at 16:47 Martin Hetfield MaliMartin Hetfield Mali 3184 silver badges13 bronze badges 3
  • 2 Helped me: API - Watching Files – vsync Commented Dec 17, 2018 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 Commented Feb 13, 2019 at 16:04
  • This article explains cb and other related issues. – YCode Commented Feb 13, 2019 at 19:46
Add a ment  | 

3 Answers 3

Reset to default 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");
  })
}));

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');
});

I had some issues too, and i found this tutorial of gulp 4, this is my gulpfile to pile scss and watch the Scss files, concat and pile 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: 'pressed'}).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.

发布评论

评论列表(0)

  1. 暂无评论