Skip to content

Commit 5086e72

Browse files
shesekjgriffiths
authored andcommitted
wasm: Fix Webpack compatibility for browser use, update package.json
Updating emsdk to v3.1.27 was necessary for emscripten-core/emscripten#17915, which allows using the same WASM build for both browser and nodejs envs.
1 parent 901faac commit 5086e72

File tree

12 files changed

+2447
-13
lines changed

12 files changed

+2447
-13
lines changed

.github/workflows/wasm-package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- run: sudo ../../contrib/bullseye_deps.sh
2929

3030
# Build NPM package into a tgz file (pack internally triggers the build/prepare script)
31-
- run: npm pack --foreground-scripts
31+
- run: npm install && npm pack --foreground-scripts
3232
name: Build & Pack
3333

3434
# Report the SHA256 digest of the final package. This should be deterministic (including generated WASM),

.gitlab-ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
bullseye_release:
2-
image: greenaddress/wallycore@sha256:100ff7b90a676f84357f519f2e4ebebff08277c74da711a70340bc58b118b3a0
2+
image: greenaddress/wallycore@sha256:8ced27d7281cc9a98301dea544d3f9c64be1c3da013faa0a6c5fd16622f40942
33
artifacts:
44
expire_in: 1 day
55
name: wallycore-bindings
@@ -28,7 +28,7 @@ bullseye_release:
2828
- gzip -9 wallycore-wasm.tar
2929

3030
manylinux_release:
31-
image: greenaddress/wallycore@sha256:100ff7b90a676f84357f519f2e4ebebff08277c74da711a70340bc58b118b3a0
31+
image: greenaddress/wallycore@sha256:8ced27d7281cc9a98301dea544d3f9c64be1c3da013faa0a6c5fd16622f40942
3232
artifacts:
3333
expire_in: 1 day
3434
name: wallycore-bindings
@@ -119,7 +119,7 @@ osx_release:
119119
# - tools\msvc\wheel.bat
120120

121121
apidocs:
122-
image: greenaddress/wallycore@sha256:100ff7b90a676f84357f519f2e4ebebff08277c74da711a70340bc58b118b3a0
122+
image: greenaddress/wallycore@sha256:8ced27d7281cc9a98301dea544d3f9c64be1c3da013faa0a6c5fd16622f40942
123123
artifacts:
124124
expire_in: 14 days
125125
name: wallycore-apidocs

contrib/bullseye_deps.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ unzip -qq ${NDK_FILENAME}
2222
rm ${NDK_FILENAME}
2323
git clone https://github.com/emscripten-core/emsdk
2424
cd emsdk
25-
./emsdk install 3.1.20
26-
./emsdk activate 3.1.20
25+
./emsdk install 3.1.27
26+
./emsdk activate 3.1.27
2727
source ./emsdk_env.sh
2828

2929
apt-get remove --purge curl unzip -yqq

src/wasm_package/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
libwally_wasm
2+
node_modules

src/wasm_package/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ $ npm install wallycore
1111

1212
## Example use
1313

14+
With ES modules:
15+
1416
```js
1517
import wally from 'wallycore'
1618

@@ -25,5 +27,18 @@ wally.tx_free(tx)
2527

2628
If you're using CommonJS, the module can be loaded asynchronously using `const wally = await import('wallycore')` or `import('wallycore').then(wally => { ... })`.
2729

30+
For browser use, you may use a bundler like [webpack](https://webpack.js.org/),
31+
or use the pre-bundled [`wallycore.bundle.js`](wallycore.bundle.js) file which exposes a global `WallyInit` promise that resolves to the module. For example:
32+
33+
```html
34+
<script src="wallycore/wallycore.bundle.min.js"></script>
35+
<script>
36+
WallyInit.then(wally => {
37+
console.log(wally.bip39_get_word(null, 10))
38+
})
39+
// or `const wally = await WallyInit`
40+
</script>
41+
```
42+
2843
## License
2944
[BSD/MIT](https://github.com/ElementsProject/libwally-core/blob/master/LICENSE)

src/wasm_package/browser/assert.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Minimal shim for nodejs's assert module, for the browser
2+
// For some reason, this doesn't work with the https://github.com/browserify/commonjs-assert shim,
3+
// which causes the webpack bundle to hang when attempting to import it.
4+
5+
export default function assert(val, err='assertion failed') {
6+
if (!val) throw err
7+
}

src/wasm_package/build.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ set -xeo pipefail
1010
# Build WASM (Elements is always enabled)
1111
(cd ../.. && ./tools/build_wasm.sh --enable-elements)
1212
mkdir -p libwally_wasm && cp ../../wally_dist/wallycore.{js,wasm} libwally_wasm/
13-
# Rename to force commonjs mode. See https://github.com/emscripten-core/emscripten/pull/17451
14-
mv libwally_wasm/wallycore.js libwally_wasm/wallycore.cjs
13+
touch libwally_wasm/index # necessary for webpack to work (fixes "Can't resolve './' in 'wasm_package/libwally_wasm'")
14+
15+
# Build browser bundle (to dist/wallycore.bundle.js, see webpack.config.js)
16+
webpack --mode production

src/wasm_package/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from 'assert'
2-
import InitWallyModule from './libwally_wasm/wallycore.cjs'
2+
import InitWallyModule from './libwally_wasm/wallycore.js'
33
import { WALLY_OK, WALLY_ERROR, WALLY_EINVAL, WALLY_ENOMEM } from './const.js'
44

55
// Initialize the underlying WASM module and expose it publicly

0 commit comments

Comments
 (0)