简体   繁体   English

Webpack /电子要求动态模块

[英]Webpack/electron require dynamic module

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. 因此,我希望用户能够将JavaScript文件添加到已经编译的电子/ webpack应用程序中,并加载并执行我的应用程序。 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. 我试过要求plugins /文件夹中的每个文件,但事实证明,编译时它只是被捆绑到bundle.js中,我希望能够像编译插件一样在编译后对其进行更改。 How can I accomplish this? 我该怎么做?

I think what you're looking for is global.require as stated in this similar question . 我认为您正在寻找的是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. 请注意,由于它是Node的require ,它将缓存必需的模块,因此修改插件的代码将不起作用,直到您重新启动电子应用程序,使其再次调用global.require为止。 If that is an issue, you can force-reload a specific module with this (unrecommended) snippet: 如果这是一个问题,您可以使用以下(不推荐)代码段强制重新加载特定模块:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM