Skip to content

Commit f9338e0

Browse files
committed
♻️ inline WASM binary into generated TypeScript module
Replace runtime filesystem read of clayterm.wasm with a build step that base64-encodes the binary into wasm.ts. This removes the node:fs/promises dependency and --allow-read requirement for the WASM file.
1 parent d402924 commit f9338e0

7 files changed

Lines changed: 30 additions & 14 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/.agent-shell/
22
/clayterm.wasm
3+
/wasm.ts
34
/build/
45
/node_modules/

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ LDFLAGS = -Wl,--no-entry \
1313
-Wl,--undefined=Clay__MeasureText \
1414
-Wl,--undefined=Clay__QueryScrollOffset
1515

16-
all: $(TARGET)
16+
all: $(TARGET) wasm.ts
1717
@echo "Built $(TARGET) ($$(wc -c < $(TARGET)) bytes)"
1818

1919
DEPS = $(wildcard src/*.c src/*.h)
2020

2121
$(TARGET): $(DEPS)
2222
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(SRC)
2323

24+
wasm.ts: $(TARGET)
25+
deno run --allow-read --allow-write tasks/bundle-wasm.ts
26+
2427
clean:
25-
rm -f $(TARGET)
28+
rm -f $(TARGET) wasm.ts
2629

2730
.PHONY: all clean

deno.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,28 @@
22
"name": "@clayterm/clayterm",
33
"license": "MIT",
44
"tasks": {
5-
"test": "deno test --allow-read",
5+
"test": "deno test",
66
"fmt": "deno fmt && clang-format -i src/*.c src/*.h",
77
"fmt:check": "deno fmt --check && clang-format --dry-run --Werror src/*.c src/*.h",
88
"build:npm": "deno run -A tasks/build-npm.ts",
99
"build:jsr": "deno run -A tasks/build-jsr.ts",
10-
"demo": "deno run -A demo/keyboard.ts"
10+
"demo": "deno run demo/keyboard.ts"
1111
},
1212
"imports": {
1313
"@std/testing": "jsr:@std/testing@1",
1414
"@std/expect": "jsr:@std/expect@1",
1515
"@sinclair/typebox": "npm:@sinclair/typebox@^0.34",
1616
"dnt": "jsr:@deno/dnt@0.42.3",
17-
"effection": "npm:effection@^4.0.2"
17+
"effection": "npm:effection@^4.0.2",
18+
"@std/encoding": "jsr:@std/encoding@1"
1819
},
1920
"exports": {
2021
".": "./mod.ts",
2122
"./validate": "./validate.ts"
2223
},
2324
"publish": {
24-
"include": ["*.ts", "clayterm.wasm"]
25+
"include": ["*.ts"],
26+
"exclude": ["!wasm.ts"]
2527
},
2628
"fmt": {
2729
"exclude": ["clay", "build"]

deno.lock

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

tasks/build-npm.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,3 @@ await build({
4444
});
4545

4646
await Deno.copyFile("README.md", `${outDir}/README.md`);
47-
await Deno.copyFile("clayterm.wasm", `${outDir}/esm/clayterm.wasm`);

tasks/bundle-wasm.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { encodeBase64 } from "@std/encoding/base64";
2+
3+
const wasm = await Deno.readFile("clayterm.wasm");
4+
const base64 = encodeBase64(wasm);
5+
6+
const source = `const bin = atob("${base64}");
7+
const bytes = new Uint8Array(bin.length);
8+
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
9+
export const compiled = await WebAssembly.compile(bytes);
10+
`;
11+
12+
await Deno.writeTextFile("wasm.ts", source);
13+
console.log(`wrote wasm.ts (${wasm.length} bytes encoded)`);

wasm.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)