#[non_exhaustive]pub struct Dist {Show 20 fields
pub quiet: bool,
pub jobs: Option<String>,
pub profile: Option<String>,
pub release: bool,
pub features: Vec<String>,
pub all_features: bool,
pub no_default_features: bool,
pub verbose: bool,
pub color: Option<String>,
pub frozen: bool,
pub locked: bool,
pub offline: bool,
pub ignore_rust_version: bool,
pub example: Option<String>,
pub build_command: Command,
pub dist_dir: Option<PathBuf>,
pub assets_dir: Option<PathBuf>,
pub app_name: Option<String>,
pub transformers: Vec<Box<dyn Transformer>>,
pub wasm_opt: Option<WasmOpt>,
}Expand description
A helper to generate the distributed package.
§Usage
use std::process;
use xtask_wasm::{anyhow::Result, clap};
#[derive(clap::Parser)]
enum Opt {
Dist(xtask_wasm::Dist),
}
fn main() -> Result<()> {
let opt: Opt = clap::Parser::parse();
match opt {
Opt::Dist(dist) => {
log::info!("Generating package...");
dist
.assets_dir("my-project/assets")
.app_name("my-project")
.build("my-project")?;
}
}
Ok(())
}In this example, we added a dist subcommand to build and package the
my-project crate. It will run the default_build_command
at the workspace root, copy the content of the my-project/assets directory,
generate JS bindings and output two files: my-project.js and my-project.wasm
into the dist directory.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.quiet: boolNo output printed to stdout.
jobs: Option<String>Number of parallel jobs, defaults to # of CPUs.
profile: Option<String>Build artifacts with the specified profile.
release: boolBuild artifacts in release mode, with optimizations.
features: Vec<String>Space or comma separated list of features to activate.
all_features: boolActivate all available features.
no_default_features: boolDo not activate the default features.
verbose: boolUse verbose output
color: Option<String>Coloring: auto, always, never.
frozen: boolRequire Cargo.lock and cache are up to date.
locked: boolRequire Cargo.lock is up to date.
offline: boolRun without accessing the network.
ignore_rust_version: boolIgnore rust-version specification in packages.
example: Option<String>Name of the example target to run.
build_command: CommandCommand passed to the build process.
dist_dir: Option<PathBuf>Directory of all generated artifacts.
assets_dir: Option<PathBuf>Directory of all static assets artifacts.
Default to assets in the package root when it exists.
app_name: Option<String>Set the resulting app name, default to app.
transformers: Vec<Box<dyn Transformer>>Transformers applied to each file in the assets directory during the build.
Each transformer is called in order for every file; the first one that returns
Ok(true) claims the file and the rest are skipped. Files not claimed by any
transformer are copied verbatim into the dist directory.
wasm_opt: Option<WasmOpt>wasm-opt only.Optional wasm-opt pass to run on the generated Wasm binary after bindgen.
Set via Dist::optimize_wasm. Only available when the wasm-opt feature is enabled.
Implementations§
Source§impl Dist
impl Dist
Sourcepub fn build_command(self, command: Command) -> Self
pub fn build_command(self, command: Command) -> Self
Set the command used by the build process.
The default command is the result of the default_build_command.
Sourcepub fn dist_dir(self, path: impl Into<PathBuf>) -> Self
pub fn dist_dir(self, path: impl Into<PathBuf>) -> Self
Set the directory for the generated artifacts.
The default for debug build is target/debug/dist and
target/release/dist for the release build.
Sourcepub fn assets_dir(self, path: impl Into<PathBuf>) -> Self
pub fn assets_dir(self, path: impl Into<PathBuf>) -> Self
Set the directory for the static assets artifacts (like index.html).
Default to assets in the package root when it exists.
Sourcepub fn app_name(self, app_name: impl Into<String>) -> Self
pub fn app_name(self, app_name: impl Into<String>) -> Self
Set the resulting package name.
The default is app.
Sourcepub fn transformer(self, transformer: impl Transformer + 'static) -> Self
pub fn transformer(self, transformer: impl Transformer + 'static) -> Self
Add a transformer for the asset copy step.
Transformers are called in the order they are added. See Transformer for details.
Sourcepub fn optimize_wasm(self, wasm_opt: WasmOpt) -> Self
Available on crate feature wasm-opt only.
pub fn optimize_wasm(self, wasm_opt: WasmOpt) -> Self
wasm-opt only.Run WasmOpt on the generated Wasm binary after the bindgen step.
This is the recommended way to integrate wasm-opt: it runs automatically at the
end of build using the resolved output path, so you do not need to
wrap Dist in a custom struct or compute the path manually.
The optimization is skipped for debug builds — it only runs when release
is true. A log::debug! message is emitted when it is skipped.
Requires the wasm-opt feature.
§Examples
use xtask_wasm::{anyhow::Result, clap, WasmOpt};
#[derive(clap::Parser)]
enum Opt {
Dist(xtask_wasm::Dist),
}
fn main() -> Result<()> {
let opt: Opt = clap::Parser::parse();
match opt {
Opt::Dist(dist) => {
dist.optimize_wasm(WasmOpt::level(1).shrink(2))
.build("my-project")?;
}
}
Ok(())
}Sourcepub fn default_debug_dir() -> Utf8PathBuf
pub fn default_debug_dir() -> Utf8PathBuf
Get the default dist directory for debug builds.
Sourcepub fn default_release_dir() -> Utf8PathBuf
pub fn default_release_dir() -> Utf8PathBuf
Get the default dist directory for release builds.
Sourcepub fn build(self, package_name: &str) -> Result<PathBuf>
pub fn build(self, package_name: &str) -> Result<PathBuf>
Build the given package for Wasm.
This will generate JS bindings via wasm-bindgen
and copy files from a given assets directory if any to finally return
the path of the generated artifacts.
Wasm optimizations can be achieved using WasmOpt if the feature wasm-opt is enabled.
Trait Implementations§
Source§impl Args for Dist
impl Args for Dist
Source§fn augment_args<'b>(__clap_app: Command) -> Command
fn augment_args<'b>(__clap_app: Command) -> Command
Source§fn augment_args_for_update<'b>(__clap_app: Command) -> Command
fn augment_args_for_update<'b>(__clap_app: Command) -> Command
Command so it can instantiate self via
FromArgMatches::update_from_arg_matches_mut Read moreSource§impl CommandFactory for Dist
impl CommandFactory for Dist
Source§impl FromArgMatches for Dist
impl FromArgMatches for Dist
Source§fn from_arg_matches(__clap_arg_matches: &ArgMatches) -> Result<Self, Error>
fn from_arg_matches(__clap_arg_matches: &ArgMatches) -> Result<Self, Error>
Source§fn from_arg_matches_mut(
__clap_arg_matches: &mut ArgMatches,
) -> Result<Self, Error>
fn from_arg_matches_mut( __clap_arg_matches: &mut ArgMatches, ) -> Result<Self, Error>
Source§fn update_from_arg_matches(
&mut self,
__clap_arg_matches: &ArgMatches,
) -> Result<(), Error>
fn update_from_arg_matches( &mut self, __clap_arg_matches: &ArgMatches, ) -> Result<(), Error>
ArgMatches to self.Source§fn update_from_arg_matches_mut(
&mut self,
__clap_arg_matches: &mut ArgMatches,
) -> Result<(), Error>
fn update_from_arg_matches_mut( &mut self, __clap_arg_matches: &mut ArgMatches, ) -> Result<(), Error>
ArgMatches to self.Source§impl Parser for Dist
impl Parser for Dist
Source§fn parse_from<I, T>(itr: I) -> Self
fn parse_from<I, T>(itr: I) -> Self
Source§fn try_parse_from<I, T>(itr: I) -> Result<Self, Error>
fn try_parse_from<I, T>(itr: I) -> Result<Self, Error>
Source§fn update_from<I, T>(&mut self, itr: I)
fn update_from<I, T>(&mut self, itr: I)
Auto Trait Implementations§
impl Freeze for Dist
impl !RefUnwindSafe for Dist
impl !Send for Dist
impl !Sync for Dist
impl Unpin for Dist
impl UnsafeUnpin for Dist
impl !UnwindSafe for Dist
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more