2017-04-05 20 views
4

我使用電子構建器(16.6.2)將我的電子應用程序打包,其中包括keytar(3.0.2)作爲prod依賴項。使用電子應用程序包裝Keytar

的package.json文件包括:

"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" 
    ] 
} 

當我運行在同一系統上的。應用程序運行良好。當我嘗試在不同的系統上運行它(或刪除我的node_modules)時,它無法找到keytar.node。當建立keytar時,它會爲我的系統包含一個完全合格的映像。在控制檯中出現以下錯誤:

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 

我必須缺少構建過程中的一個步驟。

回答

2

事實證明,我在渲染過程中使用了keytar。我將keytar移動到主進程(不通過Webpack/Babel)並由電子構建器正確包裝。

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); 
}); 

然後從渲染過程中,我可以調用

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

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