Webpack loader that turns Go code into JS modules, but with WebAssembly in the background.
Install witn npm:
npm install --save-dev go-module-loaderor with yarn:
yarn add -D go-module-loaderNote:
goalso have to be installed. And either havegoinPATH, or haveGOROOTset.
webpack.config.js
module.exports = {
// ...
module: {
rules: [
{
test: /\.go$/,
use: 'go-module-loader',
},
],
},
};src/mymath.go
package main
import (
"github.com/opkna/jsbridge"
)
func main() {
jsbridge.ExportFunc("add", add, false)
select {}
}
func add(args []interface{}) (interface{}, error) {
a := args[0].(float64)
b := args[1].(float64)
return a + b, nil
}src/index.js
import mymath from './mymath.go';
mymath.instantiate().then((inst) => {
console.log(inst.add(1, 2));
});To be able to test this loader you need to use a development server, otherwise
CORSwill block the fetch for thewasmfile. Also the dev server need to be able to handle theapplication/wasmMIME type. webpack-dev-server work well on both counts.