Skip to content

Commit d9fdd63

Browse files
Merge pull request #127 from clanehin/master
Add a --debug option to suppress cargo build --release.
2 parents 53d6641 + b393e4b commit d9fdd63

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

docs/init.md

+11
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,15 @@ This command would create a `package.json` file for a package called
4646
`@test/js-hello-world`. For more information about scoping, you can refer to
4747
the npm documentation [here][npm-scope-documentation].
4848

49+
## Debug
50+
51+
The init command accepts an optional `--debug` argument. This will build the
52+
output package using cargo's
53+
[default non-release profile][cargo-profile-sections-documentation]. Building
54+
this way is faster but applies few optimizations to the output, and enables
55+
debug assertions and other runtime correctness checks.
56+
57+
The exact meaning of this flag may evolve as the platform matures.
58+
4959
[npm-scope-documentation]: https://docs.npmjs.com/misc/scope
60+
[cargo-profile-sections-documentation]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-profile-sections

src/bindgen.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub fn wasm_bindgen_build(
3434
name: &str,
3535
disable_dts: bool,
3636
target: String,
37+
debug: bool,
3738
) -> Result<(), Error> {
3839
let step = format!(
3940
"{} {}Running WASM-bindgen...",
@@ -42,8 +43,11 @@ pub fn wasm_bindgen_build(
4243
);
4344
let pb = PBAR.message(&step);
4445
let binary_name = name.replace("-", "_");
45-
let wasm_path = format!("target/wasm32-unknown-unknown/release/{}.wasm", binary_name);
46-
46+
let release_or_debug = if debug { "debug" } else { "release" };
47+
let wasm_path = format!(
48+
"target/wasm32-unknown-unknown/{}/{}.wasm",
49+
release_or_debug, binary_name
50+
);
4751
let dts_arg = if disable_dts == false {
4852
"--typescript"
4953
} else {

src/build.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,22 @@ pub fn rustup_add_wasm_target() -> Result<(), Error> {
2525
}
2626
}
2727

28-
pub fn cargo_build_wasm(path: &str) -> Result<(), Error> {
28+
pub fn cargo_build_wasm(path: &str, debug: bool) -> Result<(), Error> {
2929
let step = format!(
3030
"{} {}Compiling to WASM...",
3131
style("[2/7]").bold().dim(),
3232
emoji::CYCLONE
3333
);
3434
let pb = PBAR.message(&step);
35-
let output = Command::new("cargo")
36-
.current_dir(path)
37-
.arg("build")
38-
.arg("--release")
39-
.arg("--target")
40-
.arg("wasm32-unknown-unknown")
41-
.output()?;
35+
let output = {
36+
let mut cmd = Command::new("cargo");
37+
cmd.current_dir(path).arg("build");
38+
if !debug {
39+
cmd.arg("--release");
40+
}
41+
cmd.arg("--target").arg("wasm32-unknown-unknown");
42+
cmd.output()?
43+
};
4244
pb.finish();
4345
if !output.status.success() {
4446
let s = String::from_utf8_lossy(&output.stderr);

src/command.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ pub enum Command {
3232
#[structopt(long = "target", short = "t", default_value = "browser")]
3333
/// Sets the target environment. [possible values: browser, nodejs]
3434
target: String,
35+
36+
#[structopt(long = "debug")]
37+
/// Build without --release.
38+
debug: bool,
3539
},
3640

3741
#[structopt(name = "pack")]
@@ -84,17 +88,19 @@ pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error
8488
scope,
8589
disable_dts,
8690
target,
91+
debug,
8792
} => {
8893
info!(&log, "Running init command...");
8994
info!(
9095
&log,
91-
"Path: {:?}, Scope: {:?}, Disable Dts: {}, Target: {}",
96+
"Path: {:?}, Scope: {:?}, Disable Dts: {}, Target: {}, Debug: {}",
9297
&path,
9398
&scope,
9499
&disable_dts,
95-
&target
100+
&target,
101+
debug
96102
);
97-
init(path, scope, disable_dts, target, &log)
103+
init(path, scope, disable_dts, target, &log, debug)
98104
}
99105
Command::Pack { path } => {
100106
info!(&log, "Running pack command...");
@@ -164,6 +170,7 @@ fn init(
164170
disable_dts: bool,
165171
target: String,
166172
log: &Logger,
173+
debug: bool,
167174
) -> result::Result<(), Error> {
168175
let started = Instant::now();
169176

@@ -174,7 +181,7 @@ fn init(
174181
info!(&log, "Adding wasm-target was successful.");
175182

176183
info!(&log, "Building wasm...");
177-
build::cargo_build_wasm(&crate_path)?;
184+
build::cargo_build_wasm(&crate_path, debug)?;
178185

179186
#[cfg(not(target_os = "windows"))]
180187
info!(
@@ -229,7 +236,7 @@ fn init(
229236
);
230237

231238
info!(&log, "Building the wasm bindings...");
232-
bindgen::wasm_bindgen_build(&crate_path, &name, disable_dts, target)?;
239+
bindgen::wasm_bindgen_build(&crate_path, &name, disable_dts, target, debug)?;
233240
#[cfg(not(target_os = "windows"))]
234241
info!(&log, "wasm bindings were built at {}/pkg.", &crate_path);
235242
#[cfg(target_os = "windows")]

0 commit comments

Comments
 (0)