Coder Social home page Coder Social logo

wasmgo's Introduction

wasmgo

The wasmgo command compiles Go to WASM, and serves the binary locally or deploys to the jsgo.io CDN.

Install

go get -u github.com/dave/wasmgo

Serve command

wasmgo serve [flags] [package]

Serves the WASM with a local web server (default to port 8080). Refresh the browser to recompile.

Deploy command

wasmgo deploy [flags] [package]

Deploys the WASM to the jsgo.io CDN.

Global flags

-b, --build string     Build tags to pass to the go build command.
-c, --command string   Name of the go command. (default "go")
-f, --flags string     Flags to pass to the go build command.
-h, --help             help for wasmgo
-i, --index string     Specify the index page template. Variables: Script, Loader, Binary. (default "index.wasmgo.html")
-o, --open             Open the page in a browser. (default true)
-v, --verbose          Show detailed status messages.

Deploy flags

-j, --json              Return all template variables as a json blob from the deploy command.
-t, --template string   Template defining the output returned by the deploy command. Variables: Page, Script, Loader, Binary. (default "{{ .Page }}")

Serve flags

-p, --port int   Server port. (default 8080)

Package

Omit the package argument to use the code in the current directory.

Examples

Here's a simple hello world:

wasmgo serve github.com/dave/wasmgo/helloworld

The page (http://localhost:8080/) opens in a browser.

Here's an amazing 2048 clone from hajimehoshi:

go get -u github.com/hajimehoshi/ebiten/examples/2048/...
wasmgo deploy -b=example github.com/hajimehoshi/ebiten/examples/2048

The deployed page opens in a browser.

Index

You may specify a custom index page by including index.wasmgo.html in your project or by using the index command line flag.

Your index page should look something like this:

<html>
<head><meta charset="utf-8"></head>
<body>
	<script src="{{ .Script }}"></script>
	<script src="{{ .Loader }}"></script>
</body>
</html>

Template variables

The index page template and the -t flag are both Go templates with several variables available:

  • Page
    The URL of the page on jsgo.io (deploy command output only).

  • Script
    To load and execute a WASM binary, a some JS bootstap code is required. The wasmgo command uses a minified version of the example in the official Go repo. The URL of this script is the Script template variable.

  • Loader
    The loader JS is a simple script that loads and executes the WASM binary. It's based on the example in the official Go repo, but simplified to execute the program immediately instead. The URL of this script is the Loader template variable.

  • Binary
    The URL of the WASM binary file.

Static files

Unfortunately wasmgo does not host your static files. I recommend using rawgit.com to serve static files.

Package splitting

The wasmgo deploy can't yet split the binary output by Go package, can you help?

wasmgo's People

Contributors

dave avatar norunners avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wasmgo's Issues

Problems with Go 1.11rc1 [SOLVED]

Golang 1.11rc1
macOS 10.13.6

Hi Dave, I've run go get -u github.com/dave/wasmgo on Go 1.11rc1 and as a result of Modules it had downloaded it to go/pkg/mod/github.com/dave/[email protected].

I switched into the helloworld folder where there is a main.go.

To save me having to type the full path, since I was there already, I tweaked the instructions to wasmgo deploy helloworld. Not surprisingly, since there is no source file called helloworld, this didn't work.

What do I have to do to get this to work?

Cheers,
Carl

Package splitting

The jsgo compiler uses GopherJS to compile Go to Javascript. It splits the output by Go package, meaning each package is individually cached by the browser. This has benefits:

  • Several projects that use the fmt package will share the same fmt.js script.
  • Deploying a small change to a project will result in clients only re-downloading the javascript for packages with changes.

Unfortunately the Go compiler doesn't support the same package splitting, so this isn't possible in the WASM version.

This ticket is to track any possible work-arounds.

Two possibilities have been researched:

  1. Fork the Go compiler and modify it to emit the WASM binary for each package separately.
  2. Split the WASM binary after it's been created by the Go compiler.

I worked on forking the Go compiler for a couple of weeks, but as expected there were huge issues. I'm not planning to continue work on this. See here for progress.

@dennwc worked on the WASM binary splitter, but came across problems. However, this is the preferred option for anyone who wants to help. See here for progress.

gzip compression on serving

If you are interested, made a quick and dirty fork to add support for gzip compression on wasmgo serve without adding any external deps. Just straight go code.

Is basically adding this file here :

https://github.com/steveoc64/wasmgo/blob/master/cmd/server/compressor.go

and changing 1 line in cmd/server/server.go to :

s := &http.Server{Addr: fmt.Sprintf(":%d", cfg.Port), Handler: compressor(svr.ServeHTTP)}

Id put in a PR, but it gets messy because of all the re-named include statements :(

Gob error running wasmgo deploy

I'm getting the following error when running wasmgo deploy <package>:

Error: gob: name not registered for interface: "github.com/dave/jsgo/server/servermsg.Queueing"

I know this used to work, so not quite sure what's wrong.

-f doesn't work to specify build tags

$ wasmgo deploy -f="-tags=example" github.com/hajimehoshi/ebiten/examples/2048
Error: exit status 1: can't load package: package github.com/hajimehoshi/ebiten/examples/2048: build constraints exclude all Go files in /Users/hajimehoshi/go/src/github.com/hajimehoshi/ebiten/examples/2048

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.