Skip to content

Commit 457dd85

Browse files
docs(transpile): use writeFiles in README, add examples
1 parent 6878a09 commit 457dd85

File tree

1 file changed

+121
-43
lines changed

1 file changed

+121
-43
lines changed

packages/jco-transpile/README.md

Lines changed: 121 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,22 @@ and `jco` derives it's use of transpilation from this library.
1616

1717
To use `@bytecodealliance/jco-transpile` as a library in your own code, you can use some of the exported functions.
1818

19+
Check out our [examples on GitHub][gh-examples].
20+
21+
[gh-examples]: https://github.com/bytecodealliance/jco/tree/main/examples/transpile
22+
1923
## Transpiling
2024

2125
To transpile an existing WebAssembly component (path on disk, `Buffer`):
2226

2327
```js
24-
import { dirname } from "node:path";
25-
import { mkdir, writeFile } from "node:fs/promises";
26-
27-
import { transpile } from '@bytecodealliance/jco-transpile';
28+
import { transpile, writeFiles } from '@bytecodealliance/jco-transpile';
2829

2930
async function example() {
31+
// Transpile a given WebAssembly component into JS runnabe in NodeJS
3032
const { files, imports, exports } = await transpile('path/to/component.wasm', { outDir: 'path/to/output/dir' });
31-
32-
// NOTE: Files is a serialization of the files produced of the following type:
33-
// type FileBytes = { [filepath: string]: Uint8Array; };
34-
//
35-
// You can use the code below to write out all files to a given directory
36-
await Promise.all(
37-
Object.entries(files).map(async ([filePath, bytes]) => {
38-
await mkdir(dirname(filePath), { recursive: true });
39-
await writeFile(filePath, bytes);
40-
})
41-
);
33+
// Write out the files that have been generated to disk
34+
await writeFiles(files);
4235
}
4336
```
4437

@@ -48,46 +41,59 @@ async function example() {
4841
> `outDir` controls the prefix of generated file paths, so it is specified, despite the fact that no files
4942
> are actually written to disk.
5043
51-
## Generating host types
52-
53-
To generate host types:
44+
### Transpilation example
5445

55-
```js
56-
import { dirname } from "node:path";
57-
import { mkdir, writeFile } from "node:fs/promises";
46+
If you write a component with the given WIT:
5847

59-
import { generateHostTypes } from '@bytecodealliance/jco-transpile';
48+
```wit
49+
package docs:adder@0.1.0;
6050
61-
async function example() {
62-
const files = await generateHostTypes('path/to/wit/dir-or-file, { outDir: 'path/to/output/dir' });
51+
interface add {
52+
add: func(x: u32, y: u32) -> u32;
53+
}
6354
64-
// NOTE: Files is a serialization of the files produced of the following type:
65-
// type FileBytes = { [filepath: string]: Uint8Array; };
66-
//
67-
// You can use the code below to write out all files to a given directory
68-
await Promise.all(
69-
Object.entries(files).map(async ([filePath, bytes]) => {
70-
await mkdir(dirname(filePath), { recursive: true });
71-
await writeFile(filePath, bytes);
72-
})
73-
);
55+
world adder {
56+
export add;
7457
}
7558
```
7659

77-
Host types are the implementations of WIT interfaces that are used by *post-transpilation* JS code,
78-
i.e. on the "host" (NodeJS + V8 or the browser), to make platform *imports* work.
60+
After tranpsilation of that component, you should see output like the following:
61+
62+
```console
63+
dist
64+
└── transpiled
65+
├── add.core.wasm
66+
├── add.d.ts
67+
├── adder.core.wasm
68+
├── adder.d.ts
69+
├── adder.js
70+
├── add.js
71+
├── add.mjs
72+
└── interfaces
73+
└── docs-adder-add.d.ts
74+
75+
3 directories, 8 files
76+
```
77+
78+
`dist/transpiled/adder.js` is the entrypoint that can be used from "host" code:
79+
80+
```js
81+
import { add } from "./dist/transpiled/adder.js";
7982

80-
For a given import in a WIT world, this type generation would enable *implementation* of the interface.
83+
console.log("1 + 2 = " + add.add(1, 2));
84+
```
8185

82-
## Generating guest types
86+
You can try this example for yourself [in GitHub][gh-examples-transpile-adder].
8387

84-
To generate guest types:
88+
[gh-examples-transpile-adder]: https://github.com/bytecodealliance/jco/tree/main/examples/transpile/adder
8589

86-
```js
87-
import { dirname } from "node:path";
88-
import { mkdir, writeFile } from "node:fs/promises";
90+
## Generating guest (component) types
8991

90-
import { generateGuestTypes } from '@bytecodealliance/jco-transpile';
92+
When writing components, you can generate the Typescript declarations that correspond to your
93+
[WebAssembly Interface Types (WIT)][wit] imports and exports by generating guest tyeps:
94+
95+
```js
96+
import { generateGuestTypes, writeFiles } from '@bytecodealliance/jco-transpile';
9197

9298
async function example() {
9399
const files = await generateGuestTypes('path/to/wit/dir-or-file, { outDir: 'path/to/output/dir' });
@@ -110,6 +116,78 @@ turned into a component (by `jco componentize`/`componentize-js`) and performing
110116
111117
For a given import in a WIT world, this type generation would enable *calling*/*using* the interface.
112118
119+
### Guest type generation example
120+
121+
For example, given the following WIT interface:
122+
123+
```wit
124+
package docs:adder@0.1.0;
125+
126+
interface add {
127+
add: func(x: u32, y: u32) -> u32;
128+
}
129+
130+
world adder {
131+
export add;
132+
}
133+
```
134+
135+
The Typescript declaration produced is:
136+
137+
```
138+
declare module 'docs:adder/add@0.1.0' {
139+
export function add(x: number, y: number): number;
140+
}
141+
```
142+
143+
[wit]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md
144+
145+
## Generating host types
146+
147+
When building platforms that run transpiled components, you can generate Typescript
148+
declarations that correspond to the [WebAssembly Interface Types (WIT)][wit] imports (that the component requires)
149+
by generating host types:
150+
151+
```js
152+
import { generateHostTypes, writeFiles } from '@bytecodealliance/jco-transpile';
153+
154+
async function example() {
155+
// Generate types for a host binding (i.e. supplying imports to a transpiled component)
156+
const files = await generateHostTypes('path/to/wit/dir-or-file, { outDir: 'path/to/output/dir' });
157+
// Write out the files that have been generated to disk
158+
await writeFiles(files);
159+
}
160+
```
161+
162+
Host types are the implementations of WIT interfaces that are used by *post-transpilation* JS code,
163+
i.e. on the "host" (NodeJS + V8 or the browser), to make platform *imports* work.
164+
165+
For a given import in a WIT world, this type generation would enable *implementation* of the imported
166+
functionality interface.
167+
168+
### Host type generation example
169+
170+
For example, given the following WIT interface:
171+
172+
```wit
173+
package docs:adder@0.1.0;
174+
175+
interface add {
176+
add: func(x: u32, y: u32) -> u32;
177+
}
178+
179+
world adder {
180+
export add;
181+
}
182+
```
183+
184+
The Typescript declaration produced is:
185+
186+
```
187+
/** @module Interface docs:adder/add@0.1.0 **/
188+
export function add(x: number, y: number): number;
189+
```
190+
113191
# License
114192

115193
This project is licensed under the Apache 2.0 license with the LLVM exception.

0 commit comments

Comments
 (0)