我之前用过Wails V2,虽然它做得很好。但它有点有限。我切换到 Electron。
我有类似的设置。 Go 中的后端服务器和 Svelte 中的前端。我使用 HTTP 服务器而不是 gRPC。芽过程是一样的。
因此,在将您的应用程序捆绑到电子中时。您需要将后端的二进制文件添加到其他资源中。
然后,从前端启动并连接到 grpc 后端服务器。使用https://github.com/grpc/grpc-node。像这样
const spawn = require('child_process').execFile;
let binaryPath = process.resourcesPath + "/app.asar.unpacked/bin/macos/myapp"
// run server
function startBackend() {
child = spawn(binaryPath, ['serve', "-p", port, "-s", userDataPath]);
}
在我的例子中,我使用电子生成器 (https://www.electron.build/) 来构建我的应用程序。
您可以使用extraResources 指令将您的后端二进制文件添加到电子应用程序。
https://www.electron.build/configuration/contents.html#extraresources
或
binaries: ["bin/windows/app.exe"]
asarUnpack: ["bin/windows/app.exe"]
我的应用适用于 MacOS,electron-builder.yaml 看起来像这样。
productName: MyApp
appId: com.electron.${name}
remoteBuild: false
compression: normal
npmRebuild: true
asar:
smartUnpack: true
directories:
output: out
buildResources: build
mac:
category: public.app-category.developer-tools
icon: ./icons/512x512.png
darkModeSupport: false
target:
- target: dmg
# - target: zip
fileAssociations:
- ext: svg
role: Viewer
# extraResources:
# - from: "bin/macos/myapp"
# to: "bin/macos/myapp"
# filter:
# - "**/*"
binaries: ["bin/macos/myapp"]
asarUnpack: ["bin/macos/myapp"]
hardenedRuntime: false
gatekeeperAssess: false
entitlements: "build/macos/entitlements.mac.plist"
entitlementsInherit: "build/macos/entitlements.mac.plist"
publish: null
dmg:
sign: true
电子生成器配置示例:https://github.com/twiny/svelte-electron-tailwind/blob/main/electron-builder.yml