Skip to content

Commit

Permalink
Add rust link opts keep-debug & no-demangle.
Browse files Browse the repository at this point in the history
These new data-* options for <link rel="rust" .../> influence the way
that Trunk invokes wasm-bindgen, invoking the --keep-debug &
--no-demangle options respectively.

closes #135
  • Loading branch information
thedodd committed Mar 26, 2021
1 parent b74d95f commit f67a943
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 63 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ Currently supported asset types:
- `href`: (optional) the path to the `Cargo.toml` of the Rust project. If a directory is specified, then Trunk will look for the `Cargo.toml` in the given directory. If no value is specified, then Trunk will look for a `Cargo.toml` in the parent directory of the source HTML file.
- `data-bin`: (optional) the name of the binary to compile and use as the main WASM application. If the Cargo project has multiple binaries, this value will be required for proper functionality.
- `data-wasm-opt`: (optional) run wasm-opt with the set optimization level. wasm-opt is **turned off by default** but that may change in the future. 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.
- `data-keep-debug`: (optional) instruct `wasm-bindgen` to preserve debug info in the final WASM output, even for `--release` mode.
- `data-no-demangle`: (optional) instruct `wasm-bindgen` to not demangle Rust symbol names.
-`sass`, `scss`: Trunk ships with a [built-in sass/scss compiler](https://github.com/compass-rs/sass-rs). Just link to your sass files from your source HTML, and Trunk will handle the rest. This content is hashed for cache control. The `href` attribute must be included in the link pointing to the sass/scss file to be processed.
-`css`: Trunk will copy linked css files found in the source HTML without content modification. This content is hashed for cache control. The `href` attribute must be included in the link pointing to the css file to be processed.
- In the future, Trunk will resolve local `@imports`, will handle minification (see [trunk#7](https://github.com/thedodd/trunk/issues/3)), and we may even look into a pattern where any CSS found in the source tree will be bundled, which would enable a nice zero-config "component styles" pattern. See [trunk#3](https://github.com/thedodd/trunk/issues/3) for more details.
Expand Down
2 changes: 2 additions & 0 deletions site/content/assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..ot
- `href`: (optional) the path to the `Cargo.toml` of the Rust project. If a directory is specified, then Trunk will look for the `Cargo.toml` in the given directory. If no value is specified, then Trunk will look for a `Cargo.toml` in the parent directory of the source HTML file.
- `data-bin`: (optional) the name of the binary to compile and use as the main WASM application. If the Cargo project has multiple binaries, this value will be required for proper functionality.
- `data-wasm-opt`: (optional) run wasm-opt with the set optimization level. wasm-opt is **turned off by default** but that may change in the future. 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.
- `data-keep-debug`: (optional) instruct `wasm-bindgen` to preserve debug info in the final WASM output, even for `--release` mode.
- `data-no-demangle`: (optional) instruct `wasm-bindgen` to not demangle Rust symbol names.

## sass/scss
`rel="sass"` or `rel="scss"`: Trunk ships with a [built-in sass/scss compiler](https://github.com/compass-rs/sass-rs). Just link to your sass files from your source HTML, and Trunk will handle the rest. This content is hashed for cache control. The `href` attribute must be included in the link pointing to the sass/scss file to be processed.
Expand Down
143 changes: 80 additions & 63 deletions src/pipelines/rust_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,73 +30,16 @@ pub struct RustApp {
/// An optional binary name which will cause cargo & wasm-bindgen to process only the target
/// binary.
bin: Option<String>,
/// An option to instruct wasm-bindgen to preserve debug info in the final WASM output, even
/// for `--release` mode.
keep_debug: bool,
/// An option to instruct wasm-bindgen to not demangle Rust symbol names.
no_demangle: bool,
/// 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,
}

/// Different optimization levels that can be configured with `wasm-opt`.
#[derive(PartialEq, Eq)]
enum WasmOptLevel {
/// Default optimization passes.
Default,
/// No optimization passes, skipping the wasp-opt step.
Off,
/// Run quick & useful optimizations. useful for iteration testing.
One,
/// Most optimizations, generally gets most performance.
Two,
/// Spend potentially a lot of time optimizing.
Three,
/// Also flatten the IR, which can take a lot more time and memory, but is useful on more nested
/// / complex / less-optimized input.
Four,
/// Default optimizations, focus on code size.
S,
/// Default optimizations, super-focusing on code size.
Z,
}

impl FromStr for WasmOptLevel {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"" => Self::Default,
"0" => Self::Off,
"1" => Self::One,
"2" => Self::Two,
"3" => Self::Three,
"4" => Self::Four,
"s" | "S" => Self::S,
"z" | "Z" => Self::Z,
_ => bail!("unknown wasm-opt level `{}`", s),
})
}
}

impl AsRef<str> for WasmOptLevel {
fn as_ref(&self) -> &str {
match self {
Self::Default => "",
Self::Off => "0",
Self::One => "1",
Self::Two => "2",
Self::Three => "3",
Self::Four => "4",
Self::S => "s",
Self::Z => "z",
}
}
}

impl Default for WasmOptLevel {
fn default() -> Self {
// Current default is off until automatic download of wasm-opt is implemented.
Self::Off
}
}

impl RustApp {
pub const TYPE_RUST_APP: &'static str = "rust";

Expand All @@ -117,6 +60,8 @@ impl RustApp {
})
.unwrap_or_else(|| html_dir.join("Cargo.toml"));
let bin = attrs.get("data-bin").map(|val| val.to_string());
let keep_debug = attrs.contains_key("data-keep-debug");
let no_demangle = attrs.contains_key("data-no-demangle");
let wasm_opt = attrs.get("data-wasm-opt").map(|val| val.parse()).transpose()?.unwrap_or_default();
let manifest = CargoMetadata::new(&manifest_href).await?;
let id = Some(id);
Expand All @@ -127,6 +72,8 @@ impl RustApp {
manifest,
ignore_chan,
bin,
keep_debug,
no_demangle,
wasm_opt,
})
}
Expand All @@ -140,6 +87,8 @@ impl RustApp {
manifest,
ignore_chan,
bin: None,
keep_debug: false,
no_demangle: false,
wasm_opt: WasmOptLevel::default(),
})
}
Expand Down Expand Up @@ -245,7 +194,13 @@ impl RustApp {
let arg_out_path = format!("--out-dir={}", bindgen_out.display());
let arg_out_name = format!("--out-name={}", &hashed_name);
let target_wasm = wasm.to_string_lossy().to_string();
let args = vec!["--target=web", &arg_out_path, &arg_out_name, "--no-typescript", &target_wasm];
let mut args = vec!["--target=web", &arg_out_path, &arg_out_name, "--no-typescript", &target_wasm];
if self.keep_debug {
args.push("--keep-debug");
}
if self.no_demangle {
args.push("--no-demangle");
}

// Invoke wasm-bindgen.
run_command("wasm-bindgen", &args).await?;
Expand Down Expand Up @@ -360,3 +315,65 @@ async fn run_command(name: &str, args: &[impl AsRef<OsStr>]) -> Result<()> {
}
Ok(())
}

/// Different optimization levels that can be configured with `wasm-opt`.
#[derive(PartialEq, Eq)]
enum WasmOptLevel {
/// Default optimization passes.
Default,
/// No optimization passes, skipping the wasp-opt step.
Off,
/// Run quick & useful optimizations. useful for iteration testing.
One,
/// Most optimizations, generally gets most performance.
Two,
/// Spend potentially a lot of time optimizing.
Three,
/// Also flatten the IR, which can take a lot more time and memory, but is useful on more nested
/// / complex / less-optimized input.
Four,
/// Default optimizations, focus on code size.
S,
/// Default optimizations, super-focusing on code size.
Z,
}

impl FromStr for WasmOptLevel {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"" => Self::Default,
"0" => Self::Off,
"1" => Self::One,
"2" => Self::Two,
"3" => Self::Three,
"4" => Self::Four,
"s" | "S" => Self::S,
"z" | "Z" => Self::Z,
_ => bail!("unknown wasm-opt level `{}`", s),
})
}
}

impl AsRef<str> for WasmOptLevel {
fn as_ref(&self) -> &str {
match self {
Self::Default => "",
Self::Off => "0",
Self::One => "1",
Self::Two => "2",
Self::Three => "3",
Self::Four => "4",
Self::S => "s",
Self::Z => "z",
}
}
}

impl Default for WasmOptLevel {
fn default() -> Self {
// Current default is off until automatic download of wasm-opt is implemented.
Self::Off
}
}

0 comments on commit f67a943

Please sign in to comment.