Merge pull request #461 from torkve/master

add an option to pass an arbitrary set of arguments to cargo build
master
ashley williams 6 years ago committed by GitHub
commit 5258d42b5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      docs/src/commands/build.md
  2. 8
      src/build.rs
  3. 9
      src/command/build.rs
  4. 16
      tests/all/build.rs

@ -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`. | | `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`. | | `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
```
<hr style="font-size: 1.5em; margin-top: 2.5em"/> <hr style="font-size: 1.5em; margin-top: 2.5em"/>
<sup id="footnote-0">0</sup> If you need to include additional assets in the pkg <sup id="footnote-0">0</sup> If you need to include additional assets in the pkg

@ -61,7 +61,12 @@ pub fn rustup_add_wasm_target(step: &Step) -> Result<(), Error> {
} }
/// Run `cargo build` targetting `wasm32-unknown-unknown`. /// Run `cargo build` targetting `wasm32-unknown-unknown`.
pub fn cargo_build_wasm(path: &Path, profile: BuildProfile, step: &Step) -> Result<(), Error> { pub fn cargo_build_wasm(
path: &Path,
profile: BuildProfile,
step: &Step,
extra_options: &Vec<String>,
) -> Result<(), Error> {
let msg = format!("{}Compiling to WASM...", emoji::CYCLONE); let msg = format!("{}Compiling to WASM...", emoji::CYCLONE);
PBAR.step(step, &msg); PBAR.step(step, &msg);
let mut cmd = Command::new("cargo"); let mut cmd = Command::new("cargo");
@ -84,6 +89,7 @@ pub fn cargo_build_wasm(path: &Path, profile: BuildProfile, step: &Step) -> Resu
} }
} }
cmd.arg("--target").arg("wasm32-unknown-unknown"); cmd.arg("--target").arg("wasm32-unknown-unknown");
cmd.args(extra_options);
child::run(cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?; child::run(cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?;
Ok(()) Ok(())
} }

@ -31,6 +31,7 @@ pub struct Build {
pub out_dir: PathBuf, pub out_dir: PathBuf,
pub bindgen: Option<Download>, pub bindgen: Option<Download>,
pub cache: Cache, pub cache: Cache,
pub extra_options: Vec<String>,
} }
/// The `BuildMode` determines which mode of initialization we are running, and /// 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")] #[structopt(long = "out-dir", short = "d", default_value = "pkg")]
/// Sets the output directory with a relative path. /// Sets the output directory with a relative path.
pub out_dir: String, pub out_dir: String,
#[structopt(last = true)]
/// List of extra options to pass to `cargo build`
pub extra_options: Vec<String>,
} }
impl Default for BuildOptions { impl Default for BuildOptions {
@ -135,6 +140,7 @@ impl Default for BuildOptions {
release: false, release: false,
profiling: false, profiling: false,
out_dir: String::new(), out_dir: String::new(),
extra_options: Vec::new(),
} }
} }
} }
@ -175,6 +181,7 @@ impl Build {
out_dir, out_dir,
bindgen: None, bindgen: None,
cache: Cache::new()?, cache: Cache::new()?,
extra_options: build_opts.extra_options,
}) })
} }
@ -282,7 +289,7 @@ impl Build {
fn step_build_wasm(&mut self, step: &Step) -> Result<(), Error> { fn step_build_wasm(&mut self, step: &Step) -> Result<(), Error> {
info!("Building wasm..."); info!("Building wasm...");
build::cargo_build_wasm(&self.crate_path, self.profile, step)?; build::cargo_build_wasm(&self.crate_path, self.profile, step, &self.extra_options)?;
info!( info!(
"wasm built at {:#?}.", "wasm built at {:#?}.",

@ -228,3 +228,19 @@ fn build_with_and_without_wasm_bindgen_debug() {
); );
} }
} }
#[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();
}

Loading…
Cancel
Save