Skip to content

Commit

Permalink
Add --cargo-build and --wasm-bindgen flags
Browse files Browse the repository at this point in the history
  • Loading branch information
darinmorrison committed May 21, 2020
1 parent e758b22 commit d282426
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn wasm_bindgen_build(
disable_dts: bool,
target: Target,
profile: BuildProfile,
wasm_bindgen_options: &[String],
) -> Result<(), failure::Error> {
let release_or_debug = match profile {
BuildProfile::Release | BuildProfile::Profiling => "release",
Expand Down Expand Up @@ -70,6 +71,8 @@ pub fn wasm_bindgen_build(
cmd.arg("--keep-debug");
}

cmd.args(wasm_bindgen_options);

child::run(cmd, "wasm-bindgen").context("Running the wasm-bindgen CLI")?;
Ok(())
}
Expand Down
18 changes: 17 additions & 1 deletion src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ fn wasm_pack_local_version() -> Option<String> {
pub fn cargo_build_wasm(
path: &Path,
profile: BuildProfile,
cargo_build_options: &[String],
extra_options: &[String],
) -> Result<(), Error> {
let msg = format!("{}Compiling to Wasm...", emoji::CYCLONE);
Expand Down Expand Up @@ -107,7 +108,22 @@ pub fn cargo_build_wasm(
}

cmd.arg("--target").arg("wasm32-unknown-unknown");
cmd.args(extra_options);

// Warn about deprecated `-- <extra_options>`
if !extra_options.is_empty() {
PBAR.warn("`-- <extra_options>` is deprecated; use `--cargo-build` instead");
if !cargo_build_options.is_empty() {
PBAR.warn("cargo build options already specified with `--cargo-build`; ignoring `-- <extra_options>`");
}
}

// Unless empty, use `cargo_build_options` and ignore `extra_options`
if !cargo_build_options.is_empty() {
cmd.args(cargo_build_options);
} else {
cmd.args(extra_options);
}

child::run(cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?;
Ok(())
}
Expand Down
27 changes: 25 additions & 2 deletions src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub struct Build {
pub bindgen: Option<install::Status>,
pub cache: Cache,
pub extra_options: Vec<String>,
pub cargo_build_options: Vec<String>,
pub wasm_bindgen_options: Vec<String>,
}

/// What sort of output we're going to be generating and flags we're invoking
Expand Down Expand Up @@ -149,8 +151,19 @@ pub struct BuildOptions {
pub out_name: Option<String>,

#[structopt(last = true)]
/// List of extra options to pass to `cargo build`
/// Deprecated. Use `--cargo-build` instead. List of extra options to pass
/// to `cargo build`
pub extra_options: Vec<String>,

#[structopt(long = "cargo-build", use_delimiter(true), value_delimiter(" "))]
/// List of extra options to pass to `cargo build. Example:
/// --cargo-build="--foo --bar --baz"`
pub cargo_build_options: Vec<String>,

#[structopt(long = "wasm-bindgen", use_delimiter(true), value_delimiter(" "))]
/// List of extra options to pass to `cargo build. Example:
/// --wasm-bindgen="--foo --bar --baz"`
pub wasm_bindgen_options: Vec<String>,
}

impl Default for BuildOptions {
Expand All @@ -168,6 +181,8 @@ impl Default for BuildOptions {
out_dir: String::new(),
out_name: None,
extra_options: Vec::new(),
cargo_build_options: Vec::new(),
wasm_bindgen_options: Vec::new(),
}
}
}
Expand Down Expand Up @@ -204,6 +219,8 @@ impl Build {
bindgen: None,
cache: cache::get_wasm_pack_cache()?,
extra_options: build_opts.extra_options,
cargo_build_options: build_opts.cargo_build_options,
wasm_bindgen_options: build_opts.wasm_bindgen_options,
})
}

Expand Down Expand Up @@ -298,7 +315,12 @@ impl Build {

fn step_build_wasm(&mut self) -> Result<(), Error> {
info!("Building wasm...");
build::cargo_build_wasm(&self.crate_path, self.profile, &self.extra_options)?;
build::cargo_build_wasm(
&self.crate_path,
self.profile,
&self.cargo_build_options,
&self.extra_options,
)?;

info!(
"wasm built at {:#?}.",
Expand Down Expand Up @@ -372,6 +394,7 @@ impl Build {
self.disable_dts,
self.target,
self.profile,
&self.wasm_bindgen_options,
)?;
info!("wasm bindings were built at {:#?}.", &self.out_dir);
Ok(())
Expand Down
76 changes: 76 additions & 0 deletions tests/all/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,79 @@ fn build_from_new() {
.assert()
.success();
}

#[test]
fn dash_dash_args_allowed_with_warning() {
use predicates::prelude::*;
let fixture = utils::fixture::js_hello_world();
fixture.install_local_wasm_bindgen();
fixture
.wasm_pack()
.arg("build")
.arg("--")
.arg("--help")
.assert()
.stderr(predicate::str::contains(
"`-- <extra_options>` is deprecated; use `--cargo-build` instead",
))
.stdout(predicate::str::contains(
"Compile a local package and all of its dependencies",
))
.success();
}

#[test]
fn dash_dash_args_ignored_with_cargo_build_options() {
use predicates::prelude::*;
let fixture = utils::fixture::js_hello_world();
fixture.install_local_wasm_bindgen();
fixture
.wasm_pack()
.arg("build")
.arg("--cargo-build=--no-default-features --verbose")
.arg("--")
.arg("--help")
.assert()
.stderr(predicate::str::contains(
"`-- <extra_options>` is deprecated; use `--cargo-build` instead",
))
.stderr(predicate::str::contains(
"cargo build options already specified with `--cargo-build`; ignoring `-- <extra_options>`",
))
.stdout(predicate::str::contains(
"Compile a local package and all of its dependencies",
).not())
.success();
}

#[test]
fn cargo_build_options_passthrough() {
use predicates::prelude::*;
let fixture = utils::fixture::js_hello_world();
fixture.install_local_wasm_bindgen();
fixture
.wasm_pack()
.arg("build")
.arg("--cargo-build=--no-default-features --help")
.assert()
.stdout(predicate::str::contains(
"Compile a local package and all of its dependencies",
))
.success();
}

#[test]
fn wasm_bindgen_options_passthrough() {
use predicates::prelude::*;
let fixture = utils::fixture::js_hello_world();
fixture.install_local_wasm_bindgen();
fixture
.wasm_pack()
.arg("build")
.arg("--wasm-bindgen=--debug --help")
.assert()
.stdout(predicate::str::contains(
"Generating JS bindings for a wasm file",
))
.success();
}

0 comments on commit d282426

Please sign in to comment.