From f22e7d400fe023284fc82234c6586aa263942430 Mon Sep 17 00:00:00 2001 From: imbytecat Date: Fri, 16 Jan 2026 20:26:25 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4Vite=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E9=85=8D=E7=BD=AE=E5=B9=B6=E5=85=A8=E9=9D=A2=E8=BD=AC?= =?UTF-8?q?=E5=90=91Tauri=E6=A1=8C=E9=9D=A2=E5=8C=96=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删除了项目入口HTML文件中的所有内容和结构 - 删除启动加载页面文件 - 更新项目脚本和依赖项以使用 Tauri 命令替代 Vite 相关配置 - 更新 README 文档,详细说明基于 Tauri 的 Nitro 全栈应用桌面化方案,包括项目结构、使用步骤、工作原理、API 调用方式及配置说明。 - 移除启动时的加载窗口逻辑,直接创建并显示主窗口 - 删除项目中的 Tauri 图标文件 - 删除已废弃的 TypeScript 图标文件 - 删除项目中的Vite图标文件 - 删除主入口文件中的问候功能相关代码 - 删除了项目中的全局样式文件,移除了所有自定义的CSS样式规则和主题配置。 - 删除 TypeScript 配置文件中的编译选项和包含路径配置 - 删除旧的Vite配置文件以移除过时的Tauri开发服务器设置 --- README.md | 144 ++++++++++++++++++++++++++++++++++++-- index.html | 43 ------------ loading.html | 67 ------------------ package.json | 16 ++--- src-tauri/src/lib.rs | 24 +------ src/assets/tauri.svg | 6 -- src/assets/typescript.svg | 25 ------- src/assets/vite.svg | 1 - src/main.ts | 22 ------ src/styles.css | 116 ------------------------------ tsconfig.json | 23 ------ vite.config.ts | 30 -------- 12 files changed, 146 insertions(+), 371 deletions(-) delete mode 100644 index.html delete mode 100644 loading.html delete mode 100644 src/assets/tauri.svg delete mode 100644 src/assets/typescript.svg delete mode 100644 src/assets/vite.svg delete mode 100644 src/main.ts delete mode 100644 src/styles.css delete mode 100644 tsconfig.json delete mode 100644 vite.config.ts diff --git a/README.md b/README.md index b381dcf..545960b 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,143 @@ -# Tauri + Vanilla TS +# Tauri Sidecar Shell for Nitro -This template should help get you started developing with Tauri in vanilla HTML, CSS and Typescript. +这是一个纯 Tauri 壳项目,用于将 Nitro 全栈应用打包成桌面应用。 -## Recommended IDE Setup +## 📁 项目结构 -- [VS Code](https://code.visualstudio.com/) + [Tauri](https://marketplace.visualstudio.com/items?itemName=tauri-apps.tauri-vscode) + [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer) +``` +tauri-demo/ +├── src-tauri/ # Tauri 后端(Rust) +│ ├── src/ +│ │ └── lib.rs # 主逻辑:启动 Nitro sidecar +│ ├── binaries/ +│ │ └── nitro-server-x86_64-pc-windows-msvc.exe # Nitro 二进制文件 +│ ├── Cargo.toml # Rust 依赖 +│ ├── tauri.conf.json # Tauri 配置 +│ └── capabilities/ # 权限配置 +├── package.json # 只包含 @tauri-apps/cli +└── TAURI_API_IN_NITRO.md # 如何在 Nitro 项目中使用 Tauri API +``` + +**注意**:没有前端代码!前端来自你的 Nitro 应用。 + +## 🚀 使用方法 + +### 1. 准备 Nitro 二进制文件 + +在你的 Nitro 项目中打包: + +```bash +bun build --compile --outfile nitro-server +``` + +### 2. 放置到 Tauri 项目 + +```bash +# Windows +cp nitro-server.exe tauri-demo/src-tauri/binaries/nitro-server-x86_64-pc-windows-msvc.exe + +# macOS (Intel) +cp nitro-server tauri-demo/src-tauri/binaries/nitro-server-x86_64-apple-darwin + +# macOS (Apple Silicon) +cp nitro-server tauri-demo/src-tauri/binaries/nitro-server-aarch64-apple-darwin + +# Linux +cp nitro-server tauri-demo/src-tauri/binaries/nitro-server-x86_64-unknown-linux-gnu +``` + +### 3. 开发测试 + +```bash +bun run dev +# 或 +bun run tauri dev +``` + +### 4. 打包发布 + +```bash +bun run build +# 或 +bun run tauri build +``` + +输出在:`src-tauri/target/release/` + +## 🔧 工作原理 + +1. **Tauri 启动** → 检测可用端口(默认 3000) +2. **启动 Nitro sidecar** → 通过环境变量 `NITRO_PORT` 传递端口 +3. **监听 stdout** → 等待 Nitro 输出 `"Listening on:"` 确认启动 +4. **创建窗口** → 加载 `http://localhost:{port}` +5. **应用退出时** → 自动终止 Nitro 进程 + +## 📦 在 Nitro 项目中使用 Tauri API + +在你的 Nitro 项目中: + +```bash +bun add @tauri-apps/api +``` + +然后直接使用: + +```typescript +import { invoke } from '@tauri-apps/api/core'; + +// 调用 Rust 命令 +const result = await invoke('greet', { name: '张三' }); +``` + +详细文档:[TAURI_API_IN_NITRO.md](./TAURI_API_IN_NITRO.md) + +## ⚙️ 配置说明 + +### 修改窗口标题/大小 + +编辑 `src-tauri/src/lib.rs`: + +```rust +.title("你的应用名称") +.inner_size(1200.0, 800.0) // 宽度 x 高度 +``` + +### 修改端口范围 + +编辑 `src-tauri/src/lib.rs`: + +```rust +let port = find_available_port(3000).await; // 从 3000 开始查找 +``` + +### 修改启动超时时间 + +编辑 `src-tauri/src/lib.rs`: + +```rust +let timeout = Duration::from_secs(5); // 5 秒超时 +``` + +## 📝 常见问题 + +### Q: 为什么没有前端代码? +A: 前端完全由你的 Nitro 应用提供,Tauri 只是一个壳。 + +### Q: 如何调试? +A: 运行 `bun run dev`,查看控制台输出: +- `使用端口: 3000` +- `Nitro: ➜ Listening on: ...` +- `✓ Nitro 服务器启动成功!` + +### Q: 打包后体积多大? +A: 约 3-5MB(Tauri 壳)+ 你的 Nitro 二进制文件大小 + +### Q: 如何添加图标? +A: 替换 `src-tauri/icons/` 目录下的图标文件 + +## 🎯 核心优势 + +- ✅ **轻量**:比 Electron 小 30-40 倍 +- ✅ **简单**:无需学 Rust,只需配置 +- ✅ **原生**:使用系统 WebView,性能好 +- ✅ **灵活**:Nitro 应用可以独立开发和测试 diff --git a/index.html b/index.html deleted file mode 100644 index 11ac6b8..0000000 --- a/index.html +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - Tauri App - - - - -
-

Welcome to Tauri

- - -

Click on the Tauri logo to learn more about the framework

- -
- - -
-

-
- - diff --git a/loading.html b/loading.html deleted file mode 100644 index e2b1388..0000000 --- a/loading.html +++ /dev/null @@ -1,67 +0,0 @@ - - - - - - 启动中... - - - -
-
-

正在启动应用

-

请稍候

-
- - diff --git a/package.json b/package.json index 80876c6..6b55674 100644 --- a/package.json +++ b/package.json @@ -4,18 +4,12 @@ "version": "0.1.0", "type": "module", "scripts": { - "dev": "vite", - "build": "tsc && vite build", - "preview": "vite preview", - "tauri": "tauri" - }, - "dependencies": { - "@tauri-apps/api": "^2", - "@tauri-apps/plugin-opener": "^2" + "tauri": "tauri", + "dev": "tauri dev", + "build": "tauri build" }, "devDependencies": { - "@tauri-apps/cli": "^2", - "vite": "^6.0.3", - "typescript": "~5.6.2" + "@tauri-apps/cli": "^2" } } + diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 314c7c5..d348ca7 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -41,20 +41,6 @@ pub fn run() { let app_handle = app.handle().clone(); - // 先创建 loading 窗口 - let _loading_window = tauri::WebviewWindowBuilder::new( - app, - "loading", - tauri::WebviewUrl::App("loading.html".into()) - ) - .title("正在启动...") - .inner_size(400.0, 300.0) - .center() - .resizable(false) - .decorations(true) - .build() - .expect("创建 loading 窗口失败"); - // 异步启动 Nitro sidecar tauri::async_runtime::spawn(async move { // 查找可用端口 @@ -92,7 +78,7 @@ pub fn run() { // 创建主窗口 let url = format!("http://localhost:{}", port); - let main_window = tauri::WebviewWindowBuilder::new( + tauri::WebviewWindowBuilder::new( &app_handle, "main", tauri::WebviewUrl::External(url.parse().unwrap()) @@ -103,14 +89,6 @@ pub fn run() { .build() .expect("创建窗口失败"); - // 关闭 loading 窗口 - if let Some(loading) = app_handle.get_webview_window("loading") { - let _ = loading.close(); - } - - // 显示主窗口 - let _ = main_window.show(); - break; } } diff --git a/src/assets/tauri.svg b/src/assets/tauri.svg deleted file mode 100644 index 31b62c9..0000000 --- a/src/assets/tauri.svg +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/src/assets/typescript.svg b/src/assets/typescript.svg deleted file mode 100644 index 30a5edd..0000000 --- a/src/assets/typescript.svg +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - diff --git a/src/assets/vite.svg b/src/assets/vite.svg deleted file mode 100644 index e7b8dfb..0000000 --- a/src/assets/vite.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/src/main.ts b/src/main.ts deleted file mode 100644 index 4783341..0000000 --- a/src/main.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { invoke } from "@tauri-apps/api/core"; - -let greetInputEl: HTMLInputElement | null; -let greetMsgEl: HTMLElement | null; - -async function greet() { - if (greetMsgEl && greetInputEl) { - // Learn more about Tauri commands at https://tauri.app/develop/calling-rust/ - greetMsgEl.textContent = await invoke("greet", { - name: greetInputEl.value, - }); - } -} - -window.addEventListener("DOMContentLoaded", () => { - greetInputEl = document.querySelector("#greet-input"); - greetMsgEl = document.querySelector("#greet-msg"); - document.querySelector("#greet-form")?.addEventListener("submit", (e) => { - e.preventDefault(); - greet(); - }); -}); diff --git a/src/styles.css b/src/styles.css deleted file mode 100644 index 7011746..0000000 --- a/src/styles.css +++ /dev/null @@ -1,116 +0,0 @@ -.logo.vite:hover { - filter: drop-shadow(0 0 2em #747bff); -} - -.logo.typescript:hover { - filter: drop-shadow(0 0 2em #2d79c7); -} -:root { - font-family: Inter, Avenir, Helvetica, Arial, sans-serif; - font-size: 16px; - line-height: 24px; - font-weight: 400; - - color: #0f0f0f; - background-color: #f6f6f6; - - font-synthesis: none; - text-rendering: optimizeLegibility; - -webkit-font-smoothing: antialiased; - -moz-osx-font-smoothing: grayscale; - -webkit-text-size-adjust: 100%; -} - -.container { - margin: 0; - padding-top: 10vh; - display: flex; - flex-direction: column; - justify-content: center; - text-align: center; -} - -.logo { - height: 6em; - padding: 1.5em; - will-change: filter; - transition: 0.75s; -} - -.logo.tauri:hover { - filter: drop-shadow(0 0 2em #24c8db); -} - -.row { - display: flex; - justify-content: center; -} - -a { - font-weight: 500; - color: #646cff; - text-decoration: inherit; -} - -a:hover { - color: #535bf2; -} - -h1 { - text-align: center; -} - -input, -button { - border-radius: 8px; - border: 1px solid transparent; - padding: 0.6em 1.2em; - font-size: 1em; - font-weight: 500; - font-family: inherit; - color: #0f0f0f; - background-color: #ffffff; - transition: border-color 0.25s; - box-shadow: 0 2px 2px rgba(0, 0, 0, 0.2); -} - -button { - cursor: pointer; -} - -button:hover { - border-color: #396cd8; -} -button:active { - border-color: #396cd8; - background-color: #e8e8e8; -} - -input, -button { - outline: none; -} - -#greet-input { - margin-right: 5px; -} - -@media (prefers-color-scheme: dark) { - :root { - color: #f6f6f6; - background-color: #2f2f2f; - } - - a:hover { - color: #24c8db; - } - - input, - button { - color: #ffffff; - background-color: #0f0f0f98; - } - button:active { - background-color: #0f0f0f69; - } -} diff --git a/tsconfig.json b/tsconfig.json deleted file mode 100644 index 75abdef..0000000 --- a/tsconfig.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "compilerOptions": { - "target": "ES2020", - "useDefineForClassFields": true, - "module": "ESNext", - "lib": ["ES2020", "DOM", "DOM.Iterable"], - "skipLibCheck": true, - - /* Bundler mode */ - "moduleResolution": "bundler", - "allowImportingTsExtensions": true, - "resolveJsonModule": true, - "isolatedModules": true, - "noEmit": true, - - /* Linting */ - "strict": true, - "noUnusedLocals": true, - "noUnusedParameters": true, - "noFallthroughCasesInSwitch": true - }, - "include": ["src"] -} diff --git a/vite.config.ts b/vite.config.ts deleted file mode 100644 index fc3fe85..0000000 --- a/vite.config.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { defineConfig } from "vite"; - -// @ts-expect-error process is a nodejs global -const host = process.env.TAURI_DEV_HOST; - -// https://vite.dev/config/ -export default defineConfig(async () => ({ - - // Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build` - // - // 1. prevent Vite from obscuring rust errors - clearScreen: false, - // 2. tauri expects a fixed port, fail if that port is not available - server: { - port: 1420, - strictPort: true, - host: host || false, - hmr: host - ? { - protocol: "ws", - host, - port: 1421, - } - : undefined, - watch: { - // 3. tell Vite to ignore watching `src-tauri` - ignored: ["**/src-tauri/**"], - }, - }, -}));