π¦ Greeter demo using wasm-pack to create an npm package
|
π¦ Written in Rust!
πͺ Compiled to WebAssembly
π¨ Exposed to JavaScript
π Great way to see what wasm-pack can do
π©ββοΈ 0BSD licensed template
You can install this package using your favorite npm package manager like npm, Yarn, pnpm, or Bun.
npm install hello-world-wasm-pack-lib
If you're using Deno you can import it straight from npm:
import {} from "npm:hello-world-wasm-pack-lib";
If you prefer to go buildless in the browser you can use an npm CDN like jsDelivr or esm.sh.
<script type="module">
import {} from "https://esm.run/hello-world-wasm-pack-lib";
</script>
You should be able to just import and use this JavaScript package like any other. The WASM magic is hidden behind a really nice wrapper layer.
import { greet, Calculator, get_report } from "hello-world-wasm-pack-lib";
greet("Alan Turing");
//=> 'Hello Alan Turing!'
const report = get_report();
console.log(report);
//=> Report {
// dog_count: 5,
// bouncy_castles: 3,
// fun_percent: 76.8
// }
π You can find complete API documentation on the docs website
To build the JavaScript package just run ./just build. Then you can cd into
the pkg/ folder if you want and launch a REPL to try it out!
./just build
cd pkg
node
If you need to edit the pkg.patch file, you can do so via the below command.
It will create two folders: a/ and b/. Edit anything in the b/ folder and
a .patch will be generated against the a/ folder and saved to pkg.patch.
./just generate-patch
# Pauses while you make changes...