npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@emily-curry/snowpack-plugin-wasm-pack

v1.1.4

Published

Snowpack plugin for rust using wasm-pack 🦀

Downloads

40

Readme

snowpack-plugin-wasm-pack

Snowpack plugin for rust using wasm-pack 🦀

A forked version of snowpack-plugin-wasm-pack. See Changes below.

Installation

yarn add --dev @emily-curry/snowpack-plugin-wasm-pack

The plugin also depends on wasm-pack and cargo-watch

These can be installed with:

cargo install wasm-pack

(or following these instructions)

cargo install cargo-watch

Usage

Create a new RustWasm project within your project:

wasm-pack new <my-wasm-project-name>

Add the plugin to your Snowpack config:

snowpack.config.js

module.exports = {
  mount: {
    src: '/',
  },
  plugins: [
    [
      '@emily-curry/snowpack-plugin-wasm-pack',
      {
        projectPath: './my-wasm-project-name',
      },
    ],
  ],
};

Options

| Name | Description | Type | Required | Default | | :------------- | :------------------------------------------------------------------- | :-------------------------------------: | :------: | :---------------: | | projectPath | Relative path from the root to your wasm-pack project. | string | yes | - | | outDir | Directory for the compiled assets. | string | | "pkg" | | outName | Sets the prefix for output file names. | string | | "index" | | logLevel | Sets the log level of wasm-pack. | "info" or "warn" or "error" | | "warn" | | target | Sets the target of wasm-pack. | string | | "web" | | scope | Scope of your package name, eg: @test/my-great-wasm. | string | | - | | extraArgs | Any extra args you want to pass to wasm-pack. eg: --no-typescript. | Array<string> | | - | | wasmPackPath | Path to a custom install of wasm-pack. | string | | - | | watch | The snowpack modes that will run wasm-pack in watch mode. | boolean or SnowpackConfig['mode'][] | | ['development'] |

In your code:

wasm-pack exports an init funtion as the default export. This must be called (once) before any other functions can be used. snowpack-plugin-wasm-pack will automatically configure path aliases to your project under the name of your package:

project/lib.rs

// --- code omitted

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

src/index.ts

import init, { add } from 'my-wasm-project-name';

const maths = async () => {
  await init();

  console.log('Addition in rust:', add(1, 2));
};

maths(); // should log 3 to console

src/index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
  </head>
  <body>
    <h1>🦀</h1>
    <script type="module" src="index.js"></script>
  </body>
</html>

Usage with typescript

You need to manually add the alias to your tsconfig under compilerOptions -> paths.

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "my-wasm-project-name": ["./my-wasm-project-name/pkg"]
    }
  }
}

Multiple RustWasm projects

Add the plugin multiple times for multiple projects:

snowpack.config.js

module.exports = {
  // ... rest omitted
  plugins: [
    [
      '@emily-curry/snowpack-plugin-wasm-pack',
      {
        projectPath: './my-wasm-project-name',
      },
    ],
    [
      '@emily-curry/snowpack-plugin-wasm-pack',
      {
        projectPath: './path/to/another/project',
      },
    ],
  ],
};

Changes

1.0.0

  • Added watch option. In modes matching the watch option, the wasm-pack build process runs alongside the snowpack build process. In non-watch modes, the wasm-pack build process executes syncronously before the main snowpack build process, ensuring the files are built in time for subsequent steps.

1.1.0

  • Added target option, allowing the wasm-pack --target arg to be set.

1.1.2

  • README improvements.

1.1.3

  • snowpack build now outputs same compilation logs as snowpack dev.

1.1.4

  • snowpack build now outputs args used to invoke wasm-pack.

Useful links

wasm-pack wasm-bindgen snowpack