前言

Tauri 是一个跨平台的桌面应用开发框架,它对标的就是一直稳坐跨平台桌面应用头把交易椅的 Electron
就像它 slogan 说的一样,它的核心特点在于编译快、包体积小、性能佳,每一个点都精准打击到我这个 Electron 重度使用者的小心脏。
除此之外,Tauri 的后端使用 Rust而不是 nodeJS,相较于 Electron 增加了一点的学习成本。
在 21 年移动端 & 桌面端跨平台前端框架里,关注度稳居第一,说明 Tauri 这只新秀,在各方面都表现不错。
image.png
6月20号,Tauri 1.0 版本正式发布,所以重新整理下以前记录过的 Tauri 相关小记,完整汇总一下。 :::info 以下所有示例及命令均以 macOS 为例 :::

安装

  1. # 安装 CLang 和 macOS 开发依赖
  2. # 如果已经安装则跳过
  3. xcode-select --install
  4. # 安装 Rust
  5. curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
  6. # 检查是否安装成功
  7. xcode-select -v
  8. rustc --version

image.png

创建项目

官方提供的脚手架模板非常丰富,集成了市面上大多数前端框架,如 React、Vue 或 Svelte,可以直接选用。

  1. yarn create tauri-app

我这里选择的 vite + vue3 + ts
image.png
项目创建成功
image.png

运行

  1. cd tauri-app
  2. yarn tauri dev

首次运相关资源构建安装需要等待一段时间
image.png
构建成功后再次运行的时间,那就是飞一样的感觉,爽
image.png

构建

修改应用ID

tauri.conf.json > tauri > bundle > identifier改为自己的包名ID

  1. {
  2. ...
  3. "tauri": {
  4. "allowlist": {
  5. "all": true
  6. },
  7. "bundle": {
  8. ...
  9. "identifier": "com.gaoquanquan.tauri",
  10. "longDescription": "",
  11. ...
  12. },
  13. ...
  14. }
  15. }

打包

  1. yarn tauri build

image.png
截屏2022-06-25 19.47.48.png
可以看见构建出来的 dmgaarch64的,即我本机是 M1 架构应用,可以使用 --target 指定构建架构。

  1. yarn tauri build --target universal-apple-darwin

支持的目标是:

  • aarch64-apple-darwin:Apple Silicon(使用 M1 处理器架构)
  • x86_64-apple-darwin: 基于 Intel 的 Mac
  • universal-apple-darwin:生成一个通用 macOS 二进制文件,可在 Apple 芯片和基于 Intel 的 Mac 上运行。

当然这只是打的本地调试包,跨平台编译和应用签名,我们在下一篇详细介绍。

问题记录

yarn tauri dev 报错

  1. Error running CLI: failed to run cargo: No such file or directory (os error 2)
  2. error Command failed with exit code 1.
  3. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
  1. # 使用 info 检查下环境依赖
  2. yarn tauri info

发现虽然安装 Rust,但当前窗口检查不到,切换新窗口重新运行 dev 命令
image.png
image.png