diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d63b8e0..5979edb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Subheadings to categorize changes are `added, changed, deprecated, removed, fixe ## Unreleased ### changed - Remove HTML glob in tailwind.config.js +- Added `data-bindgen-target` to make value of the `wasm-bindgen` flag `--target` configurable. ## 0.17.4 ### added diff --git a/site/content/assets.md b/site/content/assets.md index 051c6f42..0fafc0d4 100644 --- a/site/content/assets.md +++ b/site/content/assets.md @@ -31,6 +31,7 @@ This will typically look like: ` WasmBindgenTarget { + match self { + RustAppType::Main => WasmBindgenTarget(String::from("web")), + RustAppType::Worker => WasmBindgenTarget(String::from("no-modules")), + } + } +} + +/// Determines the value of `--target` flag for wasm-bindgen. For more details see +/// [here](https://rustwasm.github.io/wasm-bindgen/reference/deployment.html). +#[derive(Debug, Clone)] +pub struct WasmBindgenTarget(String); + +impl FromStr for WasmBindgenTarget { + type Err = anyhow::Error; + + fn from_str(s: &str) -> 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 RustApp { pub const TYPE_RUST_APP: &'static str = "rust"; @@ -114,9 +143,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 +164,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_else(|| app_type.default_target()); // Highlander-rule: There can be only one (prohibits contradicting arguments): ensure!( @@ -181,6 +215,7 @@ impl RustApp { app_type, name, loader_shim, + wasm_bindgen_target, }) } @@ -207,6 +242,7 @@ impl RustApp { weak_refs: false, wasm_opt: WasmOptLevel::Off, app_type: RustAppType::Main, + wasm_bindgen_target: RustAppType::Main.default_target(), name, loader_shim: false, }) @@ -384,12 +420,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"); }