Closed willpracht closed 6 years ago
Actually, the syntax would be more like this:
require('require-dir')('./path/to/tasks')(gulp, $, config);
:+1:
I want this plz
This one is a blocker for me, I'll try to look into it.
Now that I've looked a bit more at how require-dir actually works, I realize that we could simply achieve what we want by iterating over the object returned when calling requireDir(...). Basically if the required module returns a function, one can easily invoke it afterwards.
I'll send a pull request anyway to add support for an 'args' property on the options object that require-dir already has; this can still be used as a short-hand way of passing some value/object to all loaded modules (assuming they all export a function that can be directly invoked)
+1 @dsebastien
Just for the record here's a simplified version of what I've ended up doing in my project:
// ./gulp/tasks/something.js
...
module.exports = {
registerTask: function(gulp){
...
}
...
// consumer
const loadedModules = requireDir("./gulp/tasks", {
recurse: false
});
for(let key in loadedModules){
if(loadedModules.hasOwnProperty(key)){
let loadedModule = loadedModules[ key ];
if(loadedModule.registerTask){
console.log(`Registering module: ${key}`);
loadedModule.registerTask(gulp);
} else{
throw new TypeError(`One of the loaded modules does not expose the expected interface: ${key}`);
}
}
}
You might need something a bit more crafty if you need recursion though :)
:+1:
i was thinking something like this:
require('require-dir')('./tasks',{'params':[someArg1, someArg2, someArg3]});
When require, pass params trough apply function.
if (opts.duplicates) {
map[file] = require(path).apply(this,opts.params);
if (!map[base]) {
map[base] = map[file];
}
} else {
map[base] = require(path).apply(this,opts.params);
break;
}
You can do this with the new mapValue
parameter: mapValue(fn) { return fn(args); }
It would be fantastic to have the ability to pass in parameters to the
require()
.Example:
becomes something like...
This would give us the ability to split up Gulp tasks into their own files.
Full Example:
Does anyone have any thoughts on this?