← 中文资源

简体中文版本

Tauri 桌面应用模板

使用 React 前端、Rust 后端和安全 IPC 命令的 Tauri 2 桌面应用模板,二进制体积小。

阅读英文原文

模板说明

该模板用 Tauri 构建跨平台桌面应用:前端使用系统 WebView,原生能力由 Rust 暴露,安装包比 Electron 更小。

包含内容

包含 React 前端、Rust 命令、权限配置、构建脚本和跨平台打包配置。

使用建议

只暴露必要命令并校验参数,禁止把任意路径或 shell 命令传给后端。配置 CSP 和允许访问的域。自动更新必须签名并验证来源;文件系统访问遵循最小权限。

验证方式

在 Windows、macOS 和 Linux 构建安装包,测试文件访问、IPC、权限拒绝和更新;检查崩溃日志与卸载行为。确认 WebView 版本差异不会破坏界面。

交付检查

使用该模板初始化项目后,先运行安装、类型检查、测试与生产构建;再按模板说明逐项核对环境变量、权限边界、错误处理和关闭流程。把实际命令输出保留为交付证据,不要只依据 README 声明。

代码示例

package.json
src-tauri/Cargo.toml
src-tauri/src/main.rs
src/App.tsx
src-tauri/tauri.conf.json
{
  "name": "tauri-desktop-app",
  "version": "0.1.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "tsc && vite build",
    "tauri": "tauri"
  },
  "dependencies": {
    "@tauri-apps/api": "^2.0.0",
    "react": "^19.0.0",
    "react-dom": "^19.0.0"
  },
  "devDependencies": {
    "@tauri-apps/cli": "^2.0.0",
    "typescript": "^5.6.0",
    "vite": "^6.0.0",
    "@vitejs/plugin-react": "^4.3.0"
  }
}
[package]
name = "tauri-desktop-app"
version = "0.1.0"
edition = "2021"

[build-dependencies]
tauri-build = { version = "2", features = [] }

[dependencies]
tauri = { version = "2", features = [] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"

[features]
custom-protocol = ["tauri/custom-protocol"]
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use serde::Serialize;

#[derive(Serialize)]
struct Greeting {
    message: String,
}

#[tauri::command]
fn greet(name: &str) -> Greeting {
    Greeting {
        message: format!("Hello, {}! Welcome to Tauri.", name),
    }
}

fn main() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![greet])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
import { useState } from 'react';
import { invoke } from '@tauri-apps/api/core';

function App() {
  const [greeting, setGreeting] = useState('');

  async function handleGreet() {
    const result = await invoke<{ message: string }>('greet', { name: 'World' });
    setGreeting(result.message);
  }

  return (
    <div style={{ padding: 40, fontFamily: 'sans-serif' }}>
      <h1>Tauri Desktop App</h1>
      <button onClick={handleGreet}>Greet</button>
      <p>{greeting}</p>
    </div>
  );
}

export default App;
{
  "$schema": "https://schema.tauri.app/config/2",
  "productName": "tauri-desktop-app",
  "version": "0.1.0",
  "identifier": "com.example.tauriapp",
  "build": {
    "frontendDist": "../dist",
    "devUrl": "http://localhost:5173"
  },
  "app": {
    "windows": [
      {
        "title": "Tauri Desktop App",
        "width": 1000,
        "height": 700
      }
    ]
  }
}