如何在Electron应用程序中打包外部.exe文件

问题描述 投票:0回答:1

我正在开发一个电子应用程序,其中使用外部 .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 desktop-application electron-builder electron-packager
1个回答
0
投票

请注意,我正在使用:

electron ^12.0.0
electron-builder ^23.6.0
Windows 11

安装过程中

如果您只需要安装程序中的 exe,则可以使用

resources
目录。例如,当您想要在应用程序安装期间运行安装依赖项的 exe 时,这很有用。

这是使用 nsis 安装程序的示例:

  1. 在根目录中创建一个
    resources
    目录。
  2. build
    规则中添加您需要的所有内容:
// package.json

{
...

  "build": {
    "appId": "mycompany.MyProductName",
    "productName": "MyProductName",
    "directories": {
      "buildResources": "resources"
    },
    "nsis": {
      "oneClick": false,
      "perMachine": true
    },
    "win": {
      "target": "nsis"
    },
    "extends": null
  }
}
  1. 在新的

    vendor
    目录中创建一个
    resources
    目录。

  2. 将 exe 拖到

    vendor
    目录中。

  3. 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
  1. 运行适用于 Windows 的 electro builder
    dist
    命令。

运行时

在运行时使用 exe 是一种常见的用例,老实说,我很惊讶我能找到的相关文档如此之少。

这是我如何做到这一点的示例:

  1. 在项目中的任意位置创建一个名为

    extras
    的目录。我把我的放在
    src/main/extras

  2. 在您的

    extraFiles
    规则中添加
    build
    条目:

// package.json

{
...

  "build": {
    "extraFiles": [
      {
        "from": "src/main/extras",
        "to": "extras",
        "filter": [
          "**/*"
        ]
      }
    ],
  }
}
  1. 您现在可以使用应用程序的路径来引用exe,但您需要根据开发和生产更改路径。为此,我们可以使用 electron-is-dev:
// 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 配置文档仍然是了解详细信息的良好资源。

希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.