build-wasm.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/bin/bash -e
  2. dbg() {
  3. printf "\x1b[92m[..] $@\x1b[39m\n"
  4. }
  5. error() {
  6. printf "\x1b[91m[..] $@\x1b[39m\n"
  7. }
  8. dbg "creating webassembly deploy artifact"
  9. # install wasm-bindgen if we don't have it
  10. which wasm-bindgen >/dev/null 2>&1 || ( error 'wasm-bindgen not found: run cargo install `wasm-bindgen-cli`' ; exit 1 )
  11. if [ -e ./stories.tar.gz ]; then
  12. dbg "deleting ./stories.tar.gz"
  13. rm ./stories.tar.gz
  14. fi
  15. dbg "creating release build for wasm32"
  16. cargo build --release --target wasm32-unknown-unknown
  17. dbg "running wasm-bindgen"
  18. wasm-bindgen --no-typescript --target web \
  19. --out-dir ./out/ \
  20. --out-name "stories" \
  21. ./target/wasm32-unknown-unknown/release/stories.wasm
  22. dbg "assembling final artifact"
  23. TMP=$(mktemp -d)
  24. trap "rm -rf $TMP" EXIT
  25. DIR=$TMP/stories
  26. mkdir -p $DIR
  27. cp ./out/stories.js $DIR
  28. cp ./out/stories_bg.wasm $DIR
  29. cat >$DIR/index.html <<EOF
  30. <!doctype html>
  31. <html lang="en">
  32. <body style="margin: 0px;">
  33. <script type="module">
  34. import init from './stories.js'
  35. init().catch((error) => {
  36. if (!error.message.startsWith("Using exceptions for control flow, don't mind me. This isn't actually an error!")) {
  37. throw error;
  38. }
  39. });
  40. </script>
  41. </body>
  42. </html>
  43. EOF
  44. dbg "copying assets"
  45. mkdir -p $DIR/assets
  46. cp -r ./assets/* $DIR/assets/.
  47. dbg "removing asset sources"
  48. rm -f $DIR/assets/*.blend
  49. dbg "creating tarball"
  50. (
  51. cd $TMP
  52. tar -cf stories.tar ./stories
  53. gzip stories.tar
  54. )
  55. dbg "cleaning up..."
  56. cp $TMP/stories.tar.gz ./stories.tar.gz