using ZeroMQ with Electron 22 and Electron-Forge

31 Views Asked by At

I am trying to use ZeroMQ (0MQ) inside an Electron application: npm add [email protected] and [email protected] and @electron-forge/[email protected]

On my local machine, for development, everything works as long as I declare the module as external in my webpack configuration. Alternatively, I also need to exclude zeromq from the rebuild process on my Mac M1 (that's another story):

// webpack.main.config.js
module.exports = {
    // ...
    externals: {
        'zeromq': 'commonjs zeromq'
    },
    // ...
}
// forge.config.js
module.exports = {
    // ...
    rebuildConfig: {
        onlyModules: "axios", // prevent rebuilding on Mac as headers are missing
    },
    // ...
}

But in order to package my application and distribute it, I found no other alternative than a hook to copy zeromq module into the destination folder. This seems brittle, and inefficient at best. Is there another solution to have Electron Forge handle external modules gracefully?

// forge.config.js
const fs = require('fs');
const path = require('path');

module.exports = {
  packagerConfig: {
    extraResource: [
      "./node_modules/zeromq/",
      "./node_modules/@aminya/",
    ],
    afterCopyExtraResources: [
      (bPath, eVer, platform, arch, done) => {
        let src ;
        let dst ;
        for (const m of ["zeromq", "@aminya"]) {
          if (platform === "darwin") {
            src = path.join(bPath, "jelo-ui.app", "Contents", "Resources", m)
            dst = path.join(bPath, "jelo-ui.app", "Contents", "Resources", "app", "node_modules", m)

          } else {
            src = path.join(bPath, "resources", m)
            dst = path.join(bPath, "resources", "app", "node_modules", m)
          }
          fs.renameSync(src, dst)
        }
        done();
      }
    ]
  },
// ...
}
0

There are 0 best solutions below