Andrew Ducker
Andrew Ducker

Reputation: 5490

How do I make Gulp automatically reload whenever the gulp.js file changes?

When I'm tweaking my gulp.js file to automate new build tasks I have to save the file, switch to the shell window I'm running gulp in, kill it, and then restart it.

It would be nice to be able to automate this, so that changes in the gulp file are automatically picked up and run.

Is there a tool option to do this?

Upvotes: 2

Views: 925

Answers (1)

Ben
Ben

Reputation: 10104

If you just want to restart whatever task(s) was running when you change the gulpfile.js, you could create a task which runs gulp in a child process:

var child = require('child_process');
var gutil = require('gulp-util');

gulp.task('run', function(done) {
  var proc;

  function restart() {
    if (proc) {
      proc.removeListener('exit', done);
      proc.kill();
      proc = null;
    }

    proc = child.exec('gulp ' + gutil.env['run-tasks']);
    proc.addListener('exit', done);
    proc.stdout.pipe(process.stdout);
  }

  gulp.watch('gulpfile.js', function(evnt) {
    restart();
  }); 

  restart();
});

gulp.task('build', ...);
gulp.task('serve', ...);

You would invoke it like: gulp run --run-tasks "build serve".

Upvotes: 1

Related Questions