[英]Webpack variably named requires
I'm writing a nodejs/react app with feature modules that will ideally be toggled on and off via a configuration.json file. 我正在编写一个具有功能模块的nodejs / react应用,理想情况下,这些功能可以通过configuration.json文件进行打开和关闭。 The feature modules have an entry point to establish server-side routes for their requests, and another to establish client-side routes within React.
功能模块有一个入口来为它们的请求建立服务器端路由,并在React中建立一个客户端路由。
The server-side configuration works just fine. 服务器端配置工作正常。 At boot-time of the server, it checks the configuration setting, loads the appropriate module, and calls its register() function, to register its server-side routes, controllers, etc. However, when trying to webpack this bundle for deployment to the client, I'm having issues trying to iterate over the list of modules to include, and have them webpacked in to the bundle being deployed.
在服务器启动时,它会检查配置设置,加载适当的模块,并调用其register()函数,以注册其服务器端路由,控制器等。但是,当尝试将Web捆绑包打包以部署到对于客户端,我在尝试遍历要包含的模块列表并将它们通过Web打包到要部署的捆绑包中时遇到问题。
I've done a lot of research so far, but webpack is still a little difficult to wade through. 到目前为止,我已经做了很多研究,但是webpack仍然很难理解。 The closest I've come was adding a section to my webpack.config.js file to preload the routes to the individual modules, include them in the bundle to have the code available on the client, and then stick their names into a FEATURE_MODULES global variable via DefinePlugin, and check for the existence of that within React, to try to load and register() those modules once they hit the browser.
我最近来的是在我的webpack.config.js文件中添加了一个部分,以将路由预加载到各个模块,将其包含在捆绑包中以在客户端上提供代码,然后将其名称粘贴到FEATURE_MODULES全局变量中通过DefinePlugin定义变量,并检查React中是否存在该变量,以在这些模块到达浏览器后尝试加载和注册()。
So far, the code seems to be added to the bundle, and the module paths are passed properly to the client in that global variable, but trying to require() them in on the client is throwing an error about the module not being found, most likely because webpack is replacing the module names with numbers for its lookup map. 到目前为止,该代码似乎已添加到捆绑软件中,并且模块路径已在该全局变量中正确传递给客户端,但是尝试在客户端上使用require()会引发有关找不到模块的错误,最有可能是因为webpack正在用其查找映射的数字替换模块名称。
I've also come across stuff about require.context and being able to RegExp these things, but that's where I'm starting to get lost. 我也遇到过有关require.context和RegExp这些东西的事情,但这就是我开始迷路的地方。 Can anybody provide me with an example or a link to something a little more easily-consumed, to demonstrate how to get this done?
有人可以给我提供示例或指向更易用的东西的链接,以演示如何完成此操作吗?
Not 100% clear on what you're doing that's breaking (maybe you can post specifics if the above doesn't help?), but general guidelines: 无法100%清楚您正在做的事情正在打破(如果上述方法无济于事,您可以发布具体细节吗?),但是要遵循一般准则:
The above should get you pretty much whatever you need. 以上内容将使您几乎需要任何东西。 And replacing file paths with numbers in requires is how webpack's runtime works, so if that's a problem you're probably going about something the wrong way.
并且用数字替换文件路径是webpack运行时的工作方式,因此,如果出现问题,则可能是错误的处理方式。
If it seems like you'll have to define too many flags via the define plugin, and/or set up too much conditional logic -- are these separate builds? 如果您似乎必须通过define插件定义太多标志,和/或设置太多条件逻辑,那么这些单独的构建吗? It's pretty easy to set up multiple builds w/a single webpack config file, and to reuse shared config between them.
使用单个webpack配置文件来设置多个构建,并在它们之间重用共享的配置是非常容易的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.