Skip to content

swissChili/go-wasm

Repository files navigation

go-wasm

A Go build utility for WebAssembly projects

Installation

$ go install github.com/swissChili/go-wasm
# make sure $GOPATH/bin is in your $PATH!
$ go-wasm -h

Building

$ make

Usage

I wrote a small post on using this on dev.to. I suggest you take a look at it if you'd like to see a slightly more in-depth guide at using this software.

IMPORTANT: Please add a script tag with source index.js in your index.html file! This is used to load your web assembly

go-wasm reads from a wasm.yml file in your current directory. Here is an example wasm.yml file:

# directory to use as static dir for HTML
static: static
# Go files to build
source:
  - "somefile.go"
  - "someotherfile.go"
# Target to compile to
target: wasm
# Directory to read raw, uncompiled CSS from
cssdir: sass
# compiler 
csscomp:
  # OUTPUT and INPUT are placeholders for the args to use
  sass: sass INPUT OUTPUT
  scss: sass INPUT OUTPUT
  less: lessc INPUT -o OUTPUT
# Output dir for compiled, static HTML, CSS, JS and wasm
output: target

It will empty the output directory, compile your Go files to wasm, copy your static files over (anything in the static dir will be copied, use this for images and such), compile your sass / scss / less, and finally generate a JavaScript file that mounts your wasm program to the page.

Example

The example directory contains a small program that can be compiled for web assembly. The wasm.yml in the root contains the instructions to compile it. Feel free to look through the static/index.html and sass/main.sass files, and the outputed files to see what changes compiling makes.

Server

Did I mention this has a nifty little server to serve up your fresh-off-the-compiler wasm project? Well it does! No need to configure anything, it will automatically serve from the directory you built to, and defaults to port 8080. Use the -port flag to use a different port

eg: go-wasm -port 9090