Skip to content

Commit 3935396

Browse files
authored
Merge pull request #74 from ChainSafe/irubido/addWorkerModuleScript
fix: add missing web worker module bundler with post build script
2 parents 6aabe0f + 70970c7 commit 3935396

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

add-worker-module.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
3+
# Create the directory if it doesn't exist
4+
mkdir -p packages/webz-wallet/snippets/wasm_thread-8ee53d0673203880/src/wasm32/js
5+
6+
# Create the worker module file
7+
cat > packages/webz-wallet/snippets/wasm_thread-8ee53d0673203880/src/wasm32/js/web_worker_module.bundler.js << 'EOL'
8+
// synchronously, using the browser, import wasm_bindgen shim JS scripts
9+
import init, { wasm_thread_entry_point } from "../../../../../";
10+
// Wait for the main thread to send us the shared module/memory and work context.
11+
// Once we've got it, initialize it all with the `wasm_bindgen` global we imported via
12+
// `importScripts`.
13+
self.onmessage = event => {
14+
let [ module, memory, work, thread_key ] = event.data;
15+
init(module, memory).catch(err => {
16+
console.log(err);
17+
const error = new Error(err.message);
18+
error.customProperty = "This error right here!";
19+
// Propagate to main `onerror`:
20+
setTimeout(() => {
21+
throw error;
22+
});
23+
// Rethrow to keep promise rejected and prevent execution of further commands:
24+
throw error;
25+
}).then(() => {
26+
// Enter rust code by calling entry point defined in `lib.rs`.
27+
// This executes closure defined by work context.
28+
wasm_thread_entry_point(work);
29+
});
30+
};
31+
self.onunhandledrejection = function(e) {
32+
console.error('Worker unhandled rejection:', e.reason);
33+
throw e.reason;
34+
};
35+
self.onerror = function(e) {
36+
console.error('Worker error:', e.message);
37+
throw e;
38+
};
39+
40+
self.onended = function(e) {
41+
console.error('Worker ended:', e.message);
42+
throw e;
43+
}
44+
EOL

justfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ build:
88

99
build-wallet *features:
1010
cd crates/webz-wallet && wasm-pack build -t web --release --scope webzjs --out-dir ../../packages/webz-wallet --no-default-features --features="wasm wasm-parallel {{features}}" -Z build-std="panic_abort,std"
11+
./add-worker-module.sh
1112

1213
build-keys *features:
1314
cd crates/webz-keys && wasm-pack build -t web --release --scope webzjs --out-dir ../../packages/webz-keys --no-default-features --features="{{features}}" -Z build-std="panic_abort,std"

0 commit comments

Comments
 (0)