📋 环境准备
通用依赖
- Node.js 18+ (推荐使用 pnpm/yarn/bun)
- Rust 最新稳定版
bash
# 安装 Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# 验证安装
rustc --version
cargo --version
平台特定依赖
| 平台 | 依赖 |
|---|---|
| macOS | Xcode Command Line Tools |
| Windows | Microsoft Visual Studio C++ Build Tools |
| Linux | webkit2gtk, openssl, 等 |
🚀 创建项目
bash
# 使用 npm
npm create tauri-app@latest
# 使用 pnpm
pnpm create tauri-app
# 使用 yarn
yarn create tauri-app
# 使用 bun
bun create tauri-app
创建过程会提示选择:
- 项目名称
- 前端框架(React/Vue/Svelte/等)
- 包管理器
📁 项目结构
text
my-app/
├── src/ # 前端代码
│ ├── App.tsx
│ └── main.tsx
├── src-tauri/ # Rust 后端
│ ├── src/
│ │ ├── main.rs # 主入口
│ │ └── lib.rs # 应用逻辑
│ ├── tauri.conf.json # Tauri 配置
│ └── Cargo.toml # Rust 依赖
├── package.json
└── vite.config.ts
💻 开发模式
bash
# 进入项目目录
cd my-app
# 安装依赖
npm install
# 启动开发服务器
npm run tauri dev
开发模式特性:
- 前端热重载
- Rust 代码自动重编译
- 开发者工具可用
- 控制台日志输出
📦 打包发布
bash
# 构建生产版本
npm run tauri build
# 输出位置
# macOS: src-tauri/target/release/bundle/
# Windows: src-tauri/target/release/
# Linux: src-tauri/target/release/bundle/
输出格式
| 平台 | 输出格式 |
|---|---|
| macOS | .app, .dmg |
| Windows | .exe, .msi |
| Linux | .deb, .AppImage |
⚙️ 配置文件
json
// src-tauri/tauri.conf.json
{
"productName": "My App",
"version": "1.0.0",
"identifier": "com.myapp.app",
"build": {
"beforeBuildCommand": "npm run build",
"beforeDevCommand": "npm run dev",
"devUrl": "http://localhost:5173"
},
"app": {
"windows": [
{
"title": "My App",
"width": 800,
"height": 600
}
]
}
}