Webpack/电子要求动态模块 [英] Webpack/electron require dynamic module

查看:66
本文介绍了Webpack/电子要求动态模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文件夹上需要一个模块作为插件.因此,我希望用户能够将JavaScript文件添加到已经编译的电子/webpack应用程序中,并加载并执行我的应用程序.因此,这就像一个插件系统.我试过要求plugins/文件夹中的每个文件,但事实证明,编译时它只是被捆绑到bundle.js中,我希望能够像编译插件一样在编译后对其进行更改.我该怎么做?

I'd like to require a module on a folder, as a plugin. So I want the user to be able to add JavaScript files into an already compiled electron/webpack application and having my application load and execute it. So it would be like a plugin system. I have tried requiring every file inside the plugins/ folder but it turns out that it just gets bundled into bundle.js when compiled, and I want to be able to change it after compiled, like a plugin. How can I accomplish this?

推荐答案

我认为您正在寻找的是 ="https://stackoverflow中所述的 global.require .com/a/37349344/1423359>这个类似的问题.

I think what you're looking for is global.require as stated in this similar question.

请注意,由于它是Node的 require ,它将缓存所需的模块,因此修改插件的代码将不起作用,直到您重新启动电子应用程序,以便它调用 global.require .如果有问题,您可以使用以下(不推荐的)代码片段强制重新加载特定模块:

Note that as it's Node's require, it will cache required module, so modifying a plugin's code will not have effect until you restart your electron application so that it does call global.require again. If that is an issue, you can force-reload a specific module with this (unrecommended) snippet:

delete global.require.cache[global.require.resolve(moduleName)]

这篇关于Webpack/电子要求动态模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆