wasm_js/
bindgen.rs

1//! Functionality related to running `wasm-bindgen`.
2
3use crate::child;
4use crate::command::build::BuildProfile;
5use crate::install::{self, Tool};
6use crate::manifest::CrateData;
7use anyhow::{Context, Result};
8use semver;
9use std::path::{Path, PathBuf};
10use std::process::Command;
11
12/// Run the `wasm-bindgen` CLI to generate bindings for the current crate's
13/// `.wasm`.
14pub fn wasm_bindgen_build(
15    data: &CrateData,
16    install_status: &install::Status,
17    out_name: &Option<String>,
18    weak_refs: bool,
19    reference_types: bool,
20    profile: BuildProfile,
21    extra_options: &Vec<String>,
22) -> Result<PathBuf> {
23    let profile_name = match profile.clone() {
24        BuildProfile::Release | BuildProfile::Profiling => "release",
25        BuildProfile::Dev => "debug",
26        BuildProfile::Custom(profile_name) => &profile_name.clone(),
27    };
28
29    let target_directory = {
30        let mut has_target_dir_iter = extra_options.iter();
31        has_target_dir_iter
32            .find(|&it| it == "--target-dir")
33            .and_then(|_| has_target_dir_iter.next())
34            .map(Path::new)
35            .unwrap_or(data.target_directory())
36    };
37
38    let wasm_path = target_directory
39        .join("wasm32-unknown-unknown")
40        .join(profile_name)
41        .join(data.crate_name())
42        .with_extension("wasm");
43
44    let out_dir_path = target_directory.join("wasm-bindgen");
45    let out_dir = out_dir_path.to_str().unwrap();
46
47    let dts_arg = "--typescript";
48    let bindgen_path = install::get_tool_path(install_status, Tool::WasmBindgen)?
49        .binary(&Tool::WasmBindgen.to_string())?;
50
51    let mut cmd = Command::new(&bindgen_path);
52    cmd.arg(&wasm_path)
53        .arg("--out-dir")
54        .arg(out_dir)
55        .arg(dts_arg);
56
57    if weak_refs {
58        cmd.arg("--weak-refs");
59    }
60
61    if reference_types {
62        cmd.arg("--reference-types");
63    }
64
65    let target_arg = build_target_arg(&bindgen_path)?;
66    if supports_dash_dash_target(&bindgen_path)? {
67        cmd.arg("--target").arg(target_arg);
68    } else {
69        cmd.arg(target_arg);
70    }
71
72    if let Some(value) = out_name {
73        cmd.arg("--out-name").arg(value);
74    }
75
76    let profile = data.configured_profile(profile);
77    if profile.wasm_bindgen_debug_js_glue() {
78        cmd.arg("--debug");
79    }
80    if !profile.wasm_bindgen_demangle_name_section() {
81        cmd.arg("--no-demangle");
82    }
83    if profile.wasm_bindgen_dwarf_debug_info() {
84        cmd.arg("--keep-debug");
85    }
86    if profile.wasm_bindgen_omit_default_module_path() {
87        cmd.arg("--omit-default-module-path");
88    }
89    if profile.wasm_bindgen_split_linked_modules() {
90        cmd.arg("--split-linked-modules");
91    }
92
93    child::run(cmd, "wasm-bindgen").context("Running the wasm-bindgen CLI")?;
94    Ok(out_dir_path)
95}
96
97/// Check if the `wasm-bindgen` dependency is locally satisfied for the --target flag
98fn supports_dash_dash_target(cli_path: &Path) -> Result<bool> {
99    let cli_version = semver::Version::parse(&install::get_cli_version(
100        &install::Tool::WasmBindgen,
101        cli_path,
102    )?)?;
103    let expected_version = semver::Version::parse("0.2.40")?;
104    Ok(cli_version >= expected_version)
105}
106
107fn build_target_arg(cli_path: &Path) -> Result<String> {
108    if !supports_dash_dash_target(cli_path)? {
109        Ok("--browser".to_string())
110    } else {
111        Ok("bundler".to_string())
112    }
113}