Skip to content

Commit

Permalink
feat: add data-wasm-opt-params
Browse files Browse the repository at this point in the history
  • Loading branch information
maratik123 committed Oct 31, 2024
1 parent 340ab32 commit 246c057
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions guide/src/assets/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..ot
- `data-cargo-all-features`: (optional) Enables all Cargo features.
- Neither compatible with `data-cargo-features` nor `data-cargo-no-default-features`.
- `data-wasm-opt`: (optional) run wasm-opt with the set optimization level. The possible values are `0`, `1`, `2`, `3`, `4`, `s`, `z` or an _empty value_ for wasm-opt's default. Set this option to `0` to disable wasm-opt explicitly. The values `1-4` are increasingly stronger optimization levels for speed. `s` and `z` (z means more optimization) optimize for binary size instead. Only used in `--release` mode.
- `data-wasm-opt-params`: (optional) run wasm-opt with the additional params. Only used in `--release` mode.
- `data-keep-debug`: (optional) instruct `wasm-bindgen` to preserve debug info in the final WASM output, even for `--release` mode. This may conflict with the use of wasm-opt, so to be sure, it is recommended to set `data-wasm-opt="0"` when using this option.
- `data-no-demangle`: (optional) instruct `wasm-bindgen` to not demangle Rust symbol names.
- `data-reference-types`: (optional) instruct `wasm-bindgen` to enable [reference types](https://rustwasm.github.io/docs/wasm-bindgen/reference/reference-types.html).
Expand Down
1 change: 1 addition & 0 deletions site/content/assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..ot
- `data-cargo-all-features`: (optional) Enables all Cargo features.
- Neither compatible with `data-cargo-features` nor `data-cargo-no-default-features`.
- `data-wasm-opt`: (optional) run wasm-opt with the set optimization level. The possible values are `0`, `1`, `2`, `3`, `4`, `s`, `z` or an _empty value_ for wasm-opt's default. Set this option to `0` to disable wasm-opt explicitly. The values `1-4` are increasingly stronger optimization levels for speed. `s` and `z` (z means more optimization) optimize for binary size instead. Only used in `--release` mode.
- `data-wasm-opt-params`: (optional) run wasm-opt with the additional params. Only used in `--release` mode.
- `data-keep-debug`: (optional) instruct `wasm-bindgen` to preserve debug info in the final WASM output, even for `--release` mode. This may conflict with the use of wasm-opt, so to be sure, it is recommended to set `data-wasm-opt="0"` when using this option.
- `data-no-demangle`: (optional) instruct `wasm-bindgen` to not demangle Rust symbol names.
- `data-reference-types`: (optional) instruct `wasm-bindgen` to enable [reference types](https://rustwasm.github.io/docs/wasm-bindgen/reference/reference-types.html).
Expand Down
10 changes: 10 additions & 0 deletions src/pipelines/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ pub struct RustApp {
/// An optional optimization setting that enables wasm-opt. Can be nothing, `0` (default), `1`,
/// `2`, `3`, `4`, `s or `z`. Using `0` disables wasm-opt completely.
wasm_opt: WasmOptLevel,
/// An optional optimization command line params to wasm-opt if it is enabled.
wasm_opt_params: Option<String>,
/// The value of the `--target` flag for wasm-bindgen.
wasm_bindgen_target: WasmBindgenTarget,
/// Name for the module. Is binary name if given, otherwise it is the name of the cargo
Expand Down Expand Up @@ -169,6 +171,7 @@ impl RustApp {
WasmOptLevel::Off
}
});
let wasm_opt_params = attrs.get("data-wasm-opt-params").cloned();
let wasm_bindgen_target = attrs
.get("data-bindgen-target")
.map(|s| s.parse())
Expand Down Expand Up @@ -282,6 +285,7 @@ impl RustApp {
reference_types,
weak_refs,
wasm_opt,
wasm_opt_params,
wasm_bindgen_target,
app_type,
name,
Expand Down Expand Up @@ -331,6 +335,7 @@ impl RustApp {
reference_types: false,
weak_refs: false,
wasm_opt: WasmOptLevel::Off,
wasm_opt_params: None,
app_type: RustAppType::Main,
wasm_bindgen_target: WasmBindgenTarget::Web,
name,
Expand Down Expand Up @@ -890,6 +895,7 @@ impl RustApp {
let output = output.join(format!("{}_bg.wasm", self.name));
let arg_output = format!("--output={output}");
let arg_opt_level = format!("-O{}", self.wasm_opt.as_ref());
let arg_opt_params = self.wasm_opt_params.as_ref();
let target_wasm = self
.cfg
.staging_dist
Expand All @@ -902,6 +908,10 @@ impl RustApp {
args.push("--enable-reference-types");
}

if let Some(arg_opt_params) = arg_opt_params {
args.extend(arg_opt_params.split_whitespace());
}

// Invoke wasm-opt.
tracing::debug!("calling wasm-opt");
common::run_command(wasm_opt_name, &wasm_opt, &args, &self.cfg.working_directory)
Expand Down

0 comments on commit 246c057

Please sign in to comment.