WasmServe

repository·main·Indexed 43 days ago

https://github.com/hajimehoshi/wasmserve

An HTTP server for testing and developing Go applications compiled for WebAssembly (GOOS=js/GOARCH=wasm). It automates the build process, manages wasm_exec.js, and provides a /_notify endpoint to trigger browser reloads for a live-refresh development workflow.

Tokens
1.2K
Snippets
6
Records
8
Agent score
5%

What's inside WasmServe

  1. Run a remote or local Go package with WasmServe

    main

    You can serve a remote Go package by providing its import path, or a local package by providing its directory path.

    To run a remote package:

    wasmserve github.com/hajimehoshi/wasmserve/example

    To run a local package:

    wasmserve ./path/to/local/package

    After running the command, open http://localhost:8080/ in your browser to view the application.

    # Example: Running a remote package
    wasmserve github.com/hajimehoshi/wasmserve/example
    
    # Example: Running a local package
    git clone https://github.com/hajimehoshi/ebiten
    cd ebiten
    wasmserve ./examples/sprites
  2. How wasmserve handles builds and serving

    main

    WasmServe automates the workflow for developing WebAssembly applications in Go. When you access the server:

    1. Automatic Compilation: If main.wasm is not found, wasmserve automatically runs go build with GOOS=js and GOARCH=wasm to generate it.
    2. Dynamic index.html: It serves a generated index.html that automatically configures the Go instance with your current environment variables (env) and command-line arguments (argv).
    3. wasm_exec.js Management: It automatically locates and serves the correct wasm_exec.js from your local $GOROOT (supporting both Go 1.24+ and older versions). If it cannot find it locally, it attempts to fetch it.
    4. Live Reloading: The server implements a notification mechanism. The client-side script waits on a _wait endpoint. When a change is detected (triggered via _notify), the server notifies the client, which then calls location.reload() to refresh the page.
  3. Trigger a browser reload via the _notify endpoint

    main

    WasmServe provides a special /_notify endpoint. Making a request to this endpoint will trigger the browser to reload the current page. This is useful for integrating with build scripts or IDE commands to achieve an automatic refresh workflow during development.

    curl localhost:8080/_notify
  4. Tip: Change working directory for WasmServe

    main

    If you need to serve a package located in a different directory without manually changing your shell's location, you can use a subshell with cd:

    (cd /path/to/working/dir; wasmserve github.com/yourname/yourpackage)
  5. Use WasmServe CLI flags

    main

    The wasmserve command accepts the following flags to configure the HTTP server and build process:

    Usage of wasmserve
      -allow-origin string
            Allow specified origin (or * for all origins) to make requests to this server
      -http string
            HTTP bind address to serve (default ":8080")
      -overlay string
            Overwrite source files with a JSON file (see https://pkg.go.dev/cmd/go for more details)
      -tags string
            Build tags
  6. Reference: wasmserve CLI flags

    main

    The following flags are available for the wasmserve command line interface:

    -http string       :8080      HTTP bind address to serve
    -tags string       ""         Build tags
    -allow-origin string ""         Allow specified origin (or * for all origins) to make requests to this server
    -overlay string    ""         Overwrite source files with a JSON file (see https://pkg.go.dev/cmd/go for more details)
  7. Use wasmserve CLI flags

    main

    The wasmserve CLI provides several flags to configure the development server behavior, including network binding, CORS, and Go build settings.

    FlagDefaultDescription
    -http:8080The HTTP bind address to serve on.
    -tags(empty)Go build tags to pass to the go build command.
    -allow-origin(empty)Sets the Access-Control-Allow-Origin header. Use * to allow all origins.
    -overlay(empty)Path to a JSON file used to overwrite source files during the build (compatible with go build -overlay).

    Note: Any positional arguments provided after the flags are treated as the target package or directory to build.

    wasmserve -http :9000 -tags integration -allow-origin * ./my-wasm-app