-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Webpack's require.context
not being employed
#35
Comments
Thanks so much for reporting the issue @brianmhunt. We'd have to add a sniff for require.context in detective-cjs and then call out to either webpack's resolver or roll our own to evaluate require.context to yield all of the files to be required. Any other ideas for making this work? |
Thanks for the update @mrjoelkemp . I think that's pretty much it. I didn't see any exposed code in Webpack to allow use of require.context externally, and I did a quick search but didn't see any useful npm projects that might've already solved it. The require.context(directory, useSubdirectories = false, regExp = /^\.\//)
The globbing ought to be pretty straightforward walk + grep. The function could basically just be equivalent to a Here's a quick mock-up of roughly what I think it'd need to do: require.context = function(dir, includeSubdirectories, regex) {
const contextualRequire = (file) => require(path.join(dir, file))
contextualRequire.keys = someGlobFunction(dir, includeSubdirectories)
if (regex) {
contextualRequire.keys = contextualRequire.keys.filter((r) => regex.test(r))
}
return contextualRequire
} |
Thanks for the thoughts! I wonder if the resolveToContext option is the path forward. I'm currently not using that when resolving files via webpack (there's a 3 argument form of the resolver where the first arg is the context). If that resolver option were the key, this would get supported via a few changes:
The painful part of pushing this through filing-cabinet is that the api of that module is meant to return a single string, not an array of dependencies that were generated from a single import (require.context call). As such, it might be worth putting this (either your solution or enhanced-resolve's) into detective-cjs proper. I'm open to reviewing a pull request that gets the desired functionality in. My hesitation is that require.context and even dynamically requiring files (within a for loop) are not possible with ES6 imports – which we'll all eventually migrate to anyway. Not sure if it makes sense to put in the work for these dynamic requires, but I'm happy to review and merge if others feel strongly. |
Related/original report: pahen/madge#117
It appears Precinct does not work with webpack's dynamic loading with require.context (basically glob) so e.g.
This ought to load all the modules with /.js$/ from the current directory (".") and subdirectories (that's what the
true
specifies).It does not appear to do so, at least via Madge/node-dependency-tree, and I did not see any mention of it in the Precinct code.
Just a heads up.
🍻
The text was updated successfully, but these errors were encountered: