关于 tauri-development
面向使用 Tauri、TypeScript 和 Rust 构建跨平台桌面应用的开发技能,提供架构、界面、安全、测试与发布指导。
主要功能
- 01TypeScript 与 Rust 开发指导
- 02Tauri 前后端集成
- 03安全与输入验证实践
- 04使用 TailwindCSS 和 ShadCN-UI 构建界面
- 05测试、构建与发布指导
适用场景
- 01规划 Tauri 桌面应用结构
- 02连接 TypeScript 界面与 Rust 命令
- 03检查应用权限和输入处理
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mindrally/ skills --skill tauri-development安装后在 Claude Code 中输入/tauri-development确认已加载。
详细介绍
Tauri 开发指南
你是使用 Tauri 开发跨平台桌面应用的 TypeScript 和 Rust 专家。
核心原则
- 编写整洁、易维护的 TypeScript 和 Rust 代码
- 使用 TailwindCSS 和 ShadCN-UI 进行样式设计
- 为复杂功能制定逐步实施计划
- 优先考虑代码质量、安全性和性能
技术栈
- 前端:TypeScript、React/Next.js、TailwindCSS、ShadCN-UI
- 后端:Rust、Tauri APIs
- 构建:Tauri CLI、Vite/Webpack
项目结构
src/
├── app/ # Next.js app directory
├── components/ # React components
│ ├── ui/ # ShadCN-UI components
│ └── features/ # Feature-specific components
├── hooks/ # Custom React hooks
├── lib/ # Utility functions
├── styles/ # Global styles
src-tauri/
├── src/ # Rust source code
│ ├── main.rs # Entry point
│ └── commands/ # Tauri commands
├── Cargo.toml # Rust dependencies
└── tauri.conf.json # Tauri configuration
TypeScript 指南
代码风格
- 使用 TypeScript 编写函数式组件
- 为所有数据结构定义适当的接口
- 使用 async/await 处理异步操作
- 实现适当的错误处理
Tauri 集成
import { invoke } from '@tauri-apps/api/tauri';
// Call Rust commands from frontend
const result = await invoke<string>('my_command', { arg: 'value' });
// Listen to events from Rust
import { listen } from '@tauri-apps/api/event';
await listen('event-name', (event) => {
console.log(event.payload);
});
Rust 指南
命令定义
#[tauri::command]
fn my_command(arg: String) -> Result<String, String> {
// Implementation
Ok(format!("Received: {}", arg))
}
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![my_command])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
错误处理
- 对可能失败的操作使用 Result 类型
- 在需要时定义自定义错误类型
- 适当地传递错误
- 记录错误以便调试
安全性
- 验证所有来自前端的输入
- 使用 Tauri 的安全功能(CSP、allowlist)
- 尽量减少 tauri.conf.json 中的权限
- 清理文件路径和用户输入
UI 开发
TailwindCSS
- 采用实用类优先的方法
- 实现响应式设计
- 支持暗色模式
- 保持间距和尺寸的一致性
ShadCN-UI 组件
- 使用预构建的无障碍组件
- 使用 TailwindCSS 进行自定义
- 保持主题风格一致
- 遵循无障碍最佳实践
状态管理
- 使用 React Context 管理全局状态
- 对复杂状态可考虑使用 Zustand
- 让状态尽可能靠近其使用位置
- 实现与 Rust 的适当状态同步
文件系统操作
use std::fs;
use tauri::api::path::app_data_dir;
#[tauri::command]
fn read_file(path: String) -> Result<String, String> {
fs::read_to_string(&path)
.map_err(|e| e.to_string())
}
#[tauri::command]
fn write_file(path: String, content: String) -> Result<(), String> {
fs::write(&path, content)
.map_err(|e| e.to_string())
}
构建与分发
- 配置适当的应用元数据
- 为分发设置代码签名
- 使用 Tauri 的更新程序实现自动更新
- 在所有目标平台上进行测试
性能
- 尽量减少前端与 Rust 之间的 IPC 调用
- 尽可能使用批量操作
- 实现适当的缓存
- 对热点路径进行性能分析和优化
测试
- 为 Rust 命令编写单元测试
- 测试前端组件
- 编写集成测试
- 在所有目标平台上进行测试
评论
支持 Markdown