We're trying to improve our build system to rely less on relative path imports, and one area we need to work on this for is image loading. So we're trying to do something like this, loading the images in with Electron's nativeImage
:
import { nativeImage } from 'electron';
import ourImage from 'our-images/src/ourImage.png'
console.log(ourImage) // Outputs 14ad9a9efa18beb2868a.png
const trayIcon = nativeImage.createFromPath(ourImage); // Blank icon appears - no error message
const trayIcon = nativeImage.createFromBuffer(ourImage); // App crashes: "Error: buffer must be a node Buffer" - even if we load the file using `type: asset/source` -- https://webpack.js.org/guides/asset-modules/
Is there some way to use this pattern in Electron with Webpack? Or are we forced to use require()
and relative paths? We had originally tried require.resolve()
to use a path instead of creating a nativeImage
, but unfortunately it doesn't work consistently in the packaged app.