Convert Figma logo to code with AI

drager logowasm-pack

📦✨ your favorite rust -> wasm workflow tool!

7,038
472
7,038
388

Top Related Projects

📦✨ your favorite rust -> wasm workflow tool!

Facilitating high-level interactions between Wasm modules and JavaScript

3,459

A standard library for the client-side Web

32,087

Rust / Wasm framework for creating reliable and efficient web applications

20,163

🚀 Fast, secure, lightweight containers based on WebAssembly

Emscripten: An LLVM-to-WebAssembly Compiler

Quick Overview

wasm-pack is a command-line tool designed to help developers build and publish Rust-generated WebAssembly packages to npm. It streamlines the process of creating, testing, and publishing WebAssembly modules, making it easier for Rust developers to integrate their code with JavaScript projects.

Pros

  • Simplifies the WebAssembly development workflow for Rust projects
  • Automates the process of building and packaging WebAssembly modules
  • Integrates well with npm, allowing easy distribution of Rust-based WebAssembly modules
  • Supports various target environments, including web browsers and Node.js

Cons

  • Limited to Rust-based WebAssembly projects
  • May require additional setup and configuration for complex projects
  • Learning curve for developers new to Rust or WebAssembly
  • Dependency on external tools and ecosystems (Rust, npm)

Getting Started

To get started with wasm-pack, follow these steps:

  1. Install Rust and wasm-pack:

    curl https://sh.rustup.rs -sSf | sh
    cargo install wasm-pack
    
  2. Create a new Rust library project:

    cargo new --lib my-wasm-project
    cd my-wasm-project
    
  3. Add the following to your Cargo.toml:

    [lib]
    crate-type = ["cdylib"]
    
    [dependencies]
    wasm-bindgen = "0.2.83"
    
  4. Build your WebAssembly package:

    wasm-pack build --target web
    
  5. The compiled WebAssembly module and JavaScript wrapper will be available in the pkg directory.

Competitor Comparisons

📦✨ your favorite rust -> wasm workflow tool!

Pros of wasm-pack

  • More active development and recent updates
  • Larger community and contributor base
  • Better documentation and examples

Cons of wasm-pack

  • Potentially more complex setup for beginners
  • May have more dependencies and larger footprint

Code Comparison

wasm-pack:

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

wasm-pack>:

#[no_mangle]
pub extern "C" fn add(a: i32, b: i32) -> i32 {
    a + b
}

Summary

wasm-pack is a more mature and widely-used tool for building WebAssembly modules from Rust code. It offers better documentation, a larger community, and more frequent updates. However, it may be slightly more complex for beginners and have a larger footprint.

wasm-pack>, on the other hand, appears to be a simpler alternative with potentially fewer dependencies. It might be easier for beginners to get started with, but it may lack some of the advanced features and community support of wasm-pack.

The code comparison shows that wasm-pack uses the wasm_bindgen crate for easier JavaScript interop, while wasm-pack> uses a more low-level approach with #[no_mangle] and extern "C" attributes.

Facilitating high-level interactions between Wasm modules and JavaScript

Pros of wasm-bindgen

  • More comprehensive and actively maintained project with broader community support
  • Offers direct integration with JavaScript, allowing easier interoperability
  • Provides automatic generation of TypeScript definitions for improved type safety

Cons of wasm-bindgen

  • Steeper learning curve due to more complex features and options
  • Requires additional setup and configuration compared to wasm-pack

Code Comparison

wasm-bindgen:

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

wasm-pack:

#[no_mangle]
pub extern "C" fn add(a: i32, b: i32) -> i32 {
    a + b
}

wasm-bindgen offers a more streamlined approach with its #[wasm_bindgen] attribute, while wasm-pack requires manual export annotations. wasm-bindgen also provides better integration with JavaScript and TypeScript, making it easier to use WebAssembly modules in web projects.

wasm-pack is primarily a build tool that simplifies the process of compiling Rust to WebAssembly and creating npm packages. It can be used in conjunction with wasm-bindgen but doesn't provide the same level of JavaScript interoperability on its own.

In summary, wasm-bindgen is more feature-rich and better suited for complex WebAssembly projects with tight JavaScript integration, while wasm-pack offers a simpler approach for basic WebAssembly compilation and packaging.

3,459

A standard library for the client-side Web

Pros of stdweb

  • More mature and established project with a longer history
  • Provides a higher-level abstraction for DOM manipulation and JavaScript interop
  • Offers a wider range of pre-built bindings for common web APIs

Cons of stdweb

  • Less actively maintained, with fewer recent updates
  • May have larger bundle sizes due to its comprehensive feature set
  • Potentially slower compilation times compared to wasm-pack

Code Comparison

stdweb example:

#[macro_use]
extern crate stdweb;
use stdweb::web::*;

fn main() {
    stdweb::initialize();
    let button = document().query_selector("#myButton").unwrap();
    button.add_event_listener(|_: ClickEvent| {
        js! { console.log("Button clicked!"); }
    });
    stdweb::event_loop();
}

wasm-pack example:

use wasm_bindgen::prelude::*;

#[wasm_bindgen(start)]
pub fn main() -> Result<(), JsValue> {
    let window = web_sys::window().expect("no global `window` exists");
    let document = window.document().expect("should have a document on window");
    let body = document.body().expect("document should have a body");
    let val = document.create_element("p")?;
    val.set_inner_html("Hello from Rust!");
    body.append_child(&val)?;
    Ok(())
}

The stdweb example demonstrates its higher-level abstractions for DOM manipulation and event handling, while the wasm-pack example shows a more low-level approach using web-sys.

32,087

Rust / Wasm framework for creating reliable and efficient web applications

Pros of Yew

  • Comprehensive framework for building client-side web apps in Rust
  • Rich ecosystem with pre-built components and libraries
  • Strong community support and active development

Cons of Yew

  • Steeper learning curve for developers new to Rust or web development
  • Larger bundle sizes compared to simpler WASM solutions
  • May be overkill for small, simple projects

Code Comparison

Yew example:

use yew::prelude::*;

#[function_component(App)]
fn app() -> Html {
    html! {
        <h1>{"Hello, World!"}</h1>
    }
}

wasm-pack example:

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn greet(name: &str) -> String {
    format!("Hello, {}!", name)
}

Additional Notes

Yew is a full-featured framework for building web applications with Rust and WebAssembly, while wasm-pack is a build tool for creating and working with Rust-generated WebAssembly. They serve different purposes but can be used together in a project.

Yew provides a more opinionated structure for building complex web applications, whereas wasm-pack offers flexibility in how you integrate Rust and WebAssembly into your existing web projects.

20,163

🚀 Fast, secure, lightweight containers based on WebAssembly

Pros of Wasmer

  • More comprehensive WebAssembly runtime with broader language support
  • Offers standalone runtime and embedding options for multiple languages
  • Active development with frequent updates and larger community

Cons of Wasmer

  • Larger project scope may be overkill for simple Rust-to-WebAssembly compilation
  • Steeper learning curve due to more extensive features and options
  • Potentially higher resource usage for basic WebAssembly tasks

Code Comparison

Wasmer (runtime usage):

use wasmer::{Store, Module, Instance};

let module = Module::from_file(&store, "example.wasm")?;
let instance = Instance::new(&module, &imports)?;
let result = instance.exports.get_function("sum")?;

wasm-pack (build command):

wasm-pack build --target web

Key Differences

  • Wasmer focuses on WebAssembly runtime and execution, while wasm-pack is primarily a build tool for Rust-to-WebAssembly
  • Wasmer provides a more flexible ecosystem for running WebAssembly across various environments
  • wasm-pack is more specialized for Rust developers targeting web platforms
  • Wasmer requires more setup but offers greater extensibility
  • wasm-pack provides a simpler workflow for Rust-to-WebAssembly compilation and packaging

Emscripten: An LLVM-to-WebAssembly Compiler

Pros of Emscripten

  • More mature and widely adopted toolchain for compiling C/C++ to WebAssembly
  • Supports a broader range of languages and libraries beyond Rust
  • Provides a more complete runtime environment, including emulation of POSIX APIs

Cons of Emscripten

  • Larger output size due to additional runtime components
  • Steeper learning curve and more complex setup process
  • Less optimized for Rust-specific use cases

Code Comparison

Emscripten (C++ to WebAssembly):

#include <emscripten.h>
#include <iostream>

EMSCRIPTEN_KEEPALIVE
extern "C" int add(int a, int b) {
    return a + b;
}

wasm-pack (Rust to WebAssembly):

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

Emscripten requires more boilerplate and explicit exports, while wasm-pack leverages Rust's simpler syntax and built-in WebAssembly support. Emscripten's approach is more flexible for C/C++ projects, whereas wasm-pack is tailored for Rust development, offering a more streamlined experience for Rust-to-WebAssembly compilation.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

📦✨ wasm-pack

Your favorite Rust → Wasm workflow tool!

Build Status crates.io

Docs | Contributing | Chat

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.

demo

🔮 Prerequisites

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 was started by ashleygwilliams and is maintained by drager.

NPM DownloadsLast 30 Days