From 669c43d85371d191e61253796ac377f0a1aadb0b Mon Sep 17 00:00:00 2001 From: Vsevolod Velichko Date: Sun, 16 Dec 2018 12:22:51 +0300 Subject: [PATCH 1/2] add an option to pass an arbitrary set of arguments to cargo build --- src/build.rs | 2 ++ src/command/build.rs | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/build.rs b/src/build.rs index 4628d37..bd14b50 100644 --- a/src/build.rs +++ b/src/build.rs @@ -68,6 +68,7 @@ pub fn cargo_build_wasm( path: &Path, profile: BuildProfile, step: &Step, + extra_options: &Vec, ) -> Result<(), Error> { let msg = format!("{}Compiling to WASM...", emoji::CYCLONE); PBAR.step(step, &msg); @@ -91,6 +92,7 @@ pub fn cargo_build_wasm( } } cmd.arg("--target").arg("wasm32-unknown-unknown"); + cmd.args(extra_options); child::run(log, cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?; Ok(()) } diff --git a/src/command/build.rs b/src/command/build.rs index 9a5a152..c6545a3 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -31,6 +31,7 @@ pub struct Build { pub out_dir: PathBuf, pub bindgen: Option, pub cache: Cache, + pub extra_options: Vec, } /// The `BuildMode` determines which mode of initialization we are running, and @@ -120,6 +121,10 @@ pub struct BuildOptions { #[structopt(long = "out-dir", short = "d", default_value = "pkg")] /// Sets the output directory with a relative path. pub out_dir: String, + + #[structopt(last = true)] + /// List of extra options to pass to `cargo build` + pub extra_options: Vec, } impl Default for BuildOptions { @@ -135,6 +140,7 @@ impl Default for BuildOptions { release: false, profiling: false, out_dir: String::new(), + extra_options: Vec::new(), } } } @@ -175,6 +181,7 @@ impl Build { out_dir, bindgen: None, cache: Cache::new()?, + extra_options: build_opts.extra_options, }) } @@ -283,7 +290,13 @@ impl Build { fn step_build_wasm(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { info!(&log, "Building wasm..."); - build::cargo_build_wasm(log, &self.crate_path, self.profile, step)?; + build::cargo_build_wasm( + log, + &self.crate_path, + self.profile, + step, + &self.extra_options, + )?; info!( &log, From 43488dae1dcbe95a867df4208b9e0f3c1a79a9a1 Mon Sep 17 00:00:00 2001 From: Vsevolod Velichko Date: Wed, 19 Dec 2018 01:17:08 +0300 Subject: [PATCH 2/2] add test and some documentation for custom extra options passed to build command --- docs/src/commands/build.md | 11 +++++++++++ tests/all/build.rs | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/docs/src/commands/build.md b/docs/src/commands/build.md index 2305f50..96371fc 100644 --- a/docs/src/commands/build.md +++ b/docs/src/commands/build.md @@ -90,6 +90,17 @@ wasm-pack build examples/js-hello-world --mode no-install | `no-install` | `wasm-pack init` implicitly and create wasm binding without installing `wasm-bindgen`. | | `normal` | do all the stuffs of `no-install` with installed `wasm-bindgen`. | +## Extra options + +The `build` command can pass extra options straight to `cargo build` even if they are not +supported in wasm-pack. To use them you should add standalone `--` argument at the very +end of your command, and all the arguments you want to pass to cargo should go after. +For example to build previous example using unstable cargo offline feature: + +``` +wasm-pack build examples/js-hello-world --mode no-install -- -Z offline +``` +
0 If you need to include additional assets in the pkg diff --git a/tests/all/build.rs b/tests/all/build.rs index 7d06f15..1142805 100644 --- a/tests/all/build.rs +++ b/tests/all/build.rs @@ -248,3 +248,19 @@ fn it_format_out_dir_on_windows() { "directories in wasm-pack.log should be well formatted", ); } + +#[test] +fn build_with_arbitrary_cargo_options() { + let fixture = utils::fixture::js_hello_world(); + fixture.install_local_wasm_bindgen(); + + let cli = Cli::from_iter_safe(vec![ + "wasm-pack", + "build", + &fixture.path.display().to_string(), + "--", + "--no-default-features", + ]) + .unwrap(); + fixture.run(cli.cmd).unwrap(); +}