Vlad Miller

Software Engineer

Europe

Tauri with Cargo workspace

Somewhere in the GH Issues for Tauri I saw that people struggle to use Tauri in a nice monorepo setup.

It is actually quite simple to make it work.

Let's imagine we have the following structure for our code.


- project/
  - Cargo.toml
  - crates/
    - my_crate
      - src/
      - Cargo.toml
    - tauri_backend
      - src/
      - Cargo.toml
      - package.json
      - tauri.conf.json
  - ui
    - tauri
      - package.json 
      - # rest of the code related to the front-end app

The Cargo.toml config that we have in the root has a simple config for the monorepo rust set-up, that looks like the following

[workspace]
members = ["crates/*"]
resolver = "2"

[workspace.package]
version = "0.1.0"
edition = "2021"

Nothing too fancy.

Then we can simply create crates in the crates/ folder like so

cargo new my_crate --lib

Then we can manually add a Tauri crate to crates/. Just copy the src-tauri/ folder that is usually generated by create-tauri-app.

The key here is to update the build section in tauri.conf.json. Make sure that you have proper paths.

"build": {
    "frontendDist": "../../ui/tauri/dist",
    "devUrl": "http://localhost:1420",
    "beforeDevCommand": "cd ../../ui/tauri && pnpm dev",
    "beforeBuildCommand": "cd ../../ui/tauri && pnpm build"
},

Also, we need to create package.json in the same tauri_backend folder. Similar to

{
    "name": "@vladmiller/tauri-backend",
    "private": true,
    "scripts": {
        "tauri": "tauri"
    },
    "devDependencies": {
        "@tauri-apps/cli": "^2"
    }
}

This way you can call pnpm tauri dev or pnpm tauri build from the tauri_backend folder and get the regular Tauri experience with HMR.

This guide assumes that pnpm is being used, however, it is simple to adapt it to other node package managers.