Open-Source Wikis

/

Tauri

/

Crates

/

tauri-codegen

tauri-apps/tauri

tauri-codegen

Active contributors: Lucas Fernandes Nogueira, Amr Bashir

Purpose

crates/tauri-codegen is the compile-time engine behind generate_context!. At build time it parses the user's tauri.conf.json, embeds the frontend bundle into the binary, hashes assets for the Content-Security-Policy, embeds default icons, and produces a tauri::Context value that the runtime consumes.

It runs in two contexts:

  1. Inside the user app's build.rs when invoked by tauri-build.
  2. At macro expansion time when tauri::generate_context!() calls into it (via the proc macros in tauri-macros).

Directory layout

crates/tauri-codegen/
├── Cargo.toml
├── src/
│   ├── lib.rs              # public surface
│   ├── context.rs          # 15.5 KB: produce the Context tokens
│   ├── embedded_assets.rs  # 14.5 KB: walk frontendDist, hash, optionally compress
│   ├── image.rs            # decode/embed icon
│   └── vendor/             # vendored bits used during embedding
└── build.rs

Key abstractions

Type / function File Role
context_codegen crates/tauri-codegen/src/context.rs Top-level entry: takes a ContextData (config path, root dir, dev flag) and returns TokenStream.
EmbeddedAssets crates/tauri-codegen/src/embedded_assets.rs Walks the frontend dist directory, builds a phf map of path → asset bytes (+ CSP hash).
AssetOptions same Controls compression, CSP injection, isolation pattern wrapping.
image_icon crates/tauri-codegen/src/image.rs Decodes PNG/ICO bytes at compile time for use in include_image! and the default window icon.
CodegenConfig crates/tauri-codegen/src/context.rs Shape of the data passed to the codegen, including the parsed Config.

How it works

graph TD
    Config["tauri.conf.json"] --> Parse["tauri-utils config parser"]
    FrontendDist["dist/ (HTML/JS/CSS)"] --> Walk["EmbeddedAssets::new"]
    Parse --> Tokens["context_codegen()"]
    Walk --> Tokens
    Icon["app icon"] --> Tokens
    Tokens --> Output["TokenStream consumed by generate_context!"]
    Output --> Builder["tauri::Builder::build(context)"]

Highlights:

  • CSP injection. embedded_assets.rs rewrites <script>/<style> tags so it can compute and inject sha256 hashes into the configured CSP. The HTML rewrite logic lives in crates/tauri-utils/src/html.rs and is re-used here.
  • Compression. When the compression feature is on (default), assets are stored deflate-compressed and decoded on demand at runtime. This trades startup time for binary size.
  • Isolation pattern. When app.security.pattern.use = "isolation", the codegen wraps the user's frontend in the Tauri isolation iframe with a build-time-generated key. See systems/security.
  • Mobile. The Cargo metadata block lets tauri-codegen participate in the mobile build flow without taking a runtime dependency on TAO/WRY.

Integration points

  • Up: called by tauri-macros::generate_context! and by tauri-build's codegen step.
  • Down: depends on tauri-utils for the config types, asset HTML helpers, ACL parsing, and the isolation key generator.

Entry points for modification

  • New asset format support → crates/tauri-codegen/src/embedded_assets.rs.
  • Change Context output shape → crates/tauri-codegen/src/context.rs (plus the consumer in crates/tauri/src/manager/).
  • Image format work (e.g. animated icons) → crates/tauri-codegen/src/image.rs.

See tauri-build for the build-script side and tauri-utils for the config types.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

tauri-codegen – Tauri wiki | Factory