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

add an option to pass an arbitrary set of arguments to cargo build #461

Merged
merged 3 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions docs/src/commands/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

<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
Expand Down
2 changes: 2 additions & 0 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ 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);
PBAR.step(step, &msg);
Expand All @@ -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(())
}
Expand Down
15 changes: 14 additions & 1 deletion src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct Build {
pub out_dir: PathBuf,
pub bindgen: Option<Download>,
pub cache: Cache,
pub extra_options: Vec<String>,
}

/// The `BuildMode` determines which mode of initialization we are running, and
Expand Down Expand Up @@ -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<String>,
}

impl Default for BuildOptions {
Expand All @@ -135,6 +140,7 @@ impl Default for BuildOptions {
release: false,
profiling: false,
out_dir: String::new(),
extra_options: Vec::new(),
}
}
}
Expand Down Expand Up @@ -175,6 +181,7 @@ impl Build {
out_dir,
bindgen: None,
cache: Cache::new()?,
extra_options: build_opts.extra_options,
})
}

Expand Down Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions tests/all/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}