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

feat: add data-wasm-opt-params #901

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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
13 changes: 13 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: Vec<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,12 @@ impl RustApp {
WasmOptLevel::Off
}
});
let wasm_opt_params = attrs
.get("data-wasm-opt-params")
.iter()
.flat_map(|val| val.split_whitespace())
.map(|val| val.to_string())
.collect();
let wasm_bindgen_target = attrs
.get("data-bindgen-target")
.map(|s| s.parse())
Expand Down Expand Up @@ -282,6 +290,7 @@ impl RustApp {
reference_types,
weak_refs,
wasm_opt,
wasm_opt_params,
wasm_bindgen_target,
app_type,
name,
Expand Down Expand Up @@ -331,6 +340,7 @@ impl RustApp {
reference_types: false,
weak_refs: false,
wasm_opt: WasmOptLevel::Off,
wasm_opt_params: Default::default(),
app_type: RustAppType::Main,
wasm_bindgen_target: WasmBindgenTarget::Web,
name,
Expand Down Expand Up @@ -890,6 +900,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_slice();
let target_wasm = self
.cfg
.staging_dist
Expand All @@ -902,6 +913,8 @@ impl RustApp {
args.push("--enable-reference-types");
}

args.extend(arg_opt_params.iter().map(|s| s.as_str()));

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