tauri-apps / tauri

Build smaller, faster, and more secure desktop and mobile applications with a web frontend.
https://tauri.app
Apache License 2.0
84.69k stars 2.55k forks source link

[feat] Change `tauri.conf.json` based on build profile (dev/release) #8418

Open preyneyv opened 10 months ago

preyneyv commented 10 months ago

Describe the problem

I'd like to change the bundle identifier based on a build-time configuration (for instance, debug_assertions or an environment variable) to enable plugins like tauri-plugin-single-instance and app configuration stored in %LocalAppData% to distinguish between release and debug builds.

Describe the solution you'd like

A general solution would be loading a modified tauri.conf.json based on the build type, similar to how Tauri currently loads tauri.<platform>.conf.json depending on the platform.

For instance, there could be a tauri.conf.dev.json that's loaded for dev builds that overwrites the bundle identifier. This is also a pretty common pattern for webpack.config.js so it's not an entirely new pattern, at least for web developers.

Alternatives considered

To resolve the app configuration issue, I'm currently using a function like this:

fn path(&app: AppHandle) -> PathBuf {
  let resolver = app.path_resolver();
  resolver
      .app_local_data_dir()
      .expect("unable to resolve app local data directory")
      .join(if cfg!(debug_assertions) { "dev" } else { "release" })
      .join("settings.yml")
}

but I don't have a good solution for other places the bundle identifier would be used.

Additional context

No response

FabianLars commented 10 months ago

Not an "automatic" solution but you could use the --config flag to specify a merge config to overwrite values that differ between the configs.

Alternatively you could change the bundle id in your code with something like this (i'd choose the first option though)

fn main() {
  let mut context = tauri::generate_context!();
  context.config_mut().tauri.bundle.identifier = "a.b.c".to_string();

  tauri::Builder::default()
    .context(context) // make sure to re-use the same context
    .run("error running tauri app");
}

I think option 1 covers this usecase good enough but i'll leave the issue open for the others to make a decision.

preyneyv commented 10 months ago

Thank you! The --config flag works perfectly for me, I can pass it in during my build process.