1

I'm trying to finish my gulpfile.js but I'm running into a problem with "gulp watch". When I set my watcher to run, it detects changes and run the assigned task, but only once. The next changes on the files don't trigger the tasks anymore. I don't know why, but my watcher is working just once.

I believe the watcher is not realizing the tasks it triggered are finished. Or, maybe it's something to deal with the lazy loading or run-sequence plugins I'm using...

var
  gulp = require('gulp'),
  plugins = require('gulp-load-plugins')({
    DEBUG: true
  });
plugins.runSequence = require('run-sequence');

gulp.task('watch',function(){
  gulp.watch('public/**/*.+(css|js)',['buildEspecifico']);
});

gulp.task('buildEspecifico',function(callback){
  return plugins.runSequence(
    ['deletarMainMin'],
    ['agruparJS','agruparCSS']
  );
});

'deletarMainMin','agruparJS','agruparCSS' are other tasks defined in the same gulpfile.

Thanks in advance!

Cheers,

Fabio.

FTM
  • 1,887
  • 17
  • 34

1 Answers1

2

I believe the watcher is not realizing the tasks it triggered are finished.

Yes, that is exactly it. In your buildEspecifico task you accept a callback. That means gulp will not consider your buildEspecifico task to have finished unless you invoke that callback.

Since you don't invoke callback the buildEspecifico task never actually finishes once it runs. And because your buildEspecifico task is technically still running gulp doesn't start the task again when a file changes.

The solution is to pass callback to runSequence. It will invoke the callback when all of your tasks in the sequence have finished.

gulp.task('buildEspecifico',function(callback){
  return plugins.runSequence(
    ['deletarMainMin'],
    ['agruparJS','agruparCSS'],
    callback
  );
});

Alternatively you can also just leave out the callback altogether:

gulp.task('buildEspecifico',function(){
  return plugins.runSequence(
    ['deletarMainMin'],
    ['agruparJS','agruparCSS']
  );
});

Obviously you have to make sure that the tasks deletarMainMin, agruparJS, agruparCSS themselves finish, so either call or leave out the callback in those tasks too.

Sven Schoenung
  • 30,224
  • 8
  • 65
  • 70
  • Hello @Sven! Thanks a lot for the help. I can't remove that callback because it is necessary to run the tasks in sequence correctly. I'll try to add the callback to the sequence and see what happens. Will update the thread here in a few minutes. – FTM Oct 05 '16 at 20:48
  • Hey @Sven, it didn't work out. What happened is that after I made a change, the tasks started to run in an infinite loop. Like if the watcher was calling the `'buildEspecifico'` multiple times. – FTM Oct 05 '16 at 20:50
  • The same happened when I tried removing the callback. The watcher kept calling the task without stop. – FTM Oct 05 '16 at 20:55
  • That's a different problem and just shows that my solution is correct. You're running into an infinite loop because one of your other three tasks modifies the files you're watching. That problem didn't show up earlier because you couldn't execute `buildEspecifico` more than once. Now that you **can** run it more than once the other tasks cause the infinite loop that you would have gotten in the first place. – Sven Schoenung Oct 05 '16 at 21:06
  • You can easily check this by removing all code from your `deletarMainMin`, `agruparJS` and `agruparCSS` tasks so that they are just empty functions. – Sven Schoenung Oct 05 '16 at 21:08
  • You're right. I'll have to modify the watchers to exclude the files being modified. Thanks Sven! – FTM Oct 06 '16 at 04:54
  • Adding the callback and removing the files that were being modified by the other tasks fixed the problem! – FTM Oct 06 '16 at 06:24