packwise-skills 1.0.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.cursorrules +23 -23
- package/CLAUDE.md +25 -25
- package/LICENSE +21 -0
- package/README.md +404 -295
- package/audit.md +224 -224
- package/bin/packwise.js +322 -155
- package/install.sh +123 -0
- package/package.json +32 -31
- package/skill.md +944 -719
- package/sub-skills/ai/local-llm.md +183 -183
- package/sub-skills/ai/python-ml.md +164 -164
- package/sub-skills/backend/go-server.md +184 -184
- package/sub-skills/backend/java-spring.md +241 -241
- package/sub-skills/backend/node-server.md +164 -164
- package/sub-skills/backend/php-laravel.md +175 -175
- package/sub-skills/backend/python-server.md +164 -164
- package/sub-skills/backend/rust-backend.md +118 -118
- package/sub-skills/cli/python-cli.md +236 -236
- package/sub-skills/cli/sdk-library.md +497 -497
- package/sub-skills/cloud/ci-cd-pipelines.md +350 -350
- package/sub-skills/cloud/docker.md +191 -191
- package/sub-skills/cloud/kubernetes.md +277 -277
- package/sub-skills/cloud/payment-integration.md +307 -307
- package/sub-skills/cross-platform/multiplatform.md +252 -252
- package/sub-skills/desktop/electron.md +783 -783
- package/sub-skills/desktop/game-dev.md +443 -443
- package/sub-skills/desktop/native-app.md +123 -123
- package/sub-skills/desktop/scenarios.md +443 -443
- package/sub-skills/desktop/smart-platforms.md +324 -324
- package/sub-skills/desktop/tauri.md +428 -428
- package/sub-skills/desktop/vr-ar.md +252 -252
- package/sub-skills/desktop/web-to-desktop.md +153 -153
- package/sub-skills/embedded/car-infotainment.md +129 -129
- package/sub-skills/embedded/esp32.md +184 -184
- package/sub-skills/embedded/ros.md +150 -150
- package/sub-skills/embedded/stm32.md +160 -160
- package/sub-skills/mobile/android.md +322 -322
- package/sub-skills/mobile/capacitor.md +232 -232
- package/sub-skills/mobile/flutter-mobile.md +138 -138
- package/sub-skills/mobile/harmonyos.md +150 -150
- package/sub-skills/mobile/ios.md +245 -245
- package/sub-skills/mobile/react-native.md +443 -443
- package/sub-skills/mobile/wearables.md +230 -230
- package/sub-skills/plugins/browser-extension.md +308 -308
- package/sub-skills/plugins/jetbrains-plugin.md +226 -226
- package/sub-skills/plugins/vscode-extension.md +204 -204
- package/sub-skills/security/security-tools.md +174 -174
- package/sub-skills/web/monorepo.md +274 -274
- package/sub-skills/web/pwa.md +220 -220
- package/sub-skills/web/serverless-edge.md +295 -295
- package/sub-skills/web/spa.md +266 -266
- package/sub-skills/web/ssr.md +228 -228
- package/sub-skills/web/wasm.md +243 -243
package/sub-skills/web/wasm.md
CHANGED
|
@@ -1,243 +1,243 @@
|
|
|
1
|
-
# WebAssembly Build Sub-Skill
|
|
2
|
-
|
|
3
|
-
Compile Rust, Go, C/C++, or AssemblyScript to WebAssembly (.wasm) for browser and edge runtime use.
|
|
4
|
-
|
|
5
|
-
**Current version**: WASM 2.0 / wasm-pack 0.13 / Emscripten 3.1 (2025-2026)
|
|
6
|
-
|
|
7
|
-
## When to Use
|
|
8
|
-
|
|
9
|
-
- Performance-critical code in the browser (image processing, crypto, physics, codecs)
|
|
10
|
-
- Reuse existing Rust/Go/C libraries in web frontends
|
|
11
|
-
- Edge computing (Cloudflare Workers, Fastly Compute)
|
|
12
|
-
- Plugin systems (WASM sandboxed execution)
|
|
13
|
-
- Game engines in the browser
|
|
14
|
-
|
|
15
|
-
## Rust → WASM (Recommended Path)
|
|
16
|
-
|
|
17
|
-
### Prerequisites
|
|
18
|
-
|
|
19
|
-
```bash
|
|
20
|
-
# Rust toolchain with WASM target
|
|
21
|
-
rustup target add wasm32-unknown-unknown
|
|
22
|
-
|
|
23
|
-
# wasm-pack (build tool + npm package generator)
|
|
24
|
-
cargo install wasm-pack
|
|
25
|
-
|
|
26
|
-
# wasm-bindgen (JS ↔ Rust interop)
|
|
27
|
-
# (automatically used by wasm-pack)
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
### Project Setup
|
|
31
|
-
|
|
32
|
-
```bash
|
|
33
|
-
# Create library crate
|
|
34
|
-
cargo new --lib my-wasm-lib
|
|
35
|
-
cd my-wasm-lib
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
```toml
|
|
39
|
-
# Cargo.toml
|
|
40
|
-
[lib]
|
|
41
|
-
crate-type = ["cdylib", "rlib"]
|
|
42
|
-
|
|
43
|
-
[dependencies]
|
|
44
|
-
wasm-bindgen = "0.2"
|
|
45
|
-
serde = { version = "1", features = ["derive"] }
|
|
46
|
-
serde-wasm-bindgen = "0.6"
|
|
47
|
-
js-sys = "0.3"
|
|
48
|
-
web-sys = { version = "0.3", features = ["console"] }
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
```rust
|
|
52
|
-
// src/lib.rs
|
|
53
|
-
use wasm_bindgen::prelude::*;
|
|
54
|
-
|
|
55
|
-
#[wasm_bindgen]
|
|
56
|
-
pub fn fibonacci(n: u32) -> u32 {
|
|
57
|
-
match n {
|
|
58
|
-
0 => 0,
|
|
59
|
-
1 => 1,
|
|
60
|
-
_ => fibonacci(n - 1) + fibonacci(n - 2),
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
#[wasm_bindgen]
|
|
65
|
-
pub fn process_image(data: &[u8], width: u32, height: u32) -> Vec<u8> {
|
|
66
|
-
// Heavy computation that would be slow in JS
|
|
67
|
-
data.iter().map(|&pixel| 255 - pixel).collect()
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// Access browser APIs via web-sys
|
|
71
|
-
#[wasm_bindgen]
|
|
72
|
-
pub fn log_to_console(msg: &str) {
|
|
73
|
-
web_sys::console::log_1(&msg.into());
|
|
74
|
-
}
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
### Build
|
|
78
|
-
|
|
79
|
-
```bash
|
|
80
|
-
# Build for bundler (webpack/vite/rollup)
|
|
81
|
-
wasm-pack build --target web
|
|
82
|
-
# Output: pkg/my_wasm_lib_bg.wasm + pkg/my_wasm_lib.js
|
|
83
|
-
|
|
84
|
-
# Build for Node.js
|
|
85
|
-
wasm-pack build --target nodejs
|
|
86
|
-
|
|
87
|
-
# Build for direct browser (no bundler)
|
|
88
|
-
wasm-pack build --target no-modules
|
|
89
|
-
|
|
90
|
-
# Release optimized
|
|
91
|
-
wasm-pack build --target web --release
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
### Use in JavaScript
|
|
95
|
-
|
|
96
|
-
```javascript
|
|
97
|
-
// With bundler (Vite/Webpack)
|
|
98
|
-
import init, { fibonacci, process_image } from './pkg/my_wasm_lib.js';
|
|
99
|
-
|
|
100
|
-
await init(); // Initialize WASM module
|
|
101
|
-
console.log(fibonacci(40)); // Fast!
|
|
102
|
-
|
|
103
|
-
// With vanilla HTML (no-modules target)
|
|
104
|
-
// <script src="./pkg/my_wasm_lib.js"></script>
|
|
105
|
-
// my_wasm_lib().then(module => { module.fibonacci(40); });
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
### Vite Integration
|
|
109
|
-
|
|
110
|
-
```bash
|
|
111
|
-
# vite-plugin-wasm 3.6+ and vite-plugin-top-level-await 1.6+ both support Vite 2-8
|
|
112
|
-
npm install vite-plugin-wasm vite-plugin-top-level-await
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
```javascript
|
|
116
|
-
// vite.config.js
|
|
117
|
-
import wasm from 'vite-plugin-wasm';
|
|
118
|
-
import topLevelAwait from 'vite-plugin-top-level-await';
|
|
119
|
-
|
|
120
|
-
export default {
|
|
121
|
-
plugins: [wasm(), topLevelAwait()],
|
|
122
|
-
};
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
## Go → WASM
|
|
126
|
-
|
|
127
|
-
```bash
|
|
128
|
-
# Go has built-in WASM support (GOOS=js GOARCH=wasm)
|
|
129
|
-
GOOS=js GOARCH=wasm go build -o main.wasm .
|
|
130
|
-
|
|
131
|
-
# Copy the WASM exec helper
|
|
132
|
-
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" .
|
|
133
|
-
|
|
134
|
-
# The wasm_exec.js file is required to load the WASM module in browsers
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
```go
|
|
138
|
-
// main.go
|
|
139
|
-
package main
|
|
140
|
-
|
|
141
|
-
import (
|
|
142
|
-
"fmt"
|
|
143
|
-
"syscall/js"
|
|
144
|
-
)
|
|
145
|
-
|
|
146
|
-
func fibonacci(this js.Value, args []js.Value) interface{} {
|
|
147
|
-
n := args[0].Int()
|
|
148
|
-
if n <= 1 { return n }
|
|
149
|
-
a, b := 0, 1
|
|
150
|
-
for i := 2; i <= n; i++ { a, b = b, a+b }
|
|
151
|
-
return b
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
func main() {
|
|
155
|
-
c := make(chan struct{})
|
|
156
|
-
js.Global().Set("fibonacci", js.FuncOf(fibonacci))
|
|
157
|
-
fmt.Println("Go WASM initialized")
|
|
158
|
-
<-c // Keep running
|
|
159
|
-
}
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
```html
|
|
163
|
-
<!-- Load in browser -->
|
|
164
|
-
<script src="wasm_exec.js"></script>
|
|
165
|
-
<script>
|
|
166
|
-
const go = new Go();
|
|
167
|
-
WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject)
|
|
168
|
-
.then(result => {
|
|
169
|
-
go.run(result.instance);
|
|
170
|
-
console.log(fibonacci(40));
|
|
171
|
-
});
|
|
172
|
-
</script>
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
## C/C++ → WASM (Emscripten)
|
|
176
|
-
|
|
177
|
-
```bash
|
|
178
|
-
# Install Emscripten
|
|
179
|
-
git clone https://github.com/emscripten-core/emsdk.git
|
|
180
|
-
cd emsdk && ./emsdk install latest && ./emsdk activate latest
|
|
181
|
-
source ./emsdk_env.sh
|
|
182
|
-
|
|
183
|
-
# Compile C to WASM
|
|
184
|
-
emcc myfile.c -o myfile.js -s WASM=1 -s EXPORTED_FUNCTIONS='["_process_data"]' -s EXPORTED_RUNTIME_METHODS='["ccall","cwrap"]'
|
|
185
|
-
|
|
186
|
-
# Compile with optimization
|
|
187
|
-
emcc myfile.c -o myfile.js -O3 -s WASM=1 --closure 1
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
## AssemblyScript → WASM
|
|
191
|
-
|
|
192
|
-
```bash
|
|
193
|
-
# AssemblyScript: TypeScript-like syntax compiled to WASM
|
|
194
|
-
npm install -g assemblyscript
|
|
195
|
-
npx asc myModule.ts --outFile myModule.wasm --optimize
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
```typescript
|
|
199
|
-
// myModule.ts (AssemblyScript — NOT regular TypeScript)
|
|
200
|
-
export function fibonacci(n: i32): i32 {
|
|
201
|
-
if (n <= 1) return n;
|
|
202
|
-
let a = 0, b = 1;
|
|
203
|
-
for (let i = 2; i <= n; i++) {
|
|
204
|
-
const temp = b;
|
|
205
|
-
b = a + b;
|
|
206
|
-
a = temp;
|
|
207
|
-
}
|
|
208
|
-
return b;
|
|
209
|
-
}
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
## Size Optimization
|
|
213
|
-
|
|
214
|
-
| Technique | Tool | Typical Savings |
|
|
215
|
-
|-----------|------|----------------|
|
|
216
|
-
| `wasm-opt` | `wasm-pack` runs automatically | 10–30% |
|
|
217
|
-
| `--release` flag | All toolchains | 40–60% vs debug |
|
|
218
|
-
| `wee_alloc` | Rust: `#[global_allocator]` | 10–20KB |
|
|
219
|
-
| Feature gates | Cargo: `default-features = false` | Varies |
|
|
220
|
-
| `twiggy` profiler | `cargo install twiggy` | Identify bloat |
|
|
221
|
-
| Brotli compression | Server-side | 70–80% transfer size |
|
|
222
|
-
|
|
223
|
-
```rust
|
|
224
|
-
// Use wee_alloc to reduce WASM binary size
|
|
225
|
-
extern crate wee_alloc;
|
|
226
|
-
#[global_allocator]
|
|
227
|
-
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
|
228
|
-
```
|
|
229
|
-
|
|
230
|
-
## Common Pitfalls
|
|
231
|
-
|
|
232
|
-
| Issue | Fix |
|
|
233
|
-
|-------|-----|
|
|
234
|
-
| WASM binary too large | Use `--release`; enable `wee_alloc`; check features with `twiggy` |
|
|
235
|
-
| `wasm-pack build` fails | Ensure `wasm-bindgen` version matches `wasm-pack` version |
|
|
236
|
-
| Vite doesn't load WASM | Add `vite-plugin-wasm` and `vite-plugin-top-level-await` |
|
|
237
|
-
| Go WASM not loading | Must include `wasm_exec.js` from Go installation |
|
|
238
|
-
| CORS error when loading .wasm | Serve from same origin; add MIME type `application/wasm` |
|
|
239
|
-
| Can't use `console.log` from Rust | Use `web_sys::console::log_1` via `web-sys` crate |
|
|
240
|
-
| Memory grows unbounded | Use `#[wasm_bindgen]` memory management; call `drop()` explicitly |
|
|
241
|
-
| Debugging is hard | Use `console_error_panic_hook` crate for Rust; `console.log` for Go |
|
|
242
|
-
| Thread support | WASM threads require `SharedArrayBuffer` + COOP/COEP headers |
|
|
243
|
-
| SIMD support | Use `wasm32-unknown-unknown` target with nightly or stable 1.72+ |
|
|
1
|
+
# WebAssembly Build Sub-Skill
|
|
2
|
+
|
|
3
|
+
Compile Rust, Go, C/C++, or AssemblyScript to WebAssembly (.wasm) for browser and edge runtime use.
|
|
4
|
+
|
|
5
|
+
**Current version**: WASM 2.0 / wasm-pack 0.13 / Emscripten 3.1 (2025-2026)
|
|
6
|
+
|
|
7
|
+
## When to Use
|
|
8
|
+
|
|
9
|
+
- Performance-critical code in the browser (image processing, crypto, physics, codecs)
|
|
10
|
+
- Reuse existing Rust/Go/C libraries in web frontends
|
|
11
|
+
- Edge computing (Cloudflare Workers, Fastly Compute)
|
|
12
|
+
- Plugin systems (WASM sandboxed execution)
|
|
13
|
+
- Game engines in the browser
|
|
14
|
+
|
|
15
|
+
## Rust → WASM (Recommended Path)
|
|
16
|
+
|
|
17
|
+
### Prerequisites
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
# Rust toolchain with WASM target
|
|
21
|
+
rustup target add wasm32-unknown-unknown
|
|
22
|
+
|
|
23
|
+
# wasm-pack (build tool + npm package generator)
|
|
24
|
+
cargo install wasm-pack
|
|
25
|
+
|
|
26
|
+
# wasm-bindgen (JS ↔ Rust interop)
|
|
27
|
+
# (automatically used by wasm-pack)
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Project Setup
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
# Create library crate
|
|
34
|
+
cargo new --lib my-wasm-lib
|
|
35
|
+
cd my-wasm-lib
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```toml
|
|
39
|
+
# Cargo.toml
|
|
40
|
+
[lib]
|
|
41
|
+
crate-type = ["cdylib", "rlib"]
|
|
42
|
+
|
|
43
|
+
[dependencies]
|
|
44
|
+
wasm-bindgen = "0.2"
|
|
45
|
+
serde = { version = "1", features = ["derive"] }
|
|
46
|
+
serde-wasm-bindgen = "0.6"
|
|
47
|
+
js-sys = "0.3"
|
|
48
|
+
web-sys = { version = "0.3", features = ["console"] }
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```rust
|
|
52
|
+
// src/lib.rs
|
|
53
|
+
use wasm_bindgen::prelude::*;
|
|
54
|
+
|
|
55
|
+
#[wasm_bindgen]
|
|
56
|
+
pub fn fibonacci(n: u32) -> u32 {
|
|
57
|
+
match n {
|
|
58
|
+
0 => 0,
|
|
59
|
+
1 => 1,
|
|
60
|
+
_ => fibonacci(n - 1) + fibonacci(n - 2),
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
#[wasm_bindgen]
|
|
65
|
+
pub fn process_image(data: &[u8], width: u32, height: u32) -> Vec<u8> {
|
|
66
|
+
// Heavy computation that would be slow in JS
|
|
67
|
+
data.iter().map(|&pixel| 255 - pixel).collect()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Access browser APIs via web-sys
|
|
71
|
+
#[wasm_bindgen]
|
|
72
|
+
pub fn log_to_console(msg: &str) {
|
|
73
|
+
web_sys::console::log_1(&msg.into());
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Build
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
# Build for bundler (webpack/vite/rollup)
|
|
81
|
+
wasm-pack build --target web
|
|
82
|
+
# Output: pkg/my_wasm_lib_bg.wasm + pkg/my_wasm_lib.js
|
|
83
|
+
|
|
84
|
+
# Build for Node.js
|
|
85
|
+
wasm-pack build --target nodejs
|
|
86
|
+
|
|
87
|
+
# Build for direct browser (no bundler)
|
|
88
|
+
wasm-pack build --target no-modules
|
|
89
|
+
|
|
90
|
+
# Release optimized
|
|
91
|
+
wasm-pack build --target web --release
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Use in JavaScript
|
|
95
|
+
|
|
96
|
+
```javascript
|
|
97
|
+
// With bundler (Vite/Webpack)
|
|
98
|
+
import init, { fibonacci, process_image } from './pkg/my_wasm_lib.js';
|
|
99
|
+
|
|
100
|
+
await init(); // Initialize WASM module
|
|
101
|
+
console.log(fibonacci(40)); // Fast!
|
|
102
|
+
|
|
103
|
+
// With vanilla HTML (no-modules target)
|
|
104
|
+
// <script src="./pkg/my_wasm_lib.js"></script>
|
|
105
|
+
// my_wasm_lib().then(module => { module.fibonacci(40); });
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Vite Integration
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
# vite-plugin-wasm 3.6+ and vite-plugin-top-level-await 1.6+ both support Vite 2-8
|
|
112
|
+
npm install vite-plugin-wasm vite-plugin-top-level-await
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
```javascript
|
|
116
|
+
// vite.config.js
|
|
117
|
+
import wasm from 'vite-plugin-wasm';
|
|
118
|
+
import topLevelAwait from 'vite-plugin-top-level-await';
|
|
119
|
+
|
|
120
|
+
export default {
|
|
121
|
+
plugins: [wasm(), topLevelAwait()],
|
|
122
|
+
};
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Go → WASM
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
# Go has built-in WASM support (GOOS=js GOARCH=wasm)
|
|
129
|
+
GOOS=js GOARCH=wasm go build -o main.wasm .
|
|
130
|
+
|
|
131
|
+
# Copy the WASM exec helper
|
|
132
|
+
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" .
|
|
133
|
+
|
|
134
|
+
# The wasm_exec.js file is required to load the WASM module in browsers
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
```go
|
|
138
|
+
// main.go
|
|
139
|
+
package main
|
|
140
|
+
|
|
141
|
+
import (
|
|
142
|
+
"fmt"
|
|
143
|
+
"syscall/js"
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
func fibonacci(this js.Value, args []js.Value) interface{} {
|
|
147
|
+
n := args[0].Int()
|
|
148
|
+
if n <= 1 { return n }
|
|
149
|
+
a, b := 0, 1
|
|
150
|
+
for i := 2; i <= n; i++ { a, b = b, a+b }
|
|
151
|
+
return b
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
func main() {
|
|
155
|
+
c := make(chan struct{})
|
|
156
|
+
js.Global().Set("fibonacci", js.FuncOf(fibonacci))
|
|
157
|
+
fmt.Println("Go WASM initialized")
|
|
158
|
+
<-c // Keep running
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
```html
|
|
163
|
+
<!-- Load in browser -->
|
|
164
|
+
<script src="wasm_exec.js"></script>
|
|
165
|
+
<script>
|
|
166
|
+
const go = new Go();
|
|
167
|
+
WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject)
|
|
168
|
+
.then(result => {
|
|
169
|
+
go.run(result.instance);
|
|
170
|
+
console.log(fibonacci(40));
|
|
171
|
+
});
|
|
172
|
+
</script>
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## C/C++ → WASM (Emscripten)
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
# Install Emscripten
|
|
179
|
+
git clone https://github.com/emscripten-core/emsdk.git
|
|
180
|
+
cd emsdk && ./emsdk install latest && ./emsdk activate latest
|
|
181
|
+
source ./emsdk_env.sh
|
|
182
|
+
|
|
183
|
+
# Compile C to WASM
|
|
184
|
+
emcc myfile.c -o myfile.js -s WASM=1 -s EXPORTED_FUNCTIONS='["_process_data"]' -s EXPORTED_RUNTIME_METHODS='["ccall","cwrap"]'
|
|
185
|
+
|
|
186
|
+
# Compile with optimization
|
|
187
|
+
emcc myfile.c -o myfile.js -O3 -s WASM=1 --closure 1
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## AssemblyScript → WASM
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
# AssemblyScript: TypeScript-like syntax compiled to WASM
|
|
194
|
+
npm install -g assemblyscript
|
|
195
|
+
npx asc myModule.ts --outFile myModule.wasm --optimize
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
// myModule.ts (AssemblyScript — NOT regular TypeScript)
|
|
200
|
+
export function fibonacci(n: i32): i32 {
|
|
201
|
+
if (n <= 1) return n;
|
|
202
|
+
let a = 0, b = 1;
|
|
203
|
+
for (let i = 2; i <= n; i++) {
|
|
204
|
+
const temp = b;
|
|
205
|
+
b = a + b;
|
|
206
|
+
a = temp;
|
|
207
|
+
}
|
|
208
|
+
return b;
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Size Optimization
|
|
213
|
+
|
|
214
|
+
| Technique | Tool | Typical Savings |
|
|
215
|
+
|-----------|------|----------------|
|
|
216
|
+
| `wasm-opt` | `wasm-pack` runs automatically | 10–30% |
|
|
217
|
+
| `--release` flag | All toolchains | 40–60% vs debug |
|
|
218
|
+
| `wee_alloc` | Rust: `#[global_allocator]` | 10–20KB |
|
|
219
|
+
| Feature gates | Cargo: `default-features = false` | Varies |
|
|
220
|
+
| `twiggy` profiler | `cargo install twiggy` | Identify bloat |
|
|
221
|
+
| Brotli compression | Server-side | 70–80% transfer size |
|
|
222
|
+
|
|
223
|
+
```rust
|
|
224
|
+
// Use wee_alloc to reduce WASM binary size
|
|
225
|
+
extern crate wee_alloc;
|
|
226
|
+
#[global_allocator]
|
|
227
|
+
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## Common Pitfalls
|
|
231
|
+
|
|
232
|
+
| Issue | Fix |
|
|
233
|
+
|-------|-----|
|
|
234
|
+
| WASM binary too large | Use `--release`; enable `wee_alloc`; check features with `twiggy` |
|
|
235
|
+
| `wasm-pack build` fails | Ensure `wasm-bindgen` version matches `wasm-pack` version |
|
|
236
|
+
| Vite doesn't load WASM | Add `vite-plugin-wasm` and `vite-plugin-top-level-await` |
|
|
237
|
+
| Go WASM not loading | Must include `wasm_exec.js` from Go installation |
|
|
238
|
+
| CORS error when loading .wasm | Serve from same origin; add MIME type `application/wasm` |
|
|
239
|
+
| Can't use `console.log` from Rust | Use `web_sys::console::log_1` via `web-sys` crate |
|
|
240
|
+
| Memory grows unbounded | Use `#[wasm_bindgen]` memory management; call `drop()` explicitly |
|
|
241
|
+
| Debugging is hard | Use `console_error_panic_hook` crate for Rust; `console.log` for Go |
|
|
242
|
+
| Thread support | WASM threads require `SharedArrayBuffer` + COOP/COEP headers |
|
|
243
|
+
| SIMD support | Use `wasm32-unknown-unknown` target with nightly or stable 1.72+ |
|