Skip to content

Commit

Permalink
Make the wasm-bindgen flag --target configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasAlaif committed Dec 8, 2023
1 parent 7a27876 commit 8a9f041
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
1 change: 1 addition & 0 deletions site/content/assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..ot
- `data-reference-types`: (optional) instruct `wasm-bindgen` to enable [reference types](https://rustwasm.github.io/docs/wasm-bindgen/reference/reference-types.html).
- `data-weak-refs`: (optional) instruct `wasm-bindgen` to enable [weak references](https://rustwasm.github.io/docs/wasm-bindgen/reference/weak-references.html).
- `data-typescript`: (optional) instruct `wasm-bindgen` to output Typescript bindings. Defaults to false.
- `data-bindgen-target`: (optional) specifies the value of the `wasm-bindgen` flag [`--target`](https://rustwasm.github.io/wasm-bindgen/reference/deployment.html) (see link for possible values). Defaults to `web`. The main use-case is to switch to `no-modules` for `worker` for backwards [compatibility](https://caniuse.com/mdn-api_worker_worker_ecmascript_modules) but with some [disadvantages](https://rustwasm.github.io/wasm-bindgen/examples/without-a-bundler.html?highlight=no-modules#using-the-older---target-no-modules).
- `data-loader-shim`: (optional) instruct `trunk` to create a loader shim for web workers. Defaults to false.

## sass/scss
Expand Down
52 changes: 41 additions & 11 deletions src/pipelines/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub struct RustApp {
name: String,
/// Whether to create a loader shim script
loader_shim: bool,
/// The value of the `--target` flag for wasm-bindgen.
wasm_bindgen_target: WasmBindgenTarget,
}

/// Describes how the rust application is used.
Expand All @@ -83,6 +85,30 @@ impl FromStr for RustAppType {
}
}

/// 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<Self, Self::Err> {
ensure!(
s == "bundler" || s == "web" || s == "no-modules" || s == "nodejs" || s == "deno",
r#"unknown `data-bindgen-target="{}"` value for <link data-trunk rel="rust" .../> 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";

Expand Down Expand Up @@ -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
Expand All @@ -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!(
Expand Down Expand Up @@ -181,6 +212,7 @@ impl RustApp {
app_type,
name,
loader_shim,
wasm_bindgen_target,
})
}

Expand All @@ -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,
})
Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit 8a9f041

Please sign in to comment.