浏览代码

add script to build wasm artifact

Getty Ritter 9 月之前
父节点
当前提交
a58840a383
共有 2 个文件被更改,包括 71 次插入0 次删除
  1. 2 0
      .gitignore
  2. 69 0
      build-wasm.sh

+ 2 - 0
.gitignore

@@ -1,2 +1,4 @@
 /target
 *.blend1
+out
+*.tar.gz

+ 69 - 0
build-wasm.sh

@@ -0,0 +1,69 @@
+#!/bin/bash -e
+
+dbg() {
+    printf "\x1b[92m[..] $@\x1b[39m\n"
+}
+
+error() {
+    printf "\x1b[91m[..] $@\x1b[39m\n"
+}
+
+dbg "creating webassembly deploy artifact"
+
+# install wasm-bindgen if we don't have it
+which wasm-bindgen >/dev/null 2>&1 || ( error 'wasm-bindgen not found: run cargo install `wasm-bindgen-cli`' ; exit 1 )
+
+if [ -e ./stories.tar.gz ]; then
+    dbg "deleting ./stories.tar.gz"
+    rm ./stories.tar.gz
+fi
+
+dbg "creating release build for wasm32"
+cargo build --release --target wasm32-unknown-unknown
+dbg "running wasm-bindgen"
+wasm-bindgen --no-typescript --target web \
+             --out-dir ./out/ \
+             --out-name "stories" \
+             ./target/wasm32-unknown-unknown/release/stories.wasm
+
+dbg "assembling final artifact"
+TMP=$(mktemp -d)
+trap "rm -rf $TMP" EXIT
+DIR=$TMP/stories
+mkdir -p $DIR
+cp ./out/stories.js $DIR
+cp ./out/stories_bg.wasm $DIR
+cat >$DIR/index.html <<EOF
+<!doctype html>
+<html lang="en">
+
+  <body style="margin: 0px;">
+    <script type="module">
+     import init from './stories.js'
+
+     init().catch((error) => {
+       if (!error.message.startsWith("Using exceptions for control flow, don't mind me. This isn't actually an error!")) {
+         throw error;
+       }
+     });
+    </script>
+  </body>
+
+</html>
+EOF
+
+dbg "copying assets"
+mkdir -p $DIR/assets
+cp -r ./assets/* $DIR/assets/.
+dbg "removing asset sources"
+rm -f $DIR/assets/*.blend
+
+dbg "creating tarball"
+(
+    cd $TMP
+    tar -cf stories.tar ./stories
+    gzip stories.tar
+)
+
+dbg "cleaning up..."
+cp $TMP/stories.tar.gz ./stories.tar.gz