Skip to main content

Dist

Struct Dist 

Source
#[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>,
}
Available on non-WebAssembly only.
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
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§quiet: bool

No 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: bool

Build artifacts in release mode, with optimizations.

§features: Vec<String>

Space or comma separated list of features to activate.

§all_features: bool

Activate all available features.

§no_default_features: bool

Do not activate the default features.

§verbose: bool

Use verbose output

§color: Option<String>

Coloring: auto, always, never.

§frozen: bool

Require Cargo.lock and cache are up to date.

§locked: bool

Require Cargo.lock is up to date.

§offline: bool

Run without accessing the network.

§ignore_rust_version: bool

Ignore rust-version specification in packages.

§example: Option<String>

Name of the example target to run.

§build_command: Command

Command 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>
Available on crate feature 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

Source

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.

Source

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.

Source

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.

Source

pub fn app_name(self, app_name: impl Into<String>) -> Self

Set the resulting package name.

The default is app.

Source

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.

Source

pub fn optimize_wasm(self, wasm_opt: WasmOpt) -> Self

Available on crate feature 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(())
}
Source

pub fn example(self, example: impl Into<String>) -> Self

Set the example to build.

Source

pub fn default_debug_dir() -> Utf8PathBuf

Get the default dist directory for debug builds.

Source

pub fn default_release_dir() -> Utf8PathBuf

Get the default dist directory for release builds.

Source

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

Source§

fn group_id() -> Option<Id>

Report the ArgGroup::id for this set of arguments
Source§

fn augment_args<'b>(__clap_app: Command) -> Command

Append to Command so it can instantiate Self via FromArgMatches::from_arg_matches_mut Read more
Source§

fn augment_args_for_update<'b>(__clap_app: Command) -> Command

Append to Command so it can instantiate self via FromArgMatches::update_from_arg_matches_mut Read more
Source§

impl CommandFactory for Dist

Source§

fn command<'b>() -> Command

Build a Command that can instantiate Self. Read more
Source§

fn command_for_update<'b>() -> Command

Build a Command that can update self. Read more
Source§

impl Debug for Dist

Source§

fn fmt(&self, __derive_more_f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Dist

Source§

fn default() -> Dist

Returns the “default value” for a type. Read more
Source§

impl FromArgMatches for Dist

Source§

fn from_arg_matches(__clap_arg_matches: &ArgMatches) -> Result<Self, Error>

Instantiate Self from ArgMatches, parsing the arguments as needed. Read more
Source§

fn from_arg_matches_mut( __clap_arg_matches: &mut ArgMatches, ) -> Result<Self, Error>

Instantiate Self from ArgMatches, parsing the arguments as needed. Read more
Source§

fn update_from_arg_matches( &mut self, __clap_arg_matches: &ArgMatches, ) -> Result<(), Error>

Assign values from ArgMatches to self.
Source§

fn update_from_arg_matches_mut( &mut self, __clap_arg_matches: &mut ArgMatches, ) -> Result<(), Error>

Assign values from ArgMatches to self.
Source§

impl Parser for Dist

Source§

fn parse() -> Self

Parse from std::env::args_os(), exit on error.
Source§

fn try_parse() -> Result<Self, Error>

Parse from std::env::args_os(), return Err on error.
Source§

fn parse_from<I, T>(itr: I) -> Self
where I: IntoIterator<Item = T>, T: Into<OsString> + Clone,

Parse from iterator, exit on error.
Source§

fn try_parse_from<I, T>(itr: I) -> Result<Self, Error>
where I: IntoIterator<Item = T>, T: Into<OsString> + Clone,

Parse from iterator, return Err on error.
Source§

fn update_from<I, T>(&mut self, itr: I)
where I: IntoIterator<Item = T>, T: Into<OsString> + Clone,

Update from iterator, exit on error. Read more
Source§

fn try_update_from<I, T>(&mut self, itr: I) -> Result<(), Error>
where I: IntoIterator<Item = T>, T: Into<OsString> + Clone,

Update from iterator, return Err on error.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,