Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 39 ยท Mobile and Packaging

Sidecars, Resources and Binary Size in Tauri

Ship external programs as Tauri sidecars, bundle resource files, read them at runtime, and shrink your app with Cargo release settings. Includes runnable stand-ins for target triples and line streaming.

Sometimes the best tool for a job is not Rust and not JavaScript. You might need ffmpeg to convert video, a Python script to run a model, or a Go program you already have. Tauri lets you ship such programs inside your installer as sidecars and run them safely from your app. You will also often have plain data files (templates, sample data, fonts) that must travel with the app: those are resources.

This lesson covers both, plus the settings that make your final installer small. It continues from Bundling and Installers and relies on the permission ideas in Scopes and Input Validation.

Real versus stand-in. The json, rust, toml, ts and bash blocks are real Tauri 2 code. The runnable blocks are JavaScript models of target-triple naming and of reading a process's output line by line.

Sidecars

A sidecar is a normal executable that you bundle with the app. Tauri copies it next to your main binary at build time and gives you an API to start it.

1. Put the binary in place, with a target triple suffix

Each platform needs its own build of the program, and the file name must end with the target triple of the machine that will run it. Find yours with:

Terminal
rustc --print host-tuple
TEXT
src-tauri/binaries/
  converter-x86_64-pc-windows-msvc.exe
  converter-x86_64-apple-darwin
  converter-aarch64-apple-darwin
  converter-x86_64-unknown-linux-gnu

2. List it in the config

JSON
{
  "bundle": {
    "externalBin": ["binaries/converter"]
  }
}

Use the name without the triple. The bundler picks the right file per target and strips the suffix when copying it into the app, so at runtime the program is simply converter.

Node.js Stand-in: pick the sidecar file for a target triple

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

3. Allow and run it

Running programs is dangerous, so it goes through the shell plugin and needs a scoped permission:

JSON
{
  "identifier": "shell:allow-execute",
  "allow": [
    {
      "name": "binaries/converter",
      "sidecar": true,
      "args": ["--input", { "validator": "^[\\w./\\\\ -]+$" }, "--format", { "validator": "^(png|jpg|webp)$" }]
    }
  ]
}

The name must match externalBin, sidecar: true marks it as bundled, and args lists the allowed argument shapes. The frontend cannot add extra flags or change the program. From JavaScript:

TypeScript
import { Command } from '@tauri-apps/plugin-shell';

const command = Command.sidecar('binaries/converter', ['--input', 'photo.heic', '--format', 'png']);

command.stdout.on('data', (line) => console.log('out:', line));
command.stderr.on('data', (line) => console.error('err:', line));
command.on('close', ({ code }) => console.log('finished with code', code));

const child = await command.spawn();   // start and keep running
// await child.write('input text\n');  // talk to stdin
// await child.kill();                 // stop it

You can also await command.execute() to wait for the end and get { code, stdout, stderr } in one go. In Rust, app.shell().sidecar("converter")?.args([...]).spawn() does the same and returns a stream of CommandEvents.

Sidecars give you more than an external tool: they can run a local server (such as a Node or Python backend) for the frontend to talk to. Bind to 127.0.0.1 and a random port, pass a secret token, and kill the process when the app exits, otherwise a stray process may stay behind.

Reading output as lines

A child process writes bytes in chunks that may split a line in the middle. The shell plugin gives you whole lines, but if you ever handle raw chunks yourself you must buffer. This model shows how:

Node.js Stand-in: assemble lines from chunked process output

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Resources

Resources are files bundled as they are: templates, JSON data, a default database, models.

JSON
{
  "bundle": {
    "resources": ["assets/templates/*", "assets/default.db"]
  }
}

A map form lets you choose the destination folder inside the bundle: { "assets/**/*": "data/" }. Read a resource at runtime in Rust:

Rust
use tauri::Manager;
use tauri::path::BaseDirectory;

#[tauri::command]
fn load_template(app: tauri::AppHandle, name: String) -> Result<String, String> {
    // validate `name` first (see the scopes lesson)
    let path = app
        .path()
        .resolve(format!("assets/templates/{name}"), BaseDirectory::Resource)
        .map_err(|e| e.to_string())?;
    std::fs::read_to_string(path).map_err(|e| e.to_string())
}

Resources are read-only in practice: installers may put them in protected folders (Program Files, /usr/lib), so copy them to the app data folder if the user must change them. For files the frontend can display directly (images, audio), use the asset protocol with a scope, or read them through a command.

Making the app smaller

Tauri apps are small mostly because they use the system webview, so you do not ship a browser. A hello world installer is a few megabytes. You can reduce further in src-tauri/Cargo.toml:

TOML
[profile.release]
codegen-units = 1   # slower build, smaller and faster code
lto = true          # link-time optimization across crates
opt-level = "s"     # optimize for size ("z" is smaller still, often slower)
panic = "abort"     # no unwinding tables
strip = true        # remove symbols

More habits that matter:

  • Remove plugins and crate features you do not use; each adds code.
  • Check what costs space with cargo bloat --release --crates.
  • Keep the frontend lean: code-split, compress images, avoid giant libraries. The frontend files are compressed into the binary, so their size counts directly.
  • Do not bundle huge assets that can be downloaded on first run.
  • Debug builds are big and slow by design. Always measure the release build.
Node.js Stand-in: estimate installer size

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why must a sidecar file name end with the target triple?

    Each platform needs its own executable. The bundler matches the suffix to the build target and strips it in the final bundle.
  2. 2. How do you restrict what arguments the frontend can pass to a sidecar?

    The scope lists allowed arguments and regex validators, so the frontend cannot add extra flags.
  3. 3. Which Cargo setting removes debug symbols from the release binary?

    strip = true removes symbols. The others also shrink or speed up code but do not strip.
  4. 4. Why copy a resource file to the app data folder if the user must edit it?

    Installed resources may be read-only for normal users, so editable data belongs in the app data directory.

Summary

  • Sidecars are bundled executables: add them to externalBin with target-triple suffixes, permit them with a scoped shell:allow-execute, and run them with the shell plugin.
  • Validate arguments in the scope and kill long-running sidecars on exit.
  • Resources are bundled read-only files, resolved with BaseDirectory::Resource.
  • Shrink apps with release profile settings, fewer plugins, a lean frontend and downloads on demand.

Next: Testing Tauri Apps: Rust and Mocked IPC