Open rafegoldberg opened 7 years ago
An example could be the following:
Say you wanted a resolver that would camelcase all matching files.
--- src
|- main.js
|- some-other-file.js
`--- includes
|- one-include.js
`- another-include.js
main.js could contain the following code:
const includes = require('./includes/*.js', { mode: 'hash', resolve: ['reduce-prefix', 'strip-ext', function my_camelcase_resolver(base, files, config) {
function camelize(str) { // camelize taken from http://stackoverflow.com/a/2970667
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(letter, index) {
return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
}).replace(/\s+/g, '');
}
return Object.keys(files).reduce(function(resolved_files, file) {
resolved_files[camelize(file)] = files[file];
return resolved_files;
}, {});
}] });
Which would get converted into
const includes = {
"oneInclude": require("./includes/one-include.js"),
"anotherInclude": require("./includes/another-include.js"),
};
Writing this down, I realized this is maybe not the most user-friendly way to do this, especially if you want to use the same resolver multiple times. Also, since it's been a long time since I've worked on this plugin, I might be mistaken about the way to go about this. I suggest you try it out and let me know how it works out. I have several ideas of how this use-case might be improved upon.
Woah, would not have gotten that! I ended up getting pretty damn good results from the built-in resolvers... Still, great to see an example of how this works. Anyways, appreciate the response; a simplified/streamlined interface for implementing custom resolvers would make this package the jams. I'd be more than happy to help if/where I can be useful.
would love to see some better documentation on using a custom
resolve
function. the docs have a pretty good primer, but an example might help the less intuitive among us. (read: “me”).