Comments (9)
I'm with @FezVrasta, is this not supported anymore?
from webpack.
Sounds good and usually I would do so. It would be no problem for enhanced-require but for webpack. Webpack has to parse this function by statical code analysis so it has some restrictions. I. e. a function cannot be a closure, an object cannot contain references to variables. I think passing primitive values make this more clear.
require.context("../abc", { a: "text" }); // ok
require.context("../abc", { a: variable }); // bad
require.context("../abc", function(name) { return name.substr(2, 4) == "aa"}); // ok
require.context("../abc", function(name) { return name.substr(2, 4) == start}); // bad
require.context("../abc", true, /^[a-z]+$/); // ok
require.context("../abc", true, regExp); // bad
from webpack.
Sorry, I'm not sure whether this belonged here, in enhanced-require
or enhanced-resolve
.
I'm reading the enhanced-resolve
docs and maybe this could be done with a postprocess: {context: fn}
?
from webpack.
No'pe, that's a dead end. I thought I could modify the context in the postprocess method, but it only supplies me with the context's path.
from webpack.
require.context
is currently really unflexible... It includes all files that are requirable with a string added to the context string. This means if you can write ./B.js
it's included. webpackOptions.extensions
is used to match possible files.
This is required for compatibility. I.e. require("../components/" + expr)
Current problems for using it for other intents:
- Subdirectories are included. No opt-out possible.
- No file filters.
- enhanced-require do not include subdirectories. (Bug)
You can solve all these problems with a custom loader, but would be more pretty if you could use require.context
.
So the idea is to add two more parameters to require.context
in one of the next webpack versions to make it more useable:
require.context(context: String, recursive: Boolean = true, regExp: RegExp = null)
recusive
include subdirectories.regExp
filter included files. You promise that the string you pass to the generated function will match (after the"./"
).
An example
var fn = require.context("../components", false, /^[^\.]*$/);
This would include the correct files.
Any comments/ideas?
from webpack.
Thanks, yeah here are some comments:
- I like how you usually pass in options objects, since they make handling default/optional/named arguments easier to handle.
- If the third parameter could take a filter function, it would be a bit more flexible than with just RegExp objects.
I might look at how to do this tomorrow, I've spent all of today outside in the sun. :)
from webpack.
This is sheduled for 0.9...
from webpack.
Done
from webpack.
@sokra sorry if I'm writing in a so old issue but I'm facing issues with the proposed syntax of the post above.
I thought I could be able to do
require.context('../../app', name => ['app.js', 'reducers.js', 'routes.js', 'store.js'].indexOf(name) === -1)
to exclude the files listed in the array, but it doesn't seem to work at all.
Am I missing something or this feature has been removed?
from webpack.
Related Issues (20)
- Webpack build error for second time HOT 1
- `exportsPresence` does not work for unused imports HOT 3
- Implement ability to use "node:" prefixes for Node.js core modules HOT 1
- mangled exports breaks with destructuring assignment of JSON imports HOT 7
- there are confusing JDocs code
- Potential bug issues
- Webpack is not resolving node_modules specified in `resolve.modules` property.
- Dynamically loading chunks
- [Code implementation issues] ArrayQueue
- [feature] create cli HOT 3
- Not able to build node script
- bootstrap:27 Uncaught TypeError: __webpack_require__.nmd is not a function
- Self-reference dependency has unused export name when imported inside of a web worker
- support `import.meta.dirname` and `import.meta.filename` HOT 2
- <!-- identifier: admin-actions -->
- Remote into Web Component from React giving an error saying " Module does not exist in container"
- Multiple DefinePlugin instances cause aggressive cache invalidation due to collisions HOT 7
- Webpack fails when importing module
- import .scss build to js, the exported mapping is inconsistent with the usage【Urgent!!】
- Unused code elimination and deduplication is not applied when using sass's api: 'legacy' setting
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 webpack.