本文档详细说明 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/ 子目录。
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.dllbuild-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
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"
}
}
# 方式一:使用 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 │
└────────────────────────────────────────────────────────────────┘
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 使用
# ... 其他依赖
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
}]
}
}
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 安装包(可选)
cd client-tauri
# 方法一:使用提供的脚本
.\build-release.ps1
# 方法二:手动执行
npm install
$env:OPENSSL_STATIC = "1"
$env:RUSTFLAGS = "-C target-feature=+crt-static"
npm run tauri:build:windows
# 查找生成的 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 的核心流程:
dist/ 目录target/release/ 目录dist/ 嵌入 Rust 二进制关键点:
OPENSSL_STATIC=1) - 避免依赖外部 DLLcrt-static) - 避免依赖 VCRUNTIME