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)
- Dynamic Imports that are marked external are hoisted HOT 6
- New case for "__webpack_modules__[moduleId] is not a function" error with Module Federation HOT 1
- Webpack 5 dealing with globals differently - Not found in changelog
- Eager consumption of a shared module if there are several versions of the module
- Code splitting single JS files based on which functions are used by which parent components or files. HOT 1
- PreOrderIndex and PostOrderIndex are incorrect in some cases
- Exported object names not minified since 5.79 HOT 2
- Empty file emitted for `await import('crypto')` in a dependency despite resolve.fallback.crypto = false
- Different window.open in an async call behaviour between Webpack 4 & Webpack 5 HOT 4
- Add null check to avoid `TypeError: Cannot read properties of undefined (reading 'call')` HOT 1
- this.importModule broken in esm packages HOT 7
- How does one dynamically import optional dependencies?
- Warning appeared after updating node version HOT 1
- Allow aliases to be resolved without an extra slash (#14370) HOT 2
- Cannot read properties of undefined (reading 'Z')
- Cannot read property 'name' of undefined HOT 1
- when setting `optimization.providedExports` to false, outputModule wouldn't generate export statement HOT 3
- Bundled code is attempting to export before importing (ERROR: _common__WEBPACK_IMPORTED_MODULE_1__ is undefined)
- Export warnings while exporting CSS using Webpack 5 HOT 5
- Clarification on the Use of require Parameter in require.ensure Callback function HOT 3
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.