labelprinter 是一个完整的 Tauri 应用 (有自己的 tauri.conf.json、build.rs、前端资源),它仍然作为 workspace 的一个成员存在,独立运行、独立调试。
1. 建目录
在 workspace 根目录下执行:
bash
cargo new labelprinter --bin
生成:
text
labelprinter/
Cargo.toml
src/main.rs
2. 加入 workspace
默认自动加入workspace,检查根 Cargo.toml:
toml
[workspace]
members = [
"bridge",
"a11core",
"labelprinter",
]
[package]
name = "app"
version = "0.1.0"
edition = "2024"
[dependencies]
bridge = { path = "bridge" }
a11core = { path = "a11core" }
tauri = { version = "2", features = [] }
[build-dependencies]
tauri-build = { version = "2", features = [] }
注意:根 app 不需要依赖 labelprinter,保持它独立。
3. labelprinter/Cargo.toml
toml
[package]
name = "labelprinter"
version = "0.1.0"
edition = "2024"
description = "资产标签打印"
[build-dependencies]
tauri-build = { version = "2", features = [] }
[dependencies]
tauri = { version = "2", features = [] }
# 后面接硬件采集 / Excel / DB 时再加:
# hwcollect = { path = "../hwcollect" }
# excelgen = { path = "../excelgen" }
# pcdb = { path = "../pcdb" }
[[bin]]
name = "labelprinter"
path = "src/main.rs"
要点:
tauri放[dependencies](运行时用),tauri-build放[build-dependencies](编译期用),二者不能混。[[bin]]显式写出来,避免以后加lib.rs时产生歧义。
4. labelprinter/build.rs
rust
fn main() {
tauri_build::build();
}
没有这个文件,tauri::generate_context!() 会报找不到上下文。
5. labelprinter/src/main.rs
rust
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
fn main() {
tauri::Builder::default()
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
generate_context!() 在编译期会读 labelprinter/tauri.conf.json,所以配置文件必须在 labelprinter/ 目录下(不是 workspace 根)。
6. labelprinter/tauri.conf.json
最小可用版本:
json
{
"$schema": "https://schema.tauri.app/config/2",
"productName": "labelprinter",
"version": "0.1.0",
"identifier": "com.example.labelprinter",
"build": {
"frontendDist": "https://weida.dothantech.com/#/editor",
"devUrl": "https://weida.dothantech.com/#/editor"
},
"app": {
"windows": [
{
"title": "资产标签打印",
"width": 1280,
"height": 900
}
],
"security": {
"csp": null
}
},
"bundle": {
"active": true,
"targets": "all",
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/icon.ico",
"icons/icon.icns"
]
}
}
frontendDist 指向 labelprinter/../dist,也就是 workspace 根下的 dist/;你也可以改成自己的前端产物路径,或者用 devUrl 指向 http://localhost:1420 做开发。
7. 图标(必须)
Tauri 要求 icons/ 至少有几个占位图标,否则 tauri build 会失败。最省事的做法:
bash
cd labelprinter
cargo tauri icon path/to/any.png
或者手动放几个:
labelprinter/icons/32x32.pnglabelprinter/icons/128x128.pnglabelprinter/icons/icon.ico(Windows)labelprinter/icons/icon.icns(macOS)
只是 cargo run 调试时,缺图标一般不致命,但一旦做打包就会报错,所以最好一开始放上。
8. 前端产物
因为 frontendDist 指向 ../dist,要么:
- 放一个静态占位 :在 workspace 根建
dist/index.html,内容随便:
html
<!doctype html>
<html><body><h1>labelprinter</h1></body></html>
- 或改成
devUrl指向你已有的前端 dev server。
调试纯后端逻辑(采集/Excel/DB/打印)时,用这个占位 HTML 完全够,不需要真前端。
9. 目录最终结构
text
app/
Cargo.toml
src/main.rs # 主 app(也是 Tauri 应用)
build.rs
tauri.conf.json
...
labelprinter/
Cargo.toml
build.rs
tauri.conf.json
icons/
32x32.png
128x128.png
icon.ico
icon.icns
src/main.rs
dist/
index.html # 可选的占位前端
10. 运行与调试
bash
# 只跑 labelprinter 这个 Tauri 应用
cargo run -p labelprinter
# 只编译它,不跑
cargo check -p labelprinter
# 跑主程序(app)
cargo run -p app
# 全 workspace 检查
cargo check --workspace
关键点解说
-
为什么 labelprinter 要独立
tauri.conf.json?Tauri 的
generate_context!()宏是按 CARGO_MANIFEST_DIR 解析配置的,每个 Tauri 应用都必须有自己的一份。workspace 里的其他成员(bridge、a11core)不需要,因为它们不是 Tauri 应用。 -
和主
app的关系- 两个都是独立 Tauri 应用,各跑各的。
labelprinter想复用主 app 的业务逻辑,通过 lib crate(bridge / a11core) ,而不是直接use app::...。- 现在主 app 也不依赖 labelprinter,双向解耦。
-
和最初的"纯 bin"方案差异
- 纯 bin 方案的
[[bin]]只跑命令行逻辑,简单。 - Tauri 版多了
build.rs、tauri.conf.json、icons/、前端产物这四样,缺一不可。 - 但因为它仍是 workspace 成员,
cargo run -p labelprinter用法完全一样,不影响主 app。
- 纯 bin 方案的
-
将来并入 app 的路径
等 labelprinter 调试成熟,有两种"人工并入"方式:
- 把业务逻辑搬成 lib :把采集/Excel/DB/打印的代码抽到
bridge或新的labelcorelib,让主 app 直接调用,Tauri 界面部分可以保留成独立成员或整体删掉。 - 整体并入主 app :把
labelprinter/src/*搬进app/src/label/,把 labelprinter 窗口作为主 app 的一个页面。此时注意别让两个tauri.conf.json冲突------主 app 只用它自己的。
- 把业务逻辑搬成 lib :把采集/Excel/DB/打印的代码抽到
-
常见坑
tauri-build写进[dependencies]而不是[build-dependencies]→ 编译报错。- 忘记
build.rs→generate_context!找不到上下文。 tauri.conf.json放在 workspace 根 →generate_context!找不到。- 缺
icons/→cargo run可能能过,tauri build一定失败。 frontendDist路径写错 → 启动时白屏或报frontendDist not found。
按这个步骤建完,cargo run -p labelprinter 就能弹出一个独立窗口,和其他成员互不干扰。后续把 hwcollect/excelgen/pcdb 从"概念"落成真 crate 时,只要在 labelprinter/Cargo.toml 里加上 path 依赖,main.rs 里 use hwcollect::... 即可。