项目结构
Tauri 项目通常由两部分组成:一个 Rust 项目和一个 JavaScript 项目(可选),通常设置如下所示:
¥A Tauri project is usually made of 2 parts, a Rust project and a JavaScript project (optional), and typically the setup looks something like this:
.├── package.json├── index.html├── src/│ ├── main.js├── src-tauri/│ ├── Cargo.toml│ ├── Cargo.lock│ ├── build.rs│ ├── tauri.conf.json│ ├── src/│ │ ├── main.rs│ │ └── lib.rs│ ├── icons/│ │ ├── icon.png│ │ ├── icon.icns│ │ └── icon.ico│ └── capabilities/│ └── default.json
在这种情况下,JavaScript 项目位于顶层,Rust 项目位于 src-tauri/
内部,Rust 项目是一个包含一些额外文件的普通 Cargo 项目:
¥In this case, the JavaScript project is at the top level, and the Rust project is inside src-tauri/
,
the Rust project is a normal Cargo project with some extra files:
-
tauri.conf.json
是 Tauri 的主要配置文件,它包含从应用标识符到开发服务器 URL 的所有内容。该文件也是 Tauri CLI 查找 Rust 项目的标记。要了解更多信息,请参阅 Tauri 配置。¥
tauri.conf.json
is the main configuration file for Tauri, it contains everything from the application identifier to dev server url, this file is also a marker for the Tauri CLI to find the Rust project, to learn more about it, see Tauri Config -
capabilities/
目录是 Tauri 读取 功能文件 的默认文件夹(简而言之,你需要允许此处的命令才能在 JavaScript 代码中使用它们),要了解更多信息,请参阅 安全。¥
capabilities/
directory is the default folder Tauri reads capability files from (in short, you need to allow commands here to use them in your JavaScript code), to learn more about it, see Security -
icons/
目录是tauri icon
命令的默认输出目录,它通常在tauri.conf.json > bundle > icon
中引用并用于应用图标¥
icons/
directory is the default output directory of thetauri icon
command, it’s usually referenced intauri.conf.json > bundle > icon
and used for the app’s icons -
build.rs
包含用于 Tauri 构建系统的tauri_build::build()
。¥
build.rs
containstauri_build::build()
which is used for tauri’s build system -
src/lib.rs
包含 Rust 代码和移动端入口点(标有#[cfg_attr(mobile, tauri::mobile_entry_point)]
的函数)。我们不直接在main.rs
中编写的原因是,我们会在移动端构建中将你的应用编译为库,并通过平台框架加载它们。¥
src/lib.rs
contains the Rust code and the mobile entry point (the function marked with#[cfg_attr(mobile, tauri::mobile_entry_point)]
), the reason we don’t write directly inmain.rs
is because we compile your app to a library in mobile builds and load them through the platform frameworks -
src/main.rs
是桌面版的主要入口点,我们在main
中运行tauri_app_lib::run()
,以便使用与移动版相同的入口点。因此,为了简单起见,请勿修改此文件,而是修改lib.rs
。¥
src/main.rs
is the main entry point for the desktop, and we runtauri_app_lib::run()
inmain
to use the same entry point as mobile, so to keep it simple, don’t modify this file, modifylib.rs
instead
Tauri 的工作原理类似于静态 Web 主机,其构建方式是先将 JavaScript 项目编译为静态文件,然后编译 Rust 项目并将其打包到其中。因此,JavaScript 项目的设置与构建静态网站基本相同。要了解更多信息,请参阅 前端配置。
¥Tauri works similar to a static web host, and the way it builds is that you would compile your JavaScript project to static files first, and then compile the Rust project that will bundle those static files in, so the JavaScript project setup is basically the same as if you were to build a static website, to learn more, see Frontend Configuration
如果你只想使用 Rust 代码,只需删除其他所有内容,并将 src-tauri/
文件夹用作你的顶层项目或 Rust 工作区的成员即可。
¥If you want to work with Rust code only, simply remove everything else and use the src-tauri/
folder as your top level project or as a member of your Rust workspace
¥Next Steps
Tauri v2.8 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站