BUILD-EXE-GUIDE.md 12 KB

Tauri 项目打包 EXE 流程说明

本文档详细说明 client-tauri 项目如何将前端 + Rust 后端打包成 Windows 可执行文件(.exe)。


一、技术架构概览

┌─────────────────────────────────────────────────────────────┐
│                      client-tauri                           │
├─────────────────────────────────────────────────────────────┤
│  前端 (Frontend)         │         后端 (Tauri/Rust)         │
│  ─────────────          │         ────────────────          │
│  Vue 3 + Vite           │         Tauri 1.5                  │
│  Element Plus / AntD    │         Rust 1.60+                 │
│  TypeScript              │         OpenSSL (静态链接)        │
│  Pinia (状态管理)         │         reqwest (HTTP)           │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
                    ┌─────────────────────┐
                    │   Windows EXE       │
                    │   (单文件/安装包)     │
                    └─────────────────────┘

二、打包方式

Tauri 提供两种打包方式:

方式 命令 输出位置 说明
独立 EXE npm run tauri:build:windows src-tauri/target/release/*.exe 单个可执行文件
NSIS 安装包 npm run tauri:build src-tauri/target/release/bundle/nsis/*setup.exe 带安装向导的安装包

三、打包脚本详解

项目提供了多个 PowerShell 打包脚本,位于 client-tauri/ 根目录和 scripts/ 子目录。

3.1 核心环境配置脚本

scripts/set-openssl-static-env.ps1 - OpenSSL 静态链接环境设置

# 设置 OpenSSL 静态库路径
$opensslRoot = "C:\Program Files\OpenSSL-Win64"
$staticOnlyDir = Join-Path $opensslRoot "lib\VC\x64\static-only"

# 复制 MT 静态库
Copy-Item $cryptoStatic (Join-Path $staticOnlyDir "libcrypto.lib") -Force
Copy-Item $sslStatic (Join-Path $staticOnlyDir "libssl.lib") -Force

# 关键环境变量
$env:OPENSSL_DIR = $opensslRoot
$env:OPENSSL_LIB_DIR = $staticOnlyDir
$env:OPENSSL_INCLUDE_DIR = Join-Path $opensslRoot "include"
$env:OPENSSL_STATIC = "1"           # 启用静态链接
$env:RUSTFLAGS = "-C target-feature=+crt-static"  # 静态链接 C 运行时库

为什么要静态链接 OpenSSL?

  • 避免目标机器缺少 libcrypto-3-x64.dll / libssl-3-x64.dll
  • 特别是 Windows Server 2019 等老旧系统

3.2 主打包脚本

build-release.ps1 - 推荐使用

完整的 Release 构建脚本,包含检查和输出:

# 1. 设置静态链接环境
. (Join-Path $ProjectRoot "scripts\set-openssl-static-env.ps1")

# 2. 设置 API 地址
$env:DEFAULT_BACKEND_API_URL = "http://125.122.32.114:9000"

# 3. 安装前端依赖
npm install --silent

# 4. 执行 Tauri 构建
npm run tauri build

# 5. 查找并报告输出文件
$exe = Get-ChildItem "src-tauri\target\release" -Filter "*.exe" ...
$nsisFiles = Get-ChildItem -Path "src-tauri\target\release\bundle\nsis" -Filter "*setup.exe" ...

build-prod.ps1 - 生产环境构建

# 与 build-release 类似的配置,用于生产环境
. (Join-Path $PSScriptRoot "scripts\set-openssl-static-env.ps1")
$env:DEFAULT_BACKEND_API_URL = "http://125.122.32.114:9000"
npm run tauri build

build-windows.ps1 - 带中文命名的构建

# 执行构建后,将 EXE 复制并重命名为中文名
$destName = 'BUFF选品-STEAM扫货(游铛铛).exe'
Copy-Item -Path $foundExe.FullName -Destination $destPath -Force

四、打包命令详解

4.1 npm scripts(package.json)

{
  "scripts": {
    "build": "vite build",                    // 构建前端
    "tauri:build": "tauri build",             // 构建 Tauri(包含 NSIS)
    "tauri:build:windows": "tauri build --target x86_64-pc-windows-msvc",  // 仅 Windows
    "tauri:build:prod": "set DEFAULT_BACKEND_API_URL=... && tauri build",
    "nsis:pack": "powershell -ExecutionPolicy Bypass -File src-tauri/pack-nsis.ps1"
  }
}

4.2 常用打包命令

# 方式一:使用 npm(需要先 cd 到 client-tauri 目录)
cd client-tauri
npm run tauri:build:windows

# 方式二:使用 PowerShell 脚本(推荐)
cd client-tauri
.\build-release.ps1

# 方式三:设置 API 后构建
$env:DEFAULT_BACKEND_API_URL="http://125.122.32.114:9000"
npm run tauri:build

五、打包核心步骤(npm run tauri build 内部流程)

┌────────────────────────────────────────────────────────────────┐
│                     npm run tauri build                         │
└────────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌────────────────────────────────────────────────────────────────┐
│ Step 1: npm run build (前端构建)                                │
│ ─────────────────────────────────────────────────────────────  │
│ 执行 vite build,将 Vue/TypeScript 代码编译为静态文件            │
│ 输出目录: client-tauri/dist/                                   │
└────────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌────────────────────────────────────────────────────────────────┐
│ Step 2: Cargo Build (Rust 后端编译)                             │
│ ─────────────────────────────────────────────────────────────  │
│ • 读取 src-tauri/Cargo.toml                                    │
│ • 编译 Rust 代码(使用 MSVC 工具链)                            │
│ • 链接 Tauri 框架库                                            │
│ • 链接 OpenSSL(静态链接,无需 DLL)                            │
│ • 输出: src-tauri/target/release/your-app-name.exe            │
└────────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌────────────────────────────────────────────────────────────────┐
│ Step 3: Bundle (资源打包)                                       │
│ ─────────────────────────────────────────────────────────────  │
│ • 将前端 dist/ 嵌入 EXE                                        │
│ • 复制图标文件                                                  │
│ • 生成 NSIS 安装包(可选)                                      │
│ • 输出:                                                        │
│   - src-tauri/target/release/your-app-name.exe                │
│   - src-tauri/target/release/bundle/nsis/*setup.exe            │
└────────────────────────────────────────────────────────────────┘

六、关键配置文件

6.1 src-tauri/Cargo.toml - Rust 依赖配置

[dependencies]
tauri = { version = "1.5", features = ["window-close", "dialog-all", "fs-*", "shell-open", ...] }
reqwest = { version = "0.11", features = ["json", "socks", "gzip"] }
openssl = "0.10"  # 必须配合 OPENSSL_STATIC=1 使用
# ... 其他依赖

6.2 src-tauri/tauri.conf.json - Tauri 配置

{
  "build": {
    "beforeDevCommand": "npm run dev",
    "beforeBuildCommand": "npm run build",  // 构建前端
    "distDir": "../dist",                    // 前端构建输出
    "devPath": "http://localhost:5173"
  },
  "package": {
    "productName": "steamSH",               // 应用名称
    "version": "0.1.0"
  },
  "tauri": {
    "bundle": {
      "active": true,
      "targets": "all",                     // 生成所有平台安装包
      "identifier": "com.yourcompany.buff-client",
      "icon": ["icons/icon.ico"],
      "windows": {
        "nsis": {                           // NSIS 安装器配置
          "languages": ["SimpChinese", "English"],
          "displayLanguageSelector": true
        }
      }
    },
    "windows": [{
      "title": "BUFF选品-STEAM扫货(游铛铛)",
      "width": 1200,
      "height": 800
    }]
  }
}

6.3 src-tauri/.cargo/config.toml - Cargo 构建配置

[env]
# 跳过 Playwright 浏览器下载
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD = "1"

[target.x86_64-pc-windows-msvc]
# 静态链接 C 运行时库,避免目标机缺少 VCRUNTIME140_1.dll
rustflags = ["-C", "target-feature=+crt-static"]

七、输出文件位置

client-tauri/
└── src-tauri/target/release/
    ├── your-app-name.exe              # 独立可执行文件 (~10-30 MB)
    │
    └── bundle/
        └── nsis/
            ├── your-app-name_x.x.x_x64-setup.exe   # NSIS 安装包
            └── your-app-name.msi                   # MSI 安装包(可选)

八、快速开始

8.1 环境要求

  • Node.js 16+
  • Rust 1.60+
  • Visual Studio Build Tools (MSVC)
  • OpenSSL-Win64 (从 slproweb.com 下载 FULL 版本)

8.2 一键打包

cd client-tauri

# 方法一:使用提供的脚本
.\build-release.ps1

# 方法二:手动执行
npm install
$env:OPENSSL_STATIC = "1"
$env:RUSTFLAGS = "-C target-feature=+crt-static"
npm run tauri:build:windows

8.3 验证输出

# 查找生成的 EXE
Get-ChildItem -Path "src-tauri\target\release" -Filter "*.exe" -Recurse

# 查看文件大小
Get-ChildItem -Path "src-tauri\target\release" -Filter "*.exe" -Recurse |
    Select-Object FullName, @{N='Size(MB)';E={[math]::Round($_.Length/1MB,2)}}

九、常见问题

问题 解决方案
缺少 libcrypto-3-x64.dll 运行 scripts/set-openssl-static-env.ps1 设置静态链接
缺少 VCRUNTIME140_1.dll 确保 .cargo/config.toml 设置了 crt-static
构建太慢 使用 npm run tauri:build:windows 只构建 Windows 版本
NSIS 安装失败 检查 src-tauri/nsis/ 目录下的模板文件

十、总结

Tauri 打包 EXE 的核心流程:

  1. 前端构建:Vite 编译 Vue 代码 → dist/ 目录
  2. 后端编译:Cargo 编译 Rust 代码 → target/release/ 目录
  3. 资源嵌入:Tauri CLI 将前端 dist/ 嵌入 Rust 二进制
  4. 打包发布:生成独立 EXE 或 NSIS 安装包

关键点:

  • OpenSSL 静态链接 (OPENSSL_STATIC=1) - 避免依赖外部 DLL
  • C 运行时静态链接 (crt-static) - 避免依赖 VCRUNTIME
  • NSIS 安装器 - 提供友好的 Windows 安装体验