How to share class definitions between Electron's main and renderer process?

Viewed 163

I would like to optimize the inter process communication of my Electron application by using arguments based on classes. For example: I would like to tell the renderer process to display a certain image together with additional information that my main process has fetched from some file (or something like that). I would then create an instance of a class such as

    class structuredArgument {
      var base64image;
      var additionalInformation1;
      var additionalInformation2;
      [...]
    }

    let instanceOfStructuredArgument = new structuredArgument();

I would then send that instance to the renderer process

    event.reply('display-image', instanceOfStructuredArgument);

If I were able to share the class definition above between the main and the renderer process, I could create an instance of it in the renderer process like this

    let instanceOfStructuredArgument = Object.assign(new structuredArgument(), arg);

This would allow me to check whether the argument is actually valid, to use the class' properties in a comfortable way, etc.

The problem is, however: I can include "classDefinitions.js" in the renderer process with a script tag in my index.html. Since the main process is "node based" I need to "export" my class definitions

    module.exports.structuredArgument = structuredArgument, etc.)

Unfortunately, this "module.exports" statement is invalid from the renderer process' perspective ("Uncaught ReferenceError: module is not defined").

Is there a "right" and well working "best practice" way of purposely sharing class definitions by the main and the renderer process of an Electron app to avoid redundancy and ensure that both processes are using the same class definitions?

Thank you very much for your support!

0 Answers
Related