npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fiedka/golang-wasm-async-loader

v5.0.0

Published

Golang Wasm loader for webpack that generates an async pattern for working with Go's WebAssembly output

Downloads

10

Readme

Build Status npm node

npm install --save-dev @fiedka/golang-wasm-async-loader

This is a loader for webpack that is used for generating WebAssembly (aka WASM) bundles from Go.

The JavaScript bridge that is then generated for webpack will expose the WebAssembly functions as a Promise for interacting with.

webpack config

module.exports = {
    ...
    module: {
        rules: [
            {
                test: /\.go/,
                use: ['golang-wasm-async-loader']
            }
        ]
    },
    node: {
        fs: 'empty'
    }
};

Using in your code

You import your Go code just like any other JavaScript module you might be working with. The webpack loader will export a default export that has the functions you registered in Go on it. Unfortunately it currently doesn't provide autocomplete of those function names as they are runtime defined.

import wasm from './main.go'

async function init() {
  const result = await wasm.add(1, 2);
  console.log(result);

  const someValue = await wasm.someValue();
  console.log(someValue);
}

Here's the main.go file:

package main

import (
  "strconv"
  "syscall/js"
  "github.com/fiedka/webpack-golang-wasm-async-loader/gobridge"
)

func add(i []js.Value) (interface{},error) {
	ret := 0

	for _, item := range i {
		val, _ := strconv.Atoi(item.String())
		ret += val
	}

	return ret, nil
}

func main() {
	c := make(chan struct{}, 0)

	gobridge.RegisterCallback("add", add)
	gobridge.RegisterValue("someValue", "Hello World")

	<-c
}

How does it work?

As part of this repository a Go package has been created to improve the interop between the Go WASM runtime and work with the async pattern the loader defines.

To do this a function is exported from the package called RegisterCallback which takes two arguments:

  • A string representing the name to register it as in JavaScript (and what you'll call it using)
  • The func to register as a callback
    • The func must has a signature of (args js.Value) (interface{}, error) so you can raise an error if you need

If you want to register a static value that's been created from Go to be available in JavaScript you can do that with RegisterValue, which takes a name and a value. Values are converted to functions that return a Promise so they can be treated asynchronously like function invocations.

In JavaScript a global object is registered as __gobridge__ which the registrations happen against.

Examples

Examples are provided for a CLI using NodeJS and for web using either React or Svelte. These are in the examples directory, each with its own development and build environment.

To make an example stand-alone, copy of the corresponding example to a new directory (outside the plugin directory) and then modify the example's webpack.config.js so that the .go loader refers to this plugin. Then add it to the example's dependencies as follows.

For use with Go 1.13, use happybeing's v1.0.6 of the plugin:

npm add --save-dev [email protected]`

For use with Go 1.15, use happybeing's v1.1.0 of the plugin:

npm add --save-dev [email protected]`

For use with Go 1.16, use happybeing's v3.0.2 of the plugin:

npm add --save-dev @fiedka/[email protected]`

Environment

To build your project (and the examples) you will need the GOROOT environment variable set. So for example if using the bash shell:

node example:

GOROOT=`go env GOROOT` npm run predemo
npm run demo

web example (with hot reloading):

GOROOT=`go env GOROOT` npm run build
GOROOT=`go env GOROOT` npm run start

Licence

MIT

Credit

Aaron Powell (updates up to Go 1.15 by Mark Hughes)