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

lp-demo-rust-wasm

v0.1.0

Published

``` cargo new --lib xxx ```

Downloads

4

Readme

cargo new --lib xxx

Cargo.toml

[dependencies]
wasm-bindgen = "0.2.81"

[lib]
crate-type = ["cdylib"]

lib.rs

use wasm_bindgen::prelude::*;
// 需要包裹上这块宏
#[wasm_bindgen]
pub fn add(left: usize, right: usize) -> usize {
    left + right
}

构建

// 会生成到 pkg 目录
wasm-pack build --target web
.
├─ README.md
├─ xxx.d.ts
├─ xxx.js
├─ xxx_bg.wasm
└─ xxx_bg.wasm.d.ts

xxx.js 本质上有无都可以,你可以直接使用 xxx_bg.wasm 来消费。

但好处在于 xxx.js 是由 wasm-pack 生成的,内部有加载函数和 lib 结构,把一些能力都给做好了,也没过多的无用代码,用起来是非常方便的。

使用

// xxx.js default 是一个初始化函数,反回一个 wasm 的原始 exports,和 wasm
直接使用。
<script type="module">
  import initWasm from "./pkg/xxx.js";
  initWasm().then(({ add }) => {});
</script>

// 或者 // 但这种 import 的 add 是由 xxx.js
自己根据一定推测包裹起来的,所以本质来说他并不是真实 wasm
的函数,而是又做了一层pollyfill,有利有弊吧
<script type="module">
  import initWasm, { add } from "./pkg/xxx.js";
  initWasm().then(() => {
    add(1, 2);
  });
</script>

// 或者自行 Res.arraybuffer,然后同步加载
<script type="module">
  import { initSync } from "./pkg/xxx.js";

  // async
  const byte = Res.arrayBuffer();

  const { add } = initSync(byte);
</script>

npm 发布/消费

消费侧可以无痛使用。(.wasm 的存放问题、存放在本地的读取问题(asset cdn环境))