Comments (3)
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:
- detective-cjs would need to support this special case https://github.com/dependents/node-detective-cjs/blob/4e31629f92bafa3df9be09f5ddb3bae1474156fa/index.js#L13 of require.context. It would then have to save the arguments via a non-string output format (or an encoded string if we want to avoid multiple types for elements of the array of dependencies that precinct returns).
- The (encoded) require.context arguments would need to get processed (turning on the
resolveToContext
flag) (likely within node-filing-cabinet) when we've detected that the coded data from step 1 above.
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.
from node-precinct.
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?
from node-precinct.
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
prototype is:
require.context(directory, useSubdirectories = false, regExp = /^\.\//)
require.context
ought to return
- a function that, when called,
require
s the given file (may be relative to the directory); and - have a
keys
property that is a list of all the filenames that match.
The globbing ought to be pretty straightforward walk + grep.
The function could basically just be equivalent to a require
call (possibly with "directory" prefixed to the argument).
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
}
from node-precinct.
Related Issues (20)
- Remove rewire
- Type Definitions missing HOT 3
- Does not get correct dependencies when mixing es6 and cjs import syntax HOT 9
- Please add support for precss HOT 1
- require.resolve not being listed as a dependency HOT 2
- svelte-style html files HOT 3
- require.main.require not being parsed HOT 8
- detective-typescript uses an unsupported version of typescript HOT 1
- Allow fs to be passed in options (for paperwork)
- Dynamic imports javascript not being detected HOT 1
- Dynamic Imports in ES6 module without export are not detected HOT 4
- Keep a changelog (at least for breaking changes) HOT 1
- Update detective-typescript's parser HOT 1
- TypeScript file with a cast fails to find any dependencies HOT 2
- Missing `mixedImports` for TypeScript files HOT 1
- Support customizing options for walker HOT 2
- Export regular text instead of array in cli HOT 1
- bug
- Add recursive support HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-precinct.