[memo] Rust wasm

Jan 22, 2023 18:22 · 70 words · 1 minute read

Install packages:

  • build-essential
  • libssl-dev
  • pkg-config

Initialize cargo project for wasm.

> cargo generate --git https://github.com/rustwasm/wasm-pack-template

Install wasm-pack.

> cargo install wasm-pack

Build for wasm.

> wasm-pack build --target web

Then js, d.ts and .wasm files are generated in pkg.
Copy pkg/* into Typescript project (src/lib/wasm, for this case).

import init, {greet} from "./lib/wasm";

(async () => {
  await init();
  greet();
})()

The point is to call init() before using greet.