only pass the getrandom_backend cfg flag when needed

This commit is contained in:
Matt Yan 2025-03-01 18:30:41 +09:00 committed by Siyuan Yan
parent a0d4ab0fc8
commit ce8e35ae77

View File

@ -2,7 +2,7 @@ use std::path::Path;
use std::process::{Command, ExitCode}; use std::process::{Command, ExitCode};
use std::{env, fs}; use std::{env, fs};
use build_examples::{get_latest_wasm_opt_version, is_wasm_opt_outdated, NO_TRUNK_EXAMPLES}; use build_examples::{NO_TRUNK_EXAMPLES, get_latest_wasm_opt_version, is_wasm_opt_outdated};
fn main() -> ExitCode { fn main() -> ExitCode {
// Must be run from root of the repo: // Must be run from root of the repo:
@ -81,20 +81,26 @@ fn main() -> ExitCode {
} }
fn build_example(path: &Path, output_dir: &Path, example: &str) -> bool { fn build_example(path: &Path, output_dir: &Path, example: &str) -> bool {
// Get the public URL prefix from environment or use default
let public_url_prefix = env::var("PUBLIC_URL_PREFIX").unwrap_or_default(); let public_url_prefix = env::var("PUBLIC_URL_PREFIX").unwrap_or_default();
// Set up the dist directory for this example
let dist_dir = output_dir.join(example); let dist_dir = output_dir.join(example);
let uses_rand = has_rand_dependency(path);
let rustflags = format!(
"--cfg nightly_yew{}",
if uses_rand {
" --cfg getrandom_backend=\"wasm_js\""
} else {
""
}
);
// Run trunk build command // Run trunk build command
let status = Command::new("trunk") let status = Command::new("trunk")
.current_dir(path) .current_dir(path)
.arg("build") .arg("build")
.env( .env("RUSTFLAGS", rustflags)
"RUSTFLAGS",
"--cfg nightly_yew --cfg getrandom_backend=\"wasm_js\"",
)
.arg("--release") .arg("--release")
.arg("--dist") .arg("--dist")
.arg(&dist_dir) .arg(&dist_dir)
@ -130,3 +136,17 @@ fn build_example(path: &Path, output_dir: &Path, example: &str) -> bool {
_ => false, _ => false,
} }
} }
// Function to check if the crate has a rand dependency
fn has_rand_dependency(path: &Path) -> bool {
let cargo_toml_path = path.join("Cargo.toml");
if !cargo_toml_path.exists() {
return false;
}
match fs::read_to_string(&cargo_toml_path) {
Ok(content) => content.contains("rand = ") || content.contains("rand =\n"),
Err(_) => false,
}
}