Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bindgen): update to --target flag interface #682

Merged
merged 3 commits into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 44 additions & 15 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,19 @@ pub fn wasm_bindgen_build(
"--typescript"
};
let bindgen_path = bindgen.binary("wasm-bindgen")?;
let target_arg = match target {
Target::Nodejs => "--nodejs",
Target::NoModules => "--no-modules",
Target::Web => {
if supports_web_target(&bindgen_path)? {
"--web"
} else {
bail!("Your current version of wasm-bindgen does not support the 'web' target. Please update your project to wasm-bindgen version >= 0.2.39.")
}
}
Target::Bundler => "--browser",
};

let mut cmd = Command::new(&bindgen_path);
cmd.arg(&wasm_path)
.arg("--out-dir")
.arg(out_dir)
.arg(dts_arg)
.arg(target_arg);
.arg(dts_arg);

let target_arg = build_target_arg(target, &bindgen_path)?;
if supports_dash_dash_target(&bindgen_path)? {
cmd.arg("--target").arg(target_arg);
} else {
cmd.arg(target_arg);
}

if let Some(value) = out_name {
cmd.arg("--out-name").arg(value);
Expand All @@ -80,7 +74,7 @@ pub fn wasm_bindgen_build(
Ok(())
}

/// Check if the `wasm-bindgen` dependency is locally satisfied.
/// Check if the `wasm-bindgen` dependency is locally satisfied for the web target
fn supports_web_target(cli_path: &PathBuf) -> Result<bool, failure::Error> {
let cli_version = semver::Version::parse(&install::get_cli_version(
&install::Tool::WasmBindgen,
Expand All @@ -89,3 +83,38 @@ fn supports_web_target(cli_path: &PathBuf) -> Result<bool, failure::Error> {
let expected_version = semver::Version::parse("0.2.39")?;
Ok(cli_version >= expected_version)
}

/// Check if the `wasm-bindgen` dependency is locally satisfied for the --target flag
fn supports_dash_dash_target(cli_path: &PathBuf) -> Result<bool, failure::Error> {
let cli_version = semver::Version::parse(&install::get_cli_version(
&install::Tool::WasmBindgen,
cli_path,
)?)?;
let expected_version = semver::Version::parse("0.2.40")?;
Ok(cli_version >= expected_version)
}

fn build_target_arg(target: Target, cli_path: &PathBuf) -> Result<String, failure::Error> {
if !supports_dash_dash_target(cli_path)? {
Ok(build_target_arg_legacy(target, cli_path)?)
} else {
Ok(target.to_string())
}
}

fn build_target_arg_legacy(target: Target, cli_path: &PathBuf) -> Result<String, failure::Error> {
log::info!("Your version of wasm-bindgen is out of date. You should consider updating your Cargo.toml to a version >= 0.2.40.");
let target_arg = match target {
Target::Nodejs => "--nodejs",
Target::NoModules => "--no-modules",
Target::Web => {
if supports_web_target(&cli_path)? {
"--web"
} else {
bail!("Your current version of wasm-bindgen does not support the 'web' target. Please update your project to wasm-bindgen version >= 0.2.39.")
}
}
Target::Bundler => "--browser",
};
Ok(target_arg.to_string())
}
13 changes: 13 additions & 0 deletions src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use lockfile::Lockfile;
use log::info;
use manifest;
use readme;
use std::fmt;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Instant;
Expand Down Expand Up @@ -61,6 +62,18 @@ impl Default for Target {
}
}

impl fmt::Display for Target {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let s = match self {
Target::Bundler => "bundler",
Target::Web => "web",
Target::Nodejs => "nodejs",
Target::NoModules => "no-modules",
};
write!(f, "{}", s)
}
}

impl FromStr for Target {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
Expand Down
2 changes: 1 addition & 1 deletion tests/all/utils/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl Fixture {
wasm_pack::wasm_opt::find_wasm_opt(&cache, true).unwrap();
});
}

/// Install a local cargo-generate for this fixture.
///
/// Takes care not to re-install for every fixture, but only the one time
Expand Down