[英]Packaging different binaries per platform for Electron
我正在構建一個 Electron 應用程序,並創建了一個用 Go 編寫的小型守護程序二進制文件,該二進制文件將與應用程序一起打包為extraResource
。 但是,根據平台的不同,需要打包此二進制文件的不同版本,具體而言, agent.exe
的 agent.exe 和 MacOS 的沒有擴展名的agent
,以及 Linux 的文件名與 Mac 相同。
我在項目根目錄的bin
目錄中有這些二進制文件:
├── bin/
│ ├── windows/
│ │ └── agent.exe
│ ├── mac/
│ │ └── agent
│ ├── linux/
│ │ └── agent
測試 Windows,我包括bin/windows
目錄,它工作正常(如預期的那樣在資源目錄中的 exe 包)
這是我的 package.json 的相關片段
"build": {
"win": {
"icon": "build/app.ico",
"target": [
{
"target": "nsis",
"arch": [
"x64"
]
}
]
},
"nsis": {
"include": "build/installer.nsh",
"oneClick": false,
"allowToChangeInstallationDirectory": true,
"license": "build/eula.txt",
"installerIcon": "build/app.ico",
"artifactName": "...",
"shortcutName": "..."
},
"extraResources": [
"bin/windows/agent.exe"
]
}
是否有可能做到這一點? 就像現在一樣,我必須通過更改不同平台版本之間的extraResources
值來編輯 package.json 文件,這顯然是不理想的(因為我可能會忘記並通過忘記更改它來包含錯誤的二進制文件背部)。
請注意,我不支持 32 位 Windows,因此所有 Windows 構建都將包含相同的(64 位)二進制文件。
我正在使用電子生成器 19.50.0
將三個新目錄添加到您的build
目錄: win
、 mac
和linux
,就像我上面的示例文件夾結構(將windows
更改為win
),並添加所有特定於平台的二進制文件(以及任何其他需要與發行版一起打包的文件)進入各自的目錄。
然后將以下內容添加到package.json
的build
部分:
"extraResources": [
{
"from": "bin/${os}",
"to": "bin",
"filter": [
"**/*"
]
}
]
打包后,這些文件將添加到資源根目錄的bin
目錄中,因此以下文件和目錄會出現在resources
目錄中。 就我而言,對於內置於dist/win-unpacked/resources
中的 windows package(相對於項目根目錄)
app.asar.unpacked/
bin/
app.asar
electron.asar
elevate.exe
這樣,一旦您的應用程序安裝在目標系統上,就可以在運行時使用process.resourcesPath
訪問它:
const binaryFileName = process.platform == 'win32' ? 'binfile.exe' : 'binfile';
const binaryFile = path.join(process.resourcesPath, `bin/${binaryFileName}`);
...
spawn(binaryFile, [...args]);
歸功於 Stack Overflow 上的這篇文章。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.