-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
Add require.context.keys-like feature to import #7283
Comments
There is a |
Thanks for the info! I looked hard again and I couldn't find any documentation for this option though. I managed to find the If you were interested in extending |
If you propose a good way/syntax to do that while being spec-compliant that would be a start. |
@sokra Could you provide an example of using the |
I ended up using this: const importDocs = require.context('..', true, /\.docs\.js/, 'lazy');
importDocs.keys(); // sync, return an array of gathered paths
await importDocs(path); // async, resolves to the module It's the fourth argument. The possible values seem to be: ' |
I'm closing, since I don't have a better idea than adding a magic comment. I switched to |
I'd like to reopen, since My problem is that while I can get the list of all paths with const weakDocs = require.context('__cwd', true, /\.docs\.js/, 'weak'); I still need to wrangle with the fact that const marker = '|';
import(`__cwd/${marker}${path}${marker}.docs.js`).then(useModule); For this to work I have to modify the @sokra You wrote about being spec-compliant - are you referring to the ECMAScript spec? AFAIK there's nothing like "magic comments" there, so I was thinking that they could be utilized somehow. Do you maybe have any suggestions as to what syntax would be good in mind? Is extending |
So the problem with an expression like
Please let me know if those are any good. If not, could I ask for some feedback on why those solutions are not ok? |
const importDocs = require.context('..', true, /\.docs\.js/, 'lazy');
importDocs.keys(); // sync, return an array of gathered paths
await importDocs(`./${path}.docs.js`); // async, resolves to the module Does this work?
|
It works (with |
In the meantime I learned a lot about how Webpack works and the final solution that I came up with was to add a custom loader that generates the code that I need. To ensure that caching and hot reloading work, the loader uses the I still think that having a built-in solution would be better, but as I was able to solve my original problem in a reasonable way, I'm closing this issue. |
@fatfisz what loader are you using? Can you share it (or its code)? I only found this and the dependencies aren't added: https://github.com/terpiljenya/import-glob Even this babel plugin doesn't help: https://github.com/novemberborn/babel-plugin-import-glob |
@bfred-it Here's my code: https://github.com/Codility/burdoc/blob/master/burdocImportsLoader.js The gist of it is I have a code like this: Admittedly I could've done a better job with |
Does |
Is it possible to get a list of all the files that could be loaded with an import expression (for instance |
@BlueNebulaDev I am wondering the same thing, did you find a solution? |
Feature request
What is the expected behavior?
I'd like webpack to set a specific id for the chunk generated for dynamic imports, something similar to this:
After that I could refer to the generated keys through an appropriate
require
call (I'm new to tinkering with webpack, so I'm not yet sure ifrequire.cache
is ok or not).What is motivation or use case for adding/changing the behavior?
Importing multiple files is supported in webpack through e.g.
require.context
andimport
.import
:require.cache
entries and try to get the right one, but feels hacky)require.context
:I want to get the list of all matched files (pages) so that I can reference them in the code of a navigation component.
I'd like this to be handled by webpack. After all, the "keys" method is there in the code, it's just not accessible like in the case of
require.context
.An alternative way would be to use a babel plugin or a macro like import-all.macro, but then the plugin would not be pure and that makes caching hard.
It seems that a few other people want a feature like this: #4807 (comment)
How should this be implemented in your opinion?
Reusing the existing feature of magic comments would be a good way to achieve that IMO.
Are you willing to work on this yourself?
Of course!
The text was updated successfully, but these errors were encountered: