我正在开发一个电子应用程序,其中使用外部 .exe 文件。在开发模式下应用程序运行良好。 但是当我打包应用程序时,却找不到那些.exe文件。
如何在打包的应用程序中包含 .exe 文件。我正在使用 electronics-builder 进行包装。
以下是我的配置,
"build": {
"appId": "com.f.quickstart",
"files": [
"app/build/**/*",
"app/build/**/*.exe",
"main.js",
"mainIPC.js",
"src/**/*",
"dist/**/*",
"node_modules/**/*",
"package.json"
],
"win": {
"target": "portable",
"signAndEditExecutable": false
},
"linux": {
"target": "AppImage"
},
"mac": {
"target": "zip"
}
}
寻求帮助。
谢谢
请注意,我正在使用:
electron ^12.0.0
、electron-builder ^23.6.0
和Windows 11
。
如果您只需要安装程序中的 exe,则可以使用
resources
目录。例如,当您想要在应用程序安装期间运行安装依赖项的 exe 时,这很有用。
这是使用 nsis 安装程序的示例:
resources
目录。build
规则中添加您需要的所有内容:// package.json
{
...
"build": {
"appId": "mycompany.MyProductName",
"productName": "MyProductName",
"directories": {
"buildResources": "resources"
},
"nsis": {
"oneClick": false,
"perMachine": true
},
"win": {
"target": "nsis"
},
"extends": null
}
}
在新的
vendor
目录中创建一个 resources
目录。
将 exe 拖到
vendor
目录中。
在
installer.nsh
目录下创建一个名为resources
的文件,并写入如下内容:
!define vendordir "${BUILD_RESOURCES_DIR}\vendor"
!define myexe_64 "myexe.exe"
!define myexe "myexe_x86.exe"
!macro customInstall
${If} ${RunningX64}
ExecWait '"${vendordir}\${myexe_64}"'
${Else}
ExecWait '"${vendordir}\${myexe}"'
${EndIf}
!macroend
dist
命令。在运行时使用 exe 是一种常见的用例,老实说,我很惊讶我能找到的相关文档如此之少。
这是我如何做到这一点的示例:
在项目中的任意位置创建一个名为
extras
的目录。我把我的放在src/main/extras
。
在您的
extraFiles
规则中添加 build
条目:
// package.json
{
...
"build": {
"extraFiles": [
{
"from": "src/main/extras",
"to": "extras",
"filter": [
"**/*"
]
}
],
}
}
// preload.js
const { remote } = require('electron');
const path = require('path');
const isDev = require('electron-is-dev');
const appPath = remote.app.getAppPath();
const extrasPath = isDev ?
path.join(appPath, 'src', 'main', 'extras') :
path.join(appPath, '..', '..', 'extras');
// We finally have our exe!
const exePath = path.join(extrasPath, 'myexe.exe');
Electron Builder 配置文档仍然是了解详细信息的良好资源。
希望这有帮助!