build.sh 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #!/bin/bash
  2. #
  3. # Build the JavaScript modules
  4. #
  5. # This script is really a workaround for https://github.com/rustwasm/wasm-pack/issues/1074.
  6. #
  7. # Currently, the only reliable way to load WebAssembly in all the JS
  8. # environments we want to target (web-via-webpack, web-via-browserify, jest)
  9. # seems to be to pack the WASM into base64, and then unpack it and instantiate
  10. # it at runtime.
  11. #
  12. # Hopefully one day, https://github.com/rustwasm/wasm-pack/issues/1074 will be
  13. # fixed and this will be unnecessary.
  14. set -e
  15. cd "$(dirname "$0")"/..
  16. WASM_BINDGEN_WEAKREF=1 wasm-pack build --target nodejs --scope rust-cashu --out-dir pkg "${WASM_PACK_ARGS[@]}"
  17. # Convert the Wasm into a JS file that exports the base64'ed Wasm.
  18. echo "module.exports = \`$(base64 pkg/cashu_js_bg.wasm)\`;" > pkg/cashu_js_bg.wasm.js
  19. # In the JavaScript:
  20. # 1. Strip out the lines that load the WASM, and our new epilogue.
  21. # 2. Remove the imports of `TextDecoder` and `TextEncoder`. We rely on the global defaults.
  22. {
  23. sed -e '/Text..coder.*= require(.util.)/d' \
  24. -e '/^const path = /,$d' pkg/cashu_js.js
  25. cat scripts/epilogue.js
  26. } > pkg/cashu_js.js.new
  27. mv pkg/cashu_js.js.new pkg/cashu_js.js
  28. # also extend the typescript
  29. cat scripts/epilogue.d.ts >> pkg/cashu_js.d.ts