I am trying to pass a parameter to a task that is being invoked by gulp-watch. I need it because I am trying to build a modular framework.
- So if a file changes in module 1, the other modules don't need to be rebuild.
- And I want just one function to create the concatted & uglified files per module.
This is what I got so far:
//here I need the 'module' parameter
gulp.task('script', function(module) { ... }
gulp.task('watch', function() {
gulp.watch('files/in/module1/*.js', ['script']); //here I want to pass module1
gulp.watch('files/in/module2/*.js', ['script']); //here I want to pass module2
});
A lot of the documentation/examples seems to be outdated (gulp.run(), gulp.start()).
I hope someone can help me out here.
In that i will answer directly the question "How to pass a parameter to gulp-watch invoked task"
My way of doing, and one of the possibility i see, is to use a global variable to pass the value between the two blocks. you set it just before launching the task in the watcher. And in the task, just at the start you pass it to a local variable.
See this answer for more details: https://stackoverflow.com/a/49733123/7668448
In what you want to achieve, you can too use just one watcher over the directory that hold all modules. If so is the structure. Then when a change happen, you can recover the changed file path. From that you can deduce what module does belong to. By getting the Module folder. That way you will not need to add a new watcher for each new module. Which can be nice when there is multiple contributors to the project for example when working on open source. And you do it one time, and don't have to care about adding anything. Just like with the delegation principle, with DOM event handling when there is multiple elements. Even if the chosen structure, doesn't have all the modules in one directory. You can stay pass multiple globs to the one watcher.
And following the structure you have, you can work your way to deduce what module is.
For the watcher here how i suggest you do it:
The
evt
here hold multiple info: cwd, base, state, _contents ...etc And what interest us ispath
. Soevt.path
will give you the path of the changed file.In your task either you do that:
or you use a function :
I had the very same issue, searched for a while, and the "cleanest" way I came up with, uses the
.on()
event handler ofgulp.watch()
, and the.env
property of gulp-util:gulp-util also comes in handy if you need to pass (global) parameters from the shell:
Hope this helps someone else out there!
Regards.