user1506145
user1506145

Reputation: 5296

Gulp run watch task once at start

When starting to watch my files, the file system might have changed, when the watch was not on-line. How can I make my watch run the task once at start, after typing in the command gulp dev?

gulp.task('dev', function () {
    gulp.watch('src/**/*', ['scss','inject']);
}); 

Upvotes: 1

Views: 2535

Answers (3)

Nick Charney Kaye
Nick Charney Kaye

Reputation: 4441

To run the default task before watch:

gulp.task('watch', [ 'default' ], function () {
    gulp.watch('themes/my-theme/scss/**/*.scss', ['build-scss']);
    gulp.watch('images/*', ['compress-images']);
});

Upvotes: 1

thodic
thodic

Reputation: 2269

This is all built into gulp-watch, no need to hard code a new task!

Gulp watch includes an option called ignoreInitial which is true by default. To run the task once at start simply set this to false:

gulp.task('dev', { ignoreInitial: false }, function () {
    gulp.watch('src/**/*', ['scss','inject']);
});

Upvotes: 3

Cedon
Cedon

Reputation: 627

Add this to your gulpfile.js:

gulp.task('dev', ['scss', 'inject', 'watch']);

This way those tasks run first, and then the watch task starts so any changes to the files are accounted for prior to the Watch task starting.

Upvotes: 1

Related Questions