The playground is a place where users can explore GopherJS without needing to install anything, or to share code with others.
To build the entire playground environment,
run go generate.
This assumes that your go matches the current version of go that gopherjs supports.
If you have a different version of go then install the other versions as
described in Managing Go installations.
For example, to build for GopherJS 1.20.0 for Go 1.20.14
run go1.20.14 generate.
Generate will:
- Install your local version of GopherJS compiler
- Build the playground
- Build the web worker compiler
- Create precompiled standard library packages in the
pkgdirectory
To manually build the playground:
- See GopherJS documentation about building GopherJS
- Build the playgound with
gopherjs build ./playground.go - Build the web worker compiler with
gopherjs build ./internal/cmd/compile/compile.go - Create the precompiled standard library packages with
go run ./internal/cmd/precompile. Use the samegoasgopherjsas described above
Working on the playground application itself is made easier by using the
gopherjs serve command serve the playground locally.
Then open http://localhost:8080?debug=true&local=true.
The debug query will enable debugging for react and verbose output.
If running locally or from a fork, trying to access the remote store will
fail with a a CORS error. Use the local query to make the snippet store
(used when clicking "Share" or loading with a shared url) use local cache only.
There are a few ways to check changes. When working on the playground itself
it may be simpliest to run gopherjs build ./playground.go && gopherjs serve
after you have made changes so that the changes will be built and served.
When running code in the playground, the JS that is being run can be seen
by checking the page source files and finding the webworker that is running
the code. The webworker should have a GUID name, e.g. fb29b3a9-d5b0-478d-aaf7-85cb60ddbd6d,
and the JS compiled for that code will have the same name (usually without .js).
To upgrade to the newest version, run
VERSION="$(go list -m -versions -f "{{ range .Versions }}{{ println . }}{{ end }}" github.com/gopherjs/gopherjs | tail -n 1)"
echo "$VERSION"
go get -v "github.com/gopherjs/gopherjs@$VERSION"
go mod tidythen rebuild the playground with the correct version of go.
When committing the update, make sure that compile.js at minimum has been
updated to use the new version of GopherJS so that we can build in that version.
The snippets are prewritten example programs such as Hello and Donut.
To add new snippets:
- Write the code you would like to add
- Test it in the playground and check the console to make sure it does what you expected it to do
- Create file in the snippets folder for that code and change
file extension to
.go.txt - In
snippets.goadd the new file as an embedded string and add that string to thepredefinedmap.