build-wasm.sh raw

   1  #!/bin/bash
   2  # Build the WasmDB WASM module for browser use
   3  #
   4  # Output: wasmdb.wasm in the repository root
   5  #
   6  # Usage:
   7  #   ./scripts/build-wasm.sh
   8  #   ./scripts/build-wasm.sh --output /path/to/output/wasmdb.wasm
   9  
  10  set -e
  11  
  12  SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  13  REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
  14  
  15  OUTPUT_PATH="${REPO_ROOT}/wasmdb.wasm"
  16  
  17  # Parse arguments
  18  while [[ $# -gt 0 ]]; do
  19      case $1 in
  20          --output|-o)
  21              OUTPUT_PATH="$2"
  22              shift 2
  23              ;;
  24          *)
  25              echo "Unknown option: $1"
  26              exit 1
  27              ;;
  28      esac
  29  done
  30  
  31  echo "Building WasmDB WASM module..."
  32  echo "Output: ${OUTPUT_PATH}"
  33  
  34  cd "${REPO_ROOT}"
  35  
  36  # Build with optimizations
  37  GOOS=js GOARCH=wasm go build \
  38      -ldflags="-s -w" \
  39      -o "${OUTPUT_PATH}" \
  40      ./cmd/wasmdb
  41  
  42  # Get the size
  43  SIZE=$(du -h "${OUTPUT_PATH}" | cut -f1)
  44  echo "Build complete: ${OUTPUT_PATH} (${SIZE})"
  45  
  46  # Copy wasm_exec.js from Go installation if not present
  47  WASM_EXEC="${REPO_ROOT}/wasm_exec.js"
  48  if [ ! -f "${WASM_EXEC}" ]; then
  49      GO_ROOT=$(go env GOROOT)
  50      # Try lib/wasm first (newer Go versions), then misc/wasm
  51      if [ -f "${GO_ROOT}/lib/wasm/wasm_exec.js" ]; then
  52          cp "${GO_ROOT}/lib/wasm/wasm_exec.js" "${WASM_EXEC}"
  53          echo "Copied wasm_exec.js to ${WASM_EXEC}"
  54      elif [ -f "${GO_ROOT}/misc/wasm/wasm_exec.js" ]; then
  55          cp "${GO_ROOT}/misc/wasm/wasm_exec.js" "${WASM_EXEC}"
  56          echo "Copied wasm_exec.js to ${WASM_EXEC}"
  57      else
  58          echo "Warning: wasm_exec.js not found in Go installation"
  59          echo "Checked: ${GO_ROOT}/lib/wasm/wasm_exec.js"
  60          echo "Checked: ${GO_ROOT}/misc/wasm/wasm_exec.js"
  61          echo "You'll need to copy it manually from your Go installation"
  62      fi
  63  fi
  64  
  65  echo "Done!"
  66