{"_id":"@danielknightgaynor/golang-wasm-async-loader","_rev":"1-850033467444c7d51d892796de99cd7c","name":"@danielknightgaynor/golang-wasm-async-loader","dist-tags":{"latest":"4.0.0"},"versions":{"4.0.0":{"name":"@danielknightgaynor/golang-wasm-async-loader","version":"4.0.0","description":"Golang Wasm loader for webpack that generates an async pattern for working with Go's WebAssembly output","main":"dist/index.js","scripts":{"build":"tsc","build:watch":"tsc --watch"},"keywords":["go","golang","wasm","webassembly","webpack"],"author":{"name":"Aaron Powell"},"license":"MIT","devDependencies":{"@types/node":"^16.7.5","@types/webpack":"^5.28.0","typescript":"^4.4.2"},"peerDependencies":{"webpack":"^5.51.0"},"engines":{"node":">= 8.0.0"},"types":"./dist/index.d.ts","gitHead":"7abdf1606cca7189b7c96243ed9c379d824c3910","_id":"@danielknightgaynor/golang-wasm-async-loader@4.0.0","_nodeVersion":"16.14.0","_npmVersion":"8.5.1","dist":{"integrity":"sha512-ctRNPX/jLBzDzvW6RLgFqS2vF32DPu7ioo+Bw1F7ql7oOkTh6GVOe4OJxd0eFN9u99m2RnffKFgCEHvBCut7xw==","shasum":"2b5914480690096708423b2289b4a768ae7b73fe","tarball":"https://registry.npmjs.org/@danielknightgaynor/golang-wasm-async-loader/-/golang-wasm-async-loader-4.0.0.tgz","fileCount":13,"unpackedSize":34367,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiO0SpACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmroFw//RXyxlpbiNo5fJ1wUNis1XGvncp7m9LKgtfTwKRlCn1b68yk6\r\nacWm9IIvncr2vPtM0bKQvPamuZtNTGMj1Neu9PkSgDAGvyLZi3gjt9EMTyvk\r\n7XTempnPcRnAtnoC2XJC/Z1i/n5OqJOMvx2CzS1YaMqMnf94RV993UlWX658\r\nqYjtLPBRf2OTyoVRWVE+SuVnvFzqZvt3qW8kNpRf9Hp+pUWjy8xwUb0zfu6n\r\n2EvrkjZCrUEDjx/vdi8mLWucZ4SgePie1sPs2FKuH12W8Kt9q3qUg2mhfmEk\r\nO3RX2iLbNjwZjJfQw/ffShJQvPHrshyq8Unc3DlNBhmD0Y2QPapwwC2KWKOU\r\nyFD8I52Jhfoaqe7NsQWgwOAJP25dgmioO0uSVtGLC1zpHMUnxW9sY2kqQLC7\r\nkCwf+JsAi1rGArKWxOnT4OskLgv+scMJG8BCdeLvYMMTneJqNKzSJPiJU0RF\r\nsQcqIFVW5CTNrjTgIoTyvwSwlpr3FbQt7e8OG1/TMnIpfcQs6oUqC2SA4+4l\r\nA1++hphG4s1P/ClYJV43okou0ldcn7JQC8CSBAvwUoYCjlXqNyPL9UkwrIAN\r\nNwZZh4AZ4Qz/mXcFPShAnO9ymhMJ717OzayncNMzfJVM6oE6qzig+muYT/X6\r\nUxuELa3Gq67agOYlwTuz+xKAShhEQxSibfs=\r\n=NT+9\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCxGw4zpbWmCT/H6buPBPjzxwymvrWUpkdvKovl47MksQIhAKrurFaGcRI+2WicreiLwoRNd0av/m2czIgyU1lEFItM"}]},"_npmUser":{"name":"danielknightgaynor","email":"dan.kg@dotfive.co.uk"},"directories":{},"maintainers":[{"name":"danielknightgaynor","email":"dan.kg@dotfive.co.uk"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/golang-wasm-async-loader_4.0.0_1648051368825_0.11876459953730478"},"_hasShrinkwrap":false}},"time":{"created":"2022-03-23T16:02:48.758Z","4.0.0":"2022-03-23T16:02:49.060Z","modified":"2022-04-05T02:41:07.174Z"},"maintainers":[{"name":"danielknightgaynor","email":"dan.kg@dotfive.co.uk"}],"description":"Golang Wasm loader for webpack that generates an async pattern for working with Go's WebAssembly output","keywords":["go","golang","wasm","webassembly","webpack"],"author":{"name":"Aaron Powell"},"license":"MIT","readme":"[![Build Status][build]][build-url]\n[![npm][npm]][npm-url]\n[![node][node]][node-url]\n\n<div align=\"center\">\n  <a href=\"https://github.com/webpack/webpack\">\n    <img width=\"200\" height=\"200\"\n      src=\"https://webpack.js.org/assets/icon-square-big.svg\">\n  </a>\n  <h1>Golang WebAssembly Async Loader</h1>\n  <p>Generates a WASM package from Golang and provides an async interface for working with it</p>\n  <p>@fiedka/golang-wasm-async-loader is a fork of golang-wasm-async-loader updated for Golang up to v1.17</p>\n</div>\n\n<h2 align=\"center\">Install</h2>\n\n```bash\nnpm install --save-dev @fiedka/golang-wasm-async-loader\n```\n\nThis is a loader for [webpack](https://webpack.js.org/) that is used for generating [WebAssembly](https://webassembly.org/) (aka WASM) bundles from [Go](https://golang.org).\n\nThe JavaScript bridge that is then generated for webpack will expose the WebAssembly functions as a Promise for interacting with.\n\n## webpack config\n\n```js\nmodule.exports = {\n    ...\n    module: {\n        rules: [\n            {\n                test: /\\.go/,\n                use: ['golang-wasm-async-loader']\n            }\n        ]\n    },\n    node: {\n        fs: 'empty'\n    }\n};\n```\n\n# Using in your code\n\nYou 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.\n\n```js\nimport wasm from './main.go'\n\nasync function init() {\n  const result = await wasm.add(1, 2);\n  console.log(result);\n\n  const someValue = await wasm.someValue();\n  console.log(someValue);\n}\n```\n\nHere's the `main.go` file:\n\n```go\npackage main\n\nimport (\n  \"strconv\"\n  \"syscall/js\"\n  \"github.com/fiedka/webpack-golang-wasm-async-loader/gobridge\"\n)\n\nfunc add(i []js.Value) (interface{},error) {\n\tret := 0\n\n\tfor _, item := range i {\n\t\tval, _ := strconv.Atoi(item.String())\n\t\tret += val\n\t}\n\n\treturn ret, nil\n}\n\nfunc main() {\n\tc := make(chan struct{}, 0)\n\n\tgobridge.RegisterCallback(\"add\", add)\n\tgobridge.RegisterValue(\"someValue\", \"Hello World\")\n\n\t<-c\n}\n```\n\n## How does it work?\n\nAs 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.\n\nTo do this a function is exported from the package called `RegisterCallback` which takes two arguments:\n\n* A `string` representing the name to register it as in JavaScript (and what you'll call it using)\n* The `func` to register as a callback\n  * The `func` must has a signature of `(args js.Value) (interface{}, error)` so you can raise an error if you need\n\nIf 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.\n\nIn JavaScript a global object is registered as `__gobridge__` which the registrations happen against.\n\n## Examples\n\nExamples are provided for a CLI using NodeJS and for web using either React or Svelte. These are in the [`examples`](https://github.com/fiedka/webpack-golang-wasm-async-loader/tree/main/examples) directory, each with its own development and build environment.\n\nTo 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.\n\nFor use with **Go 1.13**, use happybeing's v1.0.6 of the plugin:\n```bash\nnpm add --save-dev golang-wasm-async-loader2@1.0.6`\n```\n\nFor use with **Go 1.15**, use happybeing's v1.1.0 of the plugin:\n```bash\nnpm add --save-dev golang-wasm-async-loader2@1.1.0`\n```\n\nFor use with **Go 1.16**, use happybeing's v3.0.2 of the plugin:\n```bash\nnpm add --save-dev @fiedka/golang-wasm-async-loader@3.0.2`\n```\n\n## Environment\n\nTo build your project (and the examples) you will need the GOROOT environment variable set. So for example if using the bash shell:\n\nnode example:\n```bash\nGOROOT=`go env GOROOT` npm run predemo\nnpm run demo\n```\nweb example (with hot reloading):\n```bash\nGOROOT=`go env GOROOT` npm run build\nGOROOT=`go env GOROOT` npm run start\n```\n# Licence\n\nMIT\n\n# Credit\n\nAaron Powell (updates up to Go 1.15 by Mark Hughes)\n\n\n[npm]: https://img.shields.io/npm/v/@fiedka/golang-wasm-async-loader.svg\n[npm-url]: https://npmjs.com/package/@fiedka/golang-wasm-async-loader\n\n[node]: https://img.shields.io/node/v/@fiedka/golang-wasm-async-loader.svg\n[node-url]: https://nodejs.org\n","readmeFilename":"README.md"}