bundling precompiled binary into electron app

后端 未结 6 1691
无人共我
无人共我 2020-11-28 22:21

Is there a good solution on how to include third party pre compiled binaries like imagemagick into an electron app? there are node.js modules but they are all wrappers or na

6条回答
  •  春和景丽
    2020-11-28 23:02

    Heavily based on Ganesh's answer, but simplified somewhat. Also I am using the Vue CLI Electron Builder Plugin so the config has to go in a slightly different place.

    1. Create a resources directory. Place all your files in there.
    2. Add this to vue.config.js:
    module.exports = {
      pluginOptions: {
        electronBuilder: {
          builderOptions: {
            ...
            "extraResources": [
              {
                "from": "resources",
                "to": ".",
                "filter": "**/*"
              }
            ],
            ...
          }
        }
      }
    }
    
    1. Create a file called resources.ts in your src folder, with these contents:
    import path from 'path';
    import { remote } from 'electron';
    
    // Get the path that `extraResources` are sent to. This is `/Resources`
    // on macOS. remote.app.getAppPath() returns `/Resources/app.asar` so
    // we just get the parent directory. If the app is not packaged we just use
    // `/resources`.
    export const resourcesPath = remote.app.isPackaged ?
                                 path.dirname(remote.app.getAppPath()) :
                                 path.resolve('resources');
    

    Note I haven't tested this on Windows/Linux but it should work assuming app.asar is in the resources directory on those platforms (I assume so).

    1. Use it like this:
    import { resourcesPath } from '../resources'; // Path to resources.ts
    
    ...
        loadFromFile(resourcesPath + '/your_file');
    

提交回复
热议问题