Rust-Wasm pack: ?✨ your favorite rust -> wasm workflow tool!

? wasm-pack

Your favorite Rust → Wasm workflow tool!

Build Status Build status crates.io

Docs | Contributing | Chat

Built with ? ? by The Rust and WebAssembly Working Group

About

This tool seeks to be a one-stop shop for building and working with rust- generated WebAssembly that you would like to interop with JavaScript, in the browser or with Node.js. wasm-pack helps you build rust-generated WebAssembly packages that you could publish to the npm registry, or otherwise use alongside any javascript packages in workflows that you already use, such as webpack or greenkeeper.

This project is a part of the rust-wasm group. You can find more info by visiting that repo!

demo

? Prerequisities

This project requires Rust 1.30.0 or later.

Quickstart Guide

Visit the quickstart guide in our documentation.

?️ Commands

  • new: Generate a new RustWasm project using a template
  • build: Generate an npm wasm pkg from a rustwasm crate
  • test: Run browser tests
  • pack and publish: Create a tarball of your rustwasm pkg and/or publish to a registry

? Logging

wasm-pack uses env_logger to produce logs when wasm-pack runs.

To configure your log level, use the RUST_LOG environment variable. For example:

RUST_LOG=info wasm-pack build

? Contributing

Read our guide on getting up and running for developing wasm-pack, and check out our contribution policy.

?‍♀️ Governance

This project is part of the rustwasm Working Group.

This project was started by ashleygwilliams and is co-maintained by ashleygwilliams, drager and the Rust Wasm Working Group Core Team.

Comments

  • Passes `extra_options` to `cargo build --tests` when running `wasm-pack test`.
    Passes `extra_options` to `cargo build --tests` when running `wasm-pack test`.

    May 17, 2020

    Fixes https://github.com/rustwasm/wasm-pack/issues/698.

    Before this change, when running wasm-pack test -- --features "feature" -- test_filter, wasm-pack runs

    cargo build --tests --target wasm32-unknown-unknown
    cargo test --target wasm32-unknown-unknown --features "feature" -- test_filter
    

    Now it does:

    cargo build --tests --target wasm32-unknown-unknown --features "feature"
    cargo test --target wasm32-unknown-unknown --features "feature" -- test_filter
    

    The command to build tests is only used once throughout the whole code base, that's why I passed the extra_options through without wrapping in Option<_>.

    Comes with a compile_error! test too :v:.


    Make sure these boxes are checked! ?✅

    • [x] You have the latest version of rustfmt installed
    $ rustup component add rustfmt
    
    • [x] You ran cargo fmt on the code base before submitting
    • [x] You reference which issue is being closed in the PR text

    ✨✨ ? Thanks so much for contributing to wasm-pack! ? ✨✨

    :v: welcome :bowing_man:!

    Reply
  • Add and option to pass -k (insecure) option for curl
    Add and option to pass -k (insecure) option for curl

    Jun 1, 2020

    Corporate proxies love to kill tls connections with breaking handshakes etc etc. In this case -k option curl helps a lot. For example, cargo has similar option "check-revoke". This is more of binary-install crate issue, but is it possible to provide such option for wasm-pack so it will invoke curl with -k option when installing tools?

    Reply
  • Some characters not rendering on Windows Powershell
    Some characters not rendering on Windows Powershell

    Jun 5, 2020

    image

    Minor thing that I think you guys should be aware of. :)

    Running Windows 10 Pro v 1903 if that's relevant, with the default powershell install.

    Reply
  • Some option to disable emojis
    Some option to disable emojis

    Jun 5, 2020

    Hi.

    Emojis are cool, but they become noising when they are overdone. It would be nice some option (--disable-emojis?) to disable them (even raw emojis like :-)) from the logs.

    With emojis (noising):

    :-) :-) Finished release [optimized] target(s) in 0.04s
    :-) [WARN]: origin crate has no README
    [INFO]: Optimizing wasm binaries with `wasm-opt`...
    [INFO]: Optional fields missing from Cargo.toml: 'description', 'repository', and 'license'. These are not necessary, but recommended
    [INFO]: :-) Done in 1.55s
    [INFO]: :-) Your wasm pkg is ready to publish at ~/yew-app/./static.
    

    Without emojis (log-friendly):

    Finished release [optimized] target(s) in 0.04s
    [WARN]: origin crate has no README
    [INFO]: Optimizing wasm binaries with `wasm-opt`...
    [INFO]: Optional fields missing from Cargo.toml: 'description', 'repository', and 'license'. These are not necessary, but recommended
    [INFO]: Done in 1.55s
    [INFO]: Your wasm pkg is ready to publish at ~/yew-app/./static.
    

    cheers

    Reply
  • Add no-modules-global support
    Add no-modules-global support

    Jun 14, 2020

    This small change adds no-modules-global support, which is a CLI arg that wasm-bindgen already supports. This simply changes the name of the generated global. Closes #729

    • [x] You have the latest version of rustfmt installed
    $ rustup component add rustfmt
    
    • [x] You ran cargo fmt on the code base before submitting
    • [x] You reference which issue is being closed in the PR text
    Reply
  • Support `net2` in wasm
    Support `net2` in wasm

    Jun 14, 2020

    ? Feature description

    Although some crates depend on net2, wasm32 doesn't support it. So cannot use the crates. I want wasm32 to have net2. Will you include net2 in further version?

    Thank you!

    Reply
  • reading the cargo toml 2x is kinda gross
    reading the cargo toml 2x is kinda gross

    Mar 3, 2018

    This issue is reserved for someone who has not yet contributed to this codebase.

    Currently we read the Cargo.toml twice! this is not preferred:

    1. read it to write package.json
    2. read it to pass crate name to wasm-bindgen

    MENTORING INSTRUCTIONS (from @mgattozzi !):

    1. Create a small expression or function to read the Cargo.toml file to a String and assign it to a variable, maybe something like CARGO_TOML
    2. Replace instances of it being read with the variable you just created
    3. Remove any code associated with reading the file
    changelog - maintenance 
    Reply
  • Coordinating with wasm-bingden about versions and list of NPM dependencies
    Coordinating with wasm-bingden about versions and list of NPM dependencies

    Apr 20, 2018

    This is related to https://github.com/rust-lang-nursery/rust-wasm/issues/34 and https://github.com/rust-lang-nursery/rust-wasm/issues/36, but I'd like to propose a strawman idea here.

    Right now wasm-bindgen has the ability to say where you're importing functionality from:

    #[wasm_bindgen(module = "foo")]
    extern {
        // ...
    }
    

    If module = "..." is omitted it's assumed to come from the ambient environment (aka the browser). Otherwise foo is interpreted as an ES module directive and that's where items are imported from in the generated JS that wasm-bindgen emits.

    The wasm-bindgen output is pretty "dumb" right now in that it's just bland ES module information, it doesn't currently attempt to give extra information like verison requirements for NPM packages. But that's where wasm-pack comes in! Ideally wasm-bindgen would emit information that wasm-pack later reads, parses, and emits a package.json for.

    So I'd propose something like:

    • Add support for version = "..." next to module = "..." in wasm-bindgen
    • For all module = "..." directives that don't start with ., require that version is listed
    • Emit a new section in the wasm executable in the wasm that wasm-bindgen emits in the format:
      • The section name is __wasm_pack_unstable
      • The section format is a list of elements. Each element is prefixed with 4 bytes of how long it is and then the rest of the data is a JSON payload.
      • The JSON payload for each element is currently { "module": "string", "version": "string" }
    • When wasm-pack executes it slurps up the __wasm_pack_unstable section, if present, and emits these packages into the package.json that's generated.

    An example of using this would look like:

    #[wasm_bindgen(module = "moment", version = "2.0.0")]
    extern {
        type Moment;
        fn moment() -> Moment;
        #[wasm_bindgen(method)]
        fn format(this: &Moment) -> String;
    }
    
    #[wasm_bindgen]
    pub fn run() -> String {
        moment().format()
    }
    

    And wasm-pack would automatically generate a package.json with:

    { 
       "dependencies": {
            "moment": "2.0.0"
        }
    }
    

    I'm definitely up for changing anything here, this is just an initial strawman! I'd love to hear others' thoughts on this as well.

    PR attached question to-do 
    Reply
  • Strange behavior on first run of `wasm-pack init`
    Strange behavior on first run of `wasm-pack init`

    Sep 13, 2018

    ? Bug description

    So in a new Rust project, I have my code written and sitting pretty and everything works. I am then ready to compile to wasm. I run wasm-pack init and get an error (described below). If I run the same command again, everything works as expected. ? So weird.

    Error:

    $ wasm-pack init --scope tejaskumar
    
      [1/8] ?  Checking crate configuration...
      [2/8] ?  Adding WASM target...
      [3/8] ?  Compiling to WASM...
      [4/8] ?  Creating a pkg directory...
      [5/8] ?  Writing a package.json...
      ⚠️   [WARN]: Field description is missing from Cargo.toml. It is not necessary, but recommended
      ⚠️   [WARN]: Field repository is missing from Cargo.toml. It is not necessary, but recommended
      ⚠️   [WARN]: Field license is missing from Cargo.toml. It is not necessary, but recommended
      [6/8] ?  Copying over your README...
      ⚠️   [WARN]: origin crate has no README
      [7/8] ⬇️  Installing WASM-bindgen...
    | [8/8] ?‍♀️  Running WASM-bindgen...
    wasm-bindgen failed to execute properly. stderr:
    
    error: failed to read `target/wasm32-unknown-unknown/release/my_wasmp.wasm`
    	caused by: No such file or directory (os error 2)
    

    ? Expected Behavior

    What should have happened? It should have created a pkg folder with my npm package ready to go.

    ? Steps to reproduce

    Clear steps describing how to reproduce the issue, including commands and flags run. If you are seeing an error, please include the full error message and stack trace.

    Unfortunately, this is completely random and hard to reproduce. :(

    ? Your environment

    Include the relevant details of your environment. wasm-pack version: 0.4.2 rustc version: rustc 1.28.0 (9634041f0 2018-07-30)

    PR attached bug user report 
    Reply
  • fix canonical paths windows
    fix canonical paths windows

    Oct 4, 2018

    This PR addresses problems with canonical paths on windows that were reported in #380. I tested this by building a wasm app with a few dependencies on windows.

    I would love somebody to check the work and try running it on another windows box targeting another wasm app ? .

    Implementation: Remove the usage of std::fs::canonicalize.

    Unit test: I added a unit test based on reported failures:

    • a project with local transitive dependencies, see #413
    changelog - maintenance 
    Reply
  • bugfix(command/build.rs): Cancel Escape
    bugfix(command/build.rs): Cancel Escape

    Oct 5, 2018

    fix #390

    Make sure these boxes are checked! ?✅

    • [x] You have the latest version of rustfmt installed and have your cloned directory set to nightly
    $ rustup override set nightly
    $ rustup component add rustfmt-preview --toolchain nightly
    
    • [x] You ran rustfmt on the code base before submitting
    • [x] You reference which issue is being closed in the PR text

    ✨✨ ? Thanks so much for contributing to wasm-pack! ? ✨✨

    changelog - fix 
    Reply
  • should we generate typscript type files by default?
    should we generate typscript type files by default?

    Apr 26, 2018

    a question came up in solving issue #107 on PR #109: should we generate typescript type files by default?

    i have requested that the PR land behind a flag but i'm curious to hear the arguments for why it should be default! this is the issue for that convo :)

    question 
    Reply