Binding a context to task functions referenced in series and parallel
See original GitHub issueI have a config registry that’s basically the same thing as what’s in the docs. I also have about a dozen functions that represent different steps of a build process I have. I only expose three tasks to the actual gulp CLI tool (clean, build and serve).
My build task looks similar to this
gulp.task('build', gulp
.series(cleanBuild, gulp
.parallel(copyHtml, copyImages, babelCompile, prepareAppIndex, copyVendorFiles, lessCompile)));
Now my problem is that the functions (copyHtml, copyImages, etc) are not registered as tasks with my ConfigRegistry. That makes sense, they’re not really “tasks”. At least not ones I want to expose directly because that would just cause static/cruft for devs who just want an easy to use build process. So what happens is the functions try to access this.config
but it’s never been binded because the individual functions are not registered as tasks so I get a classic “Cannot read property ‘config’ of undefined” TypeError.
Any ideas how I can keep this elegant or how undertaker could be updated to make this easier? The only way I can see to do it somewhat elegantly right now is to wrap each of my functions in a factory function. The function would then close (in the closure sense) over the factory function’s scope which would contain a config object I pass in (or I could just bind the config to the function).
function copyHtmlFactory(config) {
function copyHtml() {
// do something with the config parameter or this.config if using bind
}
return copyHtml; // or return copyHtml.bind({config: config});
}
I think another way I could do it would be to bind the function references (without the use of a factory function) before they are passed to .series() and .parallel().
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
A custom registry can do tons of things.
How I would handle this:
__copyHtml
).set()
method, check if the name has the double underscore, and if it does, add it to a separate object attached to your custom registry. Also, make sure to bind the context and return the newly bound function in this method..get()
method, check the name for double underscores and fetch out of the correct object..tree()
method, only return public tasks. This method is used for exposing things to the CLI..init()
method, register all tasks (public and private).I think the above solution solves all your problems. The implementation for custom registries had a ton of thought behind it to allow you to implement these types of things but you need to think thoroughly about the solution. This is a highly advanced feature for gulp and you can do lots of wonderful and scary things.
No it’s not.