xtask-wasm

actions status crate version documentation dependencies status licenses

This crate aims to provide an easy and customizable way to help you build Wasm projects by extending them with custom subcommands, based on the xtask concept, instead of using external tooling like wasm-pack.

Minimum Supported Rust Version

This crate requires Rust 1.58.1 at a minimum because there is a security issue on a function we use from std in previous version (see cve-2022-21658).

Setup

The best way to add xtask-wasm to your project is to create a workspace with two packages: your project's package and the xtask package.

Create a project using xtask

The directory layout should look like this:

console project ├── .cargo │ └── config.toml ├── Cargo.toml ├── my-project │   ├── Cargo.toml │   └── src │      └── ... └── xtask ├── Cargo.toml └── src └── main.rs

And now you can run your xtask package using:

console cargo xtask

You can find more informations about xtask here.

Use xtask-wasm as a dependency

Finally, add the following to the xtask package's Cargo.toml:

toml [dependencies] xtask-wasm = "0.1.1"

Usage

This library gives you three structs:

They all implement clap::Parser allowing them to be added easily to an existing CLI implementation and are flexible enough to be customized for most use-cases.

You can find further information for each type at their documentation level.

Examples

A basic implementation

```rust use std::process::Command; use xtaskwasm::{anyhow::Result, clap, defaultdist_dir};

[derive(clap::Parser)]

enum Opt { Dist(xtaskwasm::Dist), Watch(xtaskwasm::Watch), Start(xtask_wasm::DevServer), }

fn main() -> Result<()> { let opt: Opt = clap::Parser::parse();

match opt {
    Opt::Dist(dist) => {
        log::info!("Generating package...");

        dist
            .dist_dir_path("dist")
            .static_dir_path("my-project/static")
            .app_name("my-project")
            .run_in_workspace(true)
            .run("my-project")?;
    }
    Opt::Watch(watch) => {
        log::info!("Watching for changes and check...");

        let mut command = Command::new("cargo");
        command.arg("check");

        watch.run(command)?;
    }
    Opt::Start(mut dev_server) => {
        log::info!("Starting the development server...");

        dev_server.arg("dist").start(default_dist_dir(false))?;
    }
}

Ok(())

} ```

examples/demo

Provides a basic implementation of xtask-wasm to generate the web app package, an "hello world" app using Yew. This example demonstrates a simple directory layout and a customized dist process that use the wasm-opt feature.

The available subcommands are:

Additional flags can be found using cargo xtask <subcommand> --help.

This example also demonstrates the use of the run-example feature that allows you to use the following:

console cargo run --example run_example

This command will run the code in examples/run_example using the development server.

Features