Skip to content

Commit 9cdcf9b

Browse files
authored
feat(build): create dev alias (#4212)
1 parent ac5b2d6 commit 9cdcf9b

5 files changed

Lines changed: 32 additions & 2 deletions

File tree

.changes/dev-alias.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-build": patch
3+
---
4+
5+
Create `dev` cfg alias.

core/tauri-build/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ tauri-codegen = { version = "1.0.0-rc.7", path = "../tauri-codegen", optional =
2323
tauri-utils = { version = "1.0.0-rc.7", path = "../tauri-utils", features = [ "build", "resources" ] }
2424
cargo_toml = "0.11"
2525
serde_json = "1"
26+
heck = "0.4"
2627

2728
[target."cfg(windows)".dependencies]
2829
winres = "0.1"

core/tauri-build/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![cfg_attr(doc_cfg, feature(doc_cfg))]
66

77
pub use anyhow::Result;
8+
use heck::ToSnakeCase;
89
use tauri_utils::resources::{external_binaries, resource_relpath, ResourcePaths};
910

1011
use std::path::{Path, PathBuf};
@@ -61,6 +62,26 @@ fn copy_resources(resources: ResourcePaths<'_>, path: &Path) -> Result<()> {
6162
Ok(())
6263
}
6364

65+
// checks if the given Cargo feature is enabled.
66+
fn has_feature(feature: &str) -> bool {
67+
// when a feature is enabled, Cargo sets the `CARGO_FEATURE_<name` env var to 1
68+
// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
69+
std::env::var(format!(
70+
"CARGO_FEATURE_{}",
71+
feature.to_snake_case().to_uppercase()
72+
))
73+
.map(|x| x == "1")
74+
.unwrap_or(false)
75+
}
76+
77+
// creates a cfg alias if `has_feature` is true.
78+
// `alias` must be a snake case string.
79+
fn cfg_alias(alias: &str, has_feature: bool) {
80+
if has_feature {
81+
println!("cargo:rustc-cfg={}", alias);
82+
}
83+
}
84+
6485
/// Attributes used on Windows.
6586
#[allow(dead_code)]
6687
#[derive(Debug, Default)]
@@ -163,6 +184,8 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
163184
)?)?
164185
};
165186

187+
cfg_alias("dev", !has_feature("custom-protocol"));
188+
166189
let mut manifest = Manifest::from_path("Cargo.toml")?;
167190
if let Some(tauri) = manifest.dependencies.remove("tauri") {
168191
let features = match tauri {

core/tauri/src/manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ impl<R: Runtime> WindowManager<R> {
338338
///
339339
/// * In dev mode, this will be based on the `devPath` configuration value.
340340
/// * Otherwise, this will be based on the `distDir` configuration value.
341-
#[cfg(custom_protocol)]
341+
#[cfg(not(dev))]
342342
fn base_path(&self) -> &AppUrl {
343343
&self.inner.config.build.dist_dir
344344
}

examples/api/src-tauri/Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)