Skip to content

Commit 9750031

Browse files
committed
Update README: reorder sections, document webapitrim CLI install and wasm-gc usage
1 parent 1c7750b commit 9750031

1 file changed

Lines changed: 43 additions & 18 deletions

File tree

src/README.mbt.md

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ Type-safe MoonBit bindings for Web Platform APIs, automatically generated from W
44

55
## Table of Contents
66

7-
- [Overview](#overview)
87
- [Installation](#installation)
8+
- [Overview](#overview)
99
- [Quick Start](#quick-start)
1010
- [Counter Example](#counter-example)
1111
- [WebSocket Example](#websocket-example)
@@ -21,7 +21,7 @@ Type-safe MoonBit bindings for Web Platform APIs, automatically generated from W
2121
- [Custom Elements](#custom-elements)
2222
- [Method Chaining](#method-chaining)
2323
- [Optional Parameters](#optional-parameters)
24-
- [Trimming webapi.mjs for Production](#trimming-webapimjs-for-production)
24+
- [Trimming webapi.mjs for wasm-gc Production](#trimming-webapimjs-for-wasm-gc-production)
2525
- [WebIDL to MoonBit Conversion](#webidl-to-moonbit-conversion)
2626
- [Type Mappings](#type-mappings)
2727
- [Interface Generation](#interface-generation)
@@ -32,6 +32,20 @@ Type-safe MoonBit bindings for Web Platform APIs, automatically generated from W
3232
- [Building from Source](#building-from-source)
3333
- [License](#license)
3434

35+
## Installation
36+
37+
Add this package to your MoonBit project:
38+
39+
```bash
40+
moon add bikallem/webapi@0.4.2
41+
```
42+
43+
To also install the `webapitrim` CLI tool (for trimming `webapi.mjs` in wasm-gc deployments):
44+
45+
```bash
46+
moon add --bin bikallem/webapi
47+
```
48+
3549
## Overview
3650

3751
This library provides MoonBit FFI bindings for browser APIs including:
@@ -58,14 +72,6 @@ This library provides MoonBit FFI bindings for browser APIs including:
5872

5973
All bindings are automatically generated from official WebIDL specifications, ensuring type safety and API completeness.
6074

61-
## Installation
62-
63-
Add this package to your MoonBit project:
64-
65-
```bash
66-
moon add bikallem/webapi@0.4.2
67-
```
68-
6975
## Quick Start
7076

7177
### Counter Example
@@ -376,21 +382,40 @@ fn readme_optional() -> Unit {
376382
}
377383
```
378384

379-
## Trimming webapi.mjs for Production
385+
## Trimming webapi.mjs for wasm-gc Production
386+
387+
The full `src/webapi.mjs` JS runtime (~8,400 lines) contains modules for every supported Web API. For **wasm-gc** deployments, the `webapitrim` tool produces a minimal version containing only the modules your `.wasm` binary actually imports — typically 500–1,000 lines (~90% smaller).
380388

381-
The full `webapi/webapi.mjs` JS runtime (~8,400 lines) contains modules for every supported Web API. For wasm-gc deployments, `webapi_trim` produces a minimal version containing only the modules your `.wasm` binary actually imports — typically 500–1,000 lines (~90% smaller).
389+
> **Note:** This tool is only relevant for the wasm-gc backend. The JS backend does not use `webapi.mjs`.
390+
391+
### Installing webapitrim
392+
393+
Install the `webapitrim` CLI via the MoonBit package manager:
394+
395+
```bash
396+
moon add --bin bikallem/webapi
397+
```
398+
399+
This installs the `webapitrim` binary to `~/.moon/bin/` (or your configured MoonBit bin directory).
382400

383401
### Usage
384402

385403
```bash
386404
# Trim for a single wasm binary (output: webapi.mjs next to the .wasm file)
387-
make trim WASM=examples/_build/wasm-gc/release/build/counter/counter.wasm
405+
webapitrim path/to/app.wasm --source path/to/webapi.mjs
388406

389407
# Trim with explicit output path
390-
make trim WASM=path/to/app.wasm OUT=path/to/output.mjs
408+
webapitrim path/to/app.wasm --source path/to/webapi.mjs -o path/to/output.mjs
409+
```
391410

392-
# Trim all built examples at once
411+
When building from source, you can also use the Makefile targets:
412+
413+
```bash
414+
# Trim all built wasm-gc examples at once
393415
make trim-examples
416+
417+
# Trim a single wasm binary
418+
make trim WASM=path/to/app.wasm OUT=path/to/output.mjs
394419
```
395420

396421
### HTML Setup
@@ -399,10 +424,10 @@ Point your wasm-gc HTML page at the trimmed file instead of the full bundle:
399424

400425
```html
401426
<script type="module">
402-
import { wasmImportObject } from "./_build/wasm-gc/release/build/myapp/webapi.mjs";
427+
import { wasmImportObject } from "./webapi.mjs";
403428
404429
const { instance } = await WebAssembly.instantiateStreaming(
405-
fetch("./_build/wasm-gc/release/build/myapp/myapp.wasm"),
430+
fetch("./myapp.wasm"),
406431
wasmImportObject,
407432
{ builtins: ["js-string"], importedStringConstants: "_" }
408433
);
@@ -412,7 +437,7 @@ Point your wasm-gc HTML page at the trimmed file instead of the full bundle:
412437

413438
### How It Works
414439

415-
`webapi_trim` parses the wasm binary's import section to find which `webapi_` JS modules are referenced, then extracts only those modules (plus the shared `wasmImportObject` export) from the full `webapi.mjs`. No runtime behavior changes — just fewer unused modules shipped to the browser.
440+
`webapitrim` parses the wasm binary's import section to find which `webapi_` JS modules are referenced, then extracts only those modules (plus the shared `wasmImportObject` export) from the full `webapi.mjs`. No runtime behavior changes — just fewer unused modules shipped to the browser.
416441

417442
## WebIDL to MoonBit Conversion
418443

0 commit comments

Comments
 (0)