diff --git a/site/content/assets.md b/site/content/assets.md
index 051c6f42..d7cc7b5b 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
@@ -130,15 +156,20 @@ impl RustApp {
WasmOptLevel::Off
}
});
+ let wasm_bindgen_target = attrs
+ .get("data-bindgen-target")
+ .map(|s| s.parse())
+ .transpose()?
+ .unwrap_or_default();
let manifest = CargoMetadata::new(&manifest_href).await?;
let id = Some(id);
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,
@@ -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");
}
@@ -450,14 +480,18 @@ impl RustApp {
.await
.context("error creating loader shim script")?;
+ let shim = match self.wasm_bindgen_target.0.as_ref() {
+ "web" => format!("import init from './{hashed_js_name}';await init();"),
+ "no-modules" => format!(
+ r#"importScripts("./{hashed_js_name}");wasm_bindgen("./{hashed_wasm_name}");"#,
+ ),
+ _ => bail!(
+ "Loader shim can only be created for data-bindgen-target \"web\" or \
+ \"no-modules\"!"
+ ),
+ };
loader_f
- .write_all(
- format!(
- r#"importScripts("./{}");wasm_bindgen("./{}");"#,
- hashed_js_name, hashed_wasm_name
- )
- .as_bytes(),
- )
+ .write_all(shim.as_bytes())
.await
.context("error writing loader shim script")?;
loader_f