使用Electron应用程序打包Keytar

使用Electron应用程序打包Keytar

本文介绍了使用Electron应用程序打包Keytar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用电子生成器(16.6.2)打包我的电子应用程序,其中包括keytar(3.0.2)作为产品依赖项.

I'm using electron-builder (16.6.2) to package my electron application which includes keytar (3.0.2) as a prod dependency.

package.json文件包括:

package.json file includes:

"scripts": {
    "postinstall": "install-app-deps",
    "compile:dev": "webpack-dev-server --hot --host 0.0.0.0 --config=./webpack.dev.config.js",
    "compile": "webpack --config webpack.build.config.js",
    "dist": "yarn compile && build"
},
"build": {
    "appId": "com.myproject",
    "asar": true,
    "files": [
      "bin",
      "node_modules",
      "main.js"
    ]
}

当我在同一系统上运行.app时,它运行正常.当我尝试在其他系统上运行它(或删除我的node_modules)时,它找不到keytar.node.构建keytar时,它包含我的系统到该映像的标准路径.我在控制台中收到以下错误:

When I run the .app on the same system it runs fine. When I try running it on a different system (or deleting my node_modules) it fails to find keytar.node. When keytar is built, it includes a fully qualified path to that image for my system. I get the following error in the console:

Uncaught Error: Cannot open /Users/Kevin/Work/myproject/node_modules/keytar/build/Release/keytar.node
Error: dlopen(/Users/Kevin/Work/myproject/node_modules/keytar/build/Release/keytar.node,
1): image not found

我必须在构建过程中缺少一步.

I must be missing a step in the build process.

推荐答案

事实证明,我在渲染器过程中使用keytar.我将keytar移到了主要流程中(该流程未通过Webpack/Babel进行处理),并被电子生成器正确打包.

As it turns out, I was using keytar in the renderer process. I moved keytar into the main process (which doesn't go through Webpack / Babel) and gets packed correctly by electron-builder.

main.js

ipcMain.on('get-password', (event, user) => {
    event.returnValue = keytar.getPassword('ServiceName', user);
});

ipcMain.on('set-password', (event, user, pass) => {
    event.returnValue = keytar.replacePassword('ServiceName', user, pass);
});

然后我可以在渲染器过程中调用

Then from the renderer process I can call

const password = ipcRenderer.sendSync('get-password', user);

ipcRenderer.sendSync('set-password', user, pass);

这篇关于使用Electron应用程序打包Keytar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 02:09