Skip to content

Commit 04127bf

Browse files
committed
feat: expose unwasm/examples
1 parent 77f9f4f commit 04127bf

21 files changed

+81
-247
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
coverage
33
dist
4+
examples

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,40 @@ WebAssembly modules that don't require any imports, can be imported simply like
3434
**Using static import:**
3535

3636
```js
37-
import { func } from "lib/module.wasm";
37+
import { sum } from "unwasm/examples/sum.wasm";
3838
```
3939

4040
**Using dynamic import:**
4141

4242
```js
43-
const { func } = await import("lib/module.wasm").then((mod) => mod.default);
43+
const { sum } = await import("unwasm/examples/sum.wasm").then(
44+
(mod) => mod.default,
45+
);
4446
```
4547

4648
In case your WebAssembly module requires an import object (which is likely!), the usage syntax would be slightly different as we need to initate the module with an import object first.
4749

4850
**Using static import with imports object:**
4951

5052
```js
51-
import { func, $init } from "lib/module.wasm";
53+
import { rand, $init } from "unwasm/examples/rand.wasm";
5254

53-
await $init({ env: {} });
55+
await $init({
56+
env: {
57+
seed: () => () => Math.random() * Date.now(),
58+
},
59+
});
5460
```
5561

5662
**Using dynamic import with imports object:**
5763

5864
```js
59-
const { func } = await import("lib/module.wasm").then((mod) =>
60-
mod.$init({ env: {} }),
65+
const { rand } = await import("unwasm/examples/rand.wasm").then((mod) =>
66+
mod.$init({
67+
env: {
68+
seed: () => () => Math.random() * Date.now(),
69+
},
70+
}),
6171
);
6272
```
6373

examples/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.d.ts
2+
*.js

examples/build.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { fileURLToPath } from "node:url";
2+
import { main as asc } from "assemblyscript/asc";
3+
4+
async function compile(name) {
5+
// https://www.assemblyscript.org/compiler.html#programmatic-usage
6+
const res = await asc([`${name}.asc.ts`, "-o", `${name}.wasm`], {});
7+
8+
if (res.error) {
9+
console.log(`Compilation failed for ${name}:`, res.error);
10+
console.log(res.stderr.toString());
11+
} else {
12+
console.log(`Compiled: ${name}.wasm`);
13+
console.log(res.stdout.toString());
14+
}
15+
}
16+
17+
process.chdir(fileURLToPath(new URL(".", import.meta.url)));
18+
19+
await compile("sum");
20+
await compile("rand");

examples/rand.asc.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @ts-nocheck https://www.assemblyscript.org
2+
3+
export function rand(min: f64, max: f64): f64 {
4+
return Math.floor(Math.random() * (max - min + 1));
5+
}
File renamed without changes.

examples/sum.asc.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @ts-nocheck https://www.assemblyscript.org
2+
3+
export function sum(a: f64, b: f64): f64 {
4+
return a + b;
5+
}

examples/sum.wasm

93 Bytes
Binary file not shown.

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@
77
"sideEffects": false,
88
"type": "module",
99
"exports": {
10+
"./examples/*": "./examples/*",
1011
"./plugin": {
1112
"types": "./dist/plugin/index.d.mts",
1213
"import": "./dist/plugin/index.mjs"
1314
}
1415
},
1516
"files": [
1617
"dist",
17-
"*.d.ts"
18+
"*.d.ts",
19+
"examples/*.wasm"
1820
],
1921
"scripts": {
20-
"build": "unbuild",
22+
"build": "unbuild && pnpm build:examples",
23+
"build:examples": "node ./examples/build.mjs",
2124
"dev": "vitest dev",
2225
"lint": "eslint --cache --ext .ts,.js,.mjs,.cjs . && prettier -c src test",
2326
"lint:fix": "eslint --cache --ext .ts,.js,.mjs,.cjs . --fix && prettier -c src test -w",
@@ -36,6 +39,7 @@
3639
"@rollup/plugin-node-resolve": "^15.2.3",
3740
"@types/node": "^20.10.5",
3841
"@vitest/coverage-v8": "^1.1.0",
42+
"assemblyscript": "^0.27.22",
3943
"changelogen": "^0.5.5",
4044
"eslint": "^8.56.0",
4145
"eslint-config-unjs": "^0.2.1",

pnpm-lock.yaml

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)