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)
- Module Federation - Some chunks are not loaded when navigating from the Remote module to the Host module
- failed to import ffmpeg-core.js HOT 1
- Webpack 5 doesnt generate the chunks of imports HOT 1
- Error: Cannot find module 'webpack-cli/package.json' HOT 2
- Unable to bust cache of module federation remotes....
- Re-exported Ant Design components don't have same style with the original components
- failed to import ffmpeg-core.js HOT 3
- DllPlugin: When processing multiple chunks, does the `path` option must have a string template?
- Webpack BannerPlugin with `raw` option should put `use strict` on the very first line HOT 10
- After webpack 5 migration not able to use local packages in a mono repo
- 95% emitting emit phase takes much longer in Node >=16
- Plugin apply not working for multi compiler HOT 2
- packages/tailwindcss/src/compile.ts HOT 1
- Current error handling mainly involves calling the `onInvalidCandidate` callback function to handle invalid candidates, and printing corresponding error messages to the console.
- [5.91.0] "Fixed fs types" breaks `outputFileSystem` compatibility with `memfs` HOT 6
- Using `[name]` within `output.library`, the correct value isn't utilized in the source map namespace HOT 1
- Not found node_modules/@angular/common/bundles/common-http.umd.js
- webpack5.90.0 Modifying files in the node_modules directory (local component library) cannot trigger hot updates HOT 1
- Delete this. HOT 1
- how to make webpack recompile on dev mode with cache on? 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 webpack.