I've got some tasks already defined in gulpfile.js
and I want to use gulp-watch
plugin (to run tasks on new files). My question is, because I couldn't find anything, can I run my existing tasks while running watch (from plugin) function?
var gulp = require('gulp'), watch = require('gulp-watch'), ...; gulp.task('lint', function () { return gulp.src(path.scripts) .pipe(jshint()) .pipe(jshint.reporter(stylish)); }); gulp.task('watch', function () { watch({ glob: 'app/**/*.js' }); // Run 'lint' task for those files });
Because I don't want to include watch()
task in every task I have. I would like to have only 1 task - watch, which will combine all "watches".
----- EDIT ---- (as I probably didn't quite get my point):
I need to run task from inside of gulp('watch')
task. for example:
like I did it with gulp.watch
:
gulp.task('watch', function () { gulp.watch('files', ['task1', 'task2']); });
I need to do the same but with gulp-watch
plugin, something like (I know it wouldn't work):
var watch = require('gulp-watch'); gulp.task('watch', function () { watch({ glob: 'files' }, ['task1', 'task2']); });
The Watch method is used to monitor your source files. When any changes to the source file is made, the watch will run an appropriate task. You can use the 'default' task to watch for changes to HTML, CSS, and JavaScript files.
The watch() API connects globs to tasks using a file system watcher. It watches for changes to files that match the globs and executes the task when a change occurs. If the task doesn't signal Async Completion, it will never be run a second time.
I have also run into the problem of wanting to use gulp-watch (not gulp.watch), needing to use the callback form, and having trouble finding a suitable way to run a task in the callback.
My use case was that I wanted to watch all stylus files, but only process the main stylus file that includes all the others. Newer versions of gulp-watch may address this but I'm having problems with 4.3.x so I'm stuck on 4.2.5.
Take your pick.
var gulp = require('gulp'), watch = require('gulp-watch'), // not gulp.watch runSequence = require('run-sequence'); // plain old js function var runStylus = function() { return gulp.src('index.styl') .pipe(...) // process single file } gulp.task('stylus', runStylus); gulp.task('watch', function() { // watch many files watch('*.styl', function() { runSequence('stylus'); OR gulp.start('stylus'); OR runStylus(); }); });
All of these are working for me without warnings, but I'm still unsure about getting the "done" callback from the 4.2.x version of gulp-watch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With