go-app: Generated wasm too large
Thank you for your contribution. This looks pretty awesome to work with. I just started playing around with it.
I started with minimal example:
package main
import (
"log"
"net/http"
"github.com/maxence-charriere/go-app/v8/pkg/app"
)
type home struct {
app.Compo
}
func (h *home) Render() app.UI {
return app.H1().Text("Hello World!")
}
func main() {
app.Route("/", &home{})
app.RunWhenOnBrowser()
http.Handle("/", &app.Handler{
Name: "Hello",
Description: "An Hello World! example",
})
if err := http.ListenAndServe(":8000", nil); err != nil {
log.Fatal(err)
}
}
Generated wasm with:
GOARCH=wasm GOOS=js go build -o web/app.wasm
But the generated wasm is a whooping 11.5 MB. Any suggestion to reduce the file size?
Go version: 1.16.4
About this issue
- Original URL
- State: open
- Created 3 years ago
- Comments: 65 (32 by maintainers)
@mar1n3r0 i don’t give up. I got that a lot of you folks want this to happen.
I also made a try for tinygo support. Here is the code: https://github.com/maxence-charriere/go-app/pull/682
I was able to compile but look like there is still some issues to figure out.
note that this is pure hacking and is far from being stable.
Here’s a lighter weight http client for wasm : https://github.com/marwan-at-work/wasm-fetch
Some important links / milestones, to save time the next person trying to read through the whole thread –
We might not be able to achieve the best in one shot, IMHO, even things half done is better than the current situation, as my Original app.wasm size is 14 MB,
gzip
ed size is 3.2M.I already have a wasm module loader in my go-app tests which can load additional wasm modules generated by the original or the TinyGo compiler. This is why I know that 😃
A common pattern is to have a server configurable from environment variables.
Things like api endpoint can be configured and be automatically relayed to a client.
SetEnv has nothing to do with WASM but it has to be implemented in TinyGo in order to be used.
I don’t think that “setenv” has anything to do with the error (besides that I already wondered what “setenv” in WASM may even mean). If you compile with TinyGo you also need to use the TinyGo WASM framework variant. You can not just use this WASM with the WASM of the standard Go compiler.
Thanks, that worked like a charm. Here are the results and the example code based on the hello example: https://github.com/mar1n3r0/hello-wasm
Still the reduction from TinyGo is not on par with what vecty has achieved but at least we are under the 1 MB threshold. For comparison hellovecty.wasm with TinyGo + gzip is 5 times smaller sitting at 97K. An error occurred of course after removing the env variables but it shows what would be possible once SetEnv is included in the next TinyGo release.
main.go
wasmstub.go
server.go
@mar1n3r0 you simply build two times. One for the WASM not containing the server and one for the server itself (which simply uses net/http).
And btw. gRPC uses HTTP/2. I would rather use something like https://github.com/twitchtv/twirp
That is as a wasm server, not an HTTP client library to use in the frontend? The Server is fine in go-app as it is just the server binary where size matters less.
Yes. Still it will be around 3 MB. I will experiment with
tinygo
to see if that helps.