5

I am creating an Electron app and am using electron-builder to package and build the app. Users are able to make plugins for the app (plugins can be node modules with their own dependencies and such). Everything is working fine except the part of the app for exposing the app's API to the plugin.

I created a module in the app for handling the plugins "Plugin-handler" that imports the plugin and also exposes the API to the plugin (The app's API is just a set of functions and it is bundled with the app).

The dilemma is that the user should be able to place a plugin anywhere on their machine and the app does not know the path before the build. Consequently, I excluded the "plugin-handler" module in the Electron-builder config so it does not bundle with the Webpack. Now I need to find the right way to expose the API to the plugin.

Here is how I am doing it now, to load the plugins and passing the API:

// In the Plugin-handler module
const API = require('api')
const plugin = require('path-to-plugin')( API ) 
  • path-to-plugin is added by the user in the app when they import their plugin.

As seen above, currently I pass the API to the plugin as an argument, which is not ideal, instead, I need a way to exposing the API module (or any other module that is bundled in the APP) to the plugin so users can access it in their plugin like below:

// In the plugin 
const { arg1, arg2,... } = require('api')

I've seen apps doing this, and allowing users to access their API in their plugins, but since I am new to all this, I may be doing things wrong, so please do be kind, and thank you for the help!

I drew a simple chart for better portraying the question: The app's structure

Bondsmith
  • 1,343
  • 3
  • 13
  • 27

0 Answers0