我希望能够在运行时配置哪些模块在浏览器中加载:
var moduleName = 'my-module'
var module = require(moduleName)
它似乎没有开箱即用,但也许有某种方式?
答案 0 :(得分:0)
是的,有很多方法
Webpack热模块替换功能
Webpack Docs的示例
var requestHandler = require("./handler.js");
var server = require("http").createServer();
server.on("request", requestHandler);
server.listen(8080);
// check if HMR is enabled
if(module.hot) {
// accept update of dependency
module.hot.accept("./handler.js", function() {
// replace request handler of server
server.removeListener("request", requestHandler);
requestHandler = require("./handler.js");
server.on("request", requestHandler);
});
}
对于方法,请查看此答案
How do I include a JavaScript file in another JavaScript file?