Skip to content

Commit

Permalink
feat: expose unwasm/examples
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 28, 2023
1 parent 77f9f4f commit 04127bf
Show file tree
Hide file tree
Showing 21 changed files with 81 additions and 247 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
coverage
dist
examples
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,40 @@ WebAssembly modules that don't require any imports, can be imported simply like
**Using static import:**

```js
import { func } from "lib/module.wasm";
import { sum } from "unwasm/examples/sum.wasm";
```

**Using dynamic import:**

```js
const { func } = await import("lib/module.wasm").then((mod) => mod.default);
const { sum } = await import("unwasm/examples/sum.wasm").then(
(mod) => mod.default,
);
```

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.

**Using static import with imports object:**

```js
import { func, $init } from "lib/module.wasm";
import { rand, $init } from "unwasm/examples/rand.wasm";

await $init({ env: {} });
await $init({
env: {
seed: () => () => Math.random() * Date.now(),
},
});
```

**Using dynamic import with imports object:**

```js
const { func } = await import("lib/module.wasm").then((mod) =>
mod.$init({ env: {} }),
const { rand } = await import("unwasm/examples/rand.wasm").then((mod) =>
mod.$init({
env: {
seed: () => () => Math.random() * Date.now(),
},
}),
);
```

Expand Down
2 changes: 2 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.d.ts
*.js
20 changes: 20 additions & 0 deletions examples/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { fileURLToPath } from "node:url";
import { main as asc } from "assemblyscript/asc";

async function compile(name) {
// https://www.assemblyscript.org/compiler.html#programmatic-usage
const res = await asc([`${name}.asc.ts`, "-o", `${name}.wasm`], {});

if (res.error) {
console.log(`Compilation failed for ${name}:`, res.error);
console.log(res.stderr.toString());
} else {
console.log(`Compiled: ${name}.wasm`);
console.log(res.stdout.toString());
}
}

process.chdir(fileURLToPath(new URL(".", import.meta.url)));

await compile("sum");
await compile("rand");
5 changes: 5 additions & 0 deletions examples/rand.asc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @ts-nocheck https://www.assemblyscript.org

export function rand(min: f64, max: f64): f64 {
return Math.floor(Math.random() * (max - min + 1));
}
File renamed without changes.
5 changes: 5 additions & 0 deletions examples/sum.asc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @ts-nocheck https://www.assemblyscript.org

export function sum(a: f64, b: f64): f64 {
return a + b;
}
Binary file added examples/sum.wasm
Binary file not shown.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@
"sideEffects": false,
"type": "module",
"exports": {
"./examples/*": "./examples/*",
"./plugin": {
"types": "./dist/plugin/index.d.mts",
"import": "./dist/plugin/index.mjs"
}
},
"files": [
"dist",
"*.d.ts"
"*.d.ts",
"examples/*.wasm"
],
"scripts": {
"build": "unbuild",
"build": "unbuild && pnpm build:examples",
"build:examples": "node ./examples/build.mjs",
"dev": "vitest dev",
"lint": "eslint --cache --ext .ts,.js,.mjs,.cjs . && prettier -c src test",
"lint:fix": "eslint --cache --ext .ts,.js,.mjs,.cjs . --fix && prettier -c src test -w",
Expand All @@ -36,6 +39,7 @@
"@rollup/plugin-node-resolve": "^15.2.3",
"@types/node": "^20.10.5",
"@vitest/coverage-v8": "^1.1.0",
"assemblyscript": "^0.27.22",
"changelogen": "^0.5.5",
"eslint": "^8.56.0",
"eslint-config-unjs": "^0.2.1",
Expand Down
21 changes: 21 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/fixture/dynamic-import.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { imports } from "./_imports.mjs";

const { rand } = await import("@fixture/wasm/index.wasm").then((r) =>
const { rand } = await import("@fixture/wasm/examples/rand.wasm").then((r) =>
r.$init(imports),
);

Expand Down
2 changes: 1 addition & 1 deletion test/fixture/static-import.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { imports } from "./_imports.mjs";

// eslint-disable-next-line import/named
import { rand, $init } from "@fixture/wasm/index.wasm";
import { rand, $init } from "@fixture/wasm/examples/rand.wasm";

await $init(imports);

Expand Down
1 change: 0 additions & 1 deletion test/node_modules/@fixture/wasm/.gitignore

This file was deleted.

15 changes: 0 additions & 15 deletions test/node_modules/@fixture/wasm/dist/index.d.ts

This file was deleted.

15 changes: 0 additions & 15 deletions test/node_modules/@fixture/wasm/dist/index.js

This file was deleted.

0 comments on commit 04127bf

Please sign in to comment.