diff --git a/site/content/assets.md b/site/content/assets.md index 051c6f42..f8c78548 100644 --- a/site/content/assets.md +++ b/site/content/assets.md @@ -31,6 +31,7 @@ This will typically look like: ` Result { + ensure!( + s == "bundler" || s == "web" || s == "no-modules" || s == "nodejs" || s == "deno", + r#"unknown `data-bindgen-target="{}"` value for attr; please ensure the value is lowercase and is a supported type"#, + s + ); + Ok(Self(String::from(s))) + } +} + +impl Default for WasmBindgenTarget { + fn default() -> Self { + Self(String::from("web")) + } +} + impl RustApp { pub const TYPE_RUST_APP: &'static str = "rust"; @@ -114,9 +140,9 @@ impl RustApp { let no_demangle = attrs.contains_key("data-no-demangle"); let app_type = attrs .get("data-type") - .map(|s| s.as_str()) - .unwrap_or("main") - .parse()?; + .map(|s| s.parse()) + .transpose()? + .unwrap_or(RustAppType::Main); let reference_types = attrs.contains_key("data-reference-types"); let weak_refs = attrs.contains_key("data-weak-refs"); let wasm_opt = attrs @@ -135,16 +161,21 @@ impl RustApp { let name = bin.clone().unwrap_or_else(|| manifest.package.name.clone()); let data_features = attrs.get("data-cargo-features").map(|val| val.to_string()); - let data_all_features = attrs.get("data-cargo-all-features").is_some(); - let data_no_default_features = attrs.get("data-cargo-no-default-features").is_some(); + let data_all_features = attrs.contains_key("data-cargo-all-features"); + let data_no_default_features = attrs.contains_key("data-cargo-no-default-features"); - let loader_shim = attrs.get("data-loader-shim").is_some(); + let loader_shim = attrs.contains_key("data-loader-shim"); if loader_shim { ensure!( app_type == RustAppType::Worker, "Loader shim has no effect when data-type is \"main\"!" ); } + let wasm_bindgen_target = attrs + .get("data-bindgen-target") + .map(|s| s.parse()) + .transpose()? + .unwrap_or_default(); // Highlander-rule: There can be only one (prohibits contradicting arguments): ensure!( @@ -181,6 +212,7 @@ impl RustApp { app_type, name, loader_shim, + wasm_bindgen_target, }) } @@ -207,6 +239,7 @@ impl RustApp { weak_refs: false, wasm_opt: WasmOptLevel::Off, app_type: RustAppType::Main, + wasm_bindgen_target: WasmBindgenTarget::default(), name, loader_shim: false, }) @@ -384,12 +417,9 @@ impl RustApp { let arg_out_path = format!("--out-dir={}", bindgen_out); let arg_out_name = format!("--out-name={}", &hashed_name); let target_wasm = wasm.to_string_lossy().to_string(); - let target_type = match self.app_type { - RustAppType::Main => "--target=web", - RustAppType::Worker => "--target=no-modules", - }; + let target_type = format!("--target={}", self.wasm_bindgen_target.0); - let mut args = vec![target_type, &arg_out_path, &arg_out_name, &target_wasm]; + let mut args: Vec<&str> = vec![&target_type, &arg_out_path, &arg_out_name, &target_wasm]; if self.keep_debug { args.push("--keep-debug"); }