-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: pull out some code, turn struct into enum
- Loading branch information
Showing
3 changed files
with
164 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use crate::config::{CargoMetadata, ConfigOptsTools}; | ||
use anyhow::anyhow; | ||
use cargo_lock::Lockfile; | ||
use std::borrow::Cow; | ||
use std::fmt::{Display, Formatter}; | ||
use std::path::Path; | ||
use std::str::FromStr; | ||
|
||
/// Determines the value of `--target` flag for wasm-bindgen. For more details see | ||
/// [here](https://rustwasm.github.io/wasm-bindgen/reference/deployment.html). | ||
#[derive(Clone, Debug, Default, PartialEq, Eq)] | ||
pub enum WasmBindgenTarget { | ||
Bundler, | ||
#[default] | ||
Web, | ||
NoModules, | ||
NodeJs, | ||
Deno, | ||
} | ||
|
||
impl FromStr for WasmBindgenTarget { | ||
type Err = anyhow::Error; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
Ok(match s { | ||
"bundler" => Self::Bundler, | ||
"web" => Self::Web, | ||
"no-modules" => Self::NoModules, | ||
"nodejs" => Self::NodeJs, | ||
"deno" => Self::Deno, | ||
s => { | ||
return Err(anyhow!( | ||
r#"unknown `data-bindgen-target="{s}"` value for <link data-trunk rel="rust" .../> attr; please ensure the value is lowercase and is a supported type"# | ||
)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
impl Display for WasmBindgenTarget { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Self::Bundler => f.write_str("bundler"), | ||
Self::Web => f.write_str("web"), | ||
Self::NoModules => f.write_str("no-modules"), | ||
Self::NodeJs => f.write_str("nodejs"), | ||
Self::Deno => f.write_str("deno"), | ||
} | ||
} | ||
} | ||
|
||
/// Find the appropriate version of `wasm-bindgen` to use. The version can be found in 3 different | ||
/// location in order: | ||
/// - Defined in the `Trunk.toml` as highest priority. | ||
/// - Located in the `Cargo.lock` if it exists. This is mostly the case as we run `cargo build` | ||
/// before even calling this function. | ||
/// - Located in the `Cargo.toml` as direct dependency of the project. | ||
pub fn find_wasm_bindgen_version<'a>( | ||
cfg: &'a ConfigOptsTools, | ||
manifest: &CargoMetadata, | ||
) -> Option<Cow<'a, str>> { | ||
let find_lock = || -> Option<Cow<'_, str>> { | ||
let lock_path = Path::new(&manifest.manifest_path) | ||
.parent()? | ||
.join("Cargo.lock"); | ||
let lockfile = Lockfile::load(lock_path).ok()?; | ||
let name = "wasm-bindgen".parse().ok()?; | ||
|
||
lockfile | ||
.packages | ||
.into_iter() | ||
.find(|p| p.name == name) | ||
.map(|p| Cow::from(p.version.to_string())) | ||
}; | ||
|
||
let find_manifest = || -> Option<Cow<'_, str>> { | ||
manifest | ||
.metadata | ||
.packages | ||
.iter() | ||
.find(|p| p.name == "wasm-bindgen") | ||
.map(|p| Cow::from(p.version.to_string())) | ||
}; | ||
|
||
cfg.wasm_bindgen | ||
.as_deref() | ||
.map(Cow::from) | ||
.or_else(find_lock) | ||
.or_else(find_manifest) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use anyhow::bail; | ||
use std::str::FromStr; | ||
|
||
/// Different optimization levels that can be configured with `wasm-opt`. | ||
#[derive(PartialEq, Eq)] | ||
pub 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) -> anyhow::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 { | ||
Self::Default | ||
} | ||
} |