Is it possible to use dynamic require
and require.context
with explicit loaders in the require statement? I'd like to be able to do something like this, but it's not working for me:
var req = require.context('../somedir', false, /\.js$/);
var imported = req('my-loader!' + someModulePath); // someModulePath defined above somewhere
When I try this, I get a 'module not found' error that makes it seem like webpack is treating the my-loader!
part of the string as the start of a file path, but I want my-loader!
to be recognized as a loader, as described here: https://webpack.github.io/docs/using-loaders.html#loaders-in-require
Loaders are run only once at compile-time, which means after your require.context
is compiled, it's just pure Javascript. You can write it like this:
var req = require.context("my-loader!../somedir", false, /\.js$/);
var imported = req(someModulePath);
The function returned by require.context
is evaluated at run-time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.