happybeing/webpack-golang-wasm-async-loader

Support Go versions other than v1.13

happybeing opened this issue ยท 6 comments

v1.0.6 of this plugin only supports Go v1.13 due to the need to fix a problem in the distributed with this and current Go versions up to at least v1.15 (released November 2020).

Each Go version provides its own version of $GOROOT/misc/wasm/wasm_exec.js and these need a slight modification to work in certain environments (including with my p2p-git-portal-poc project). This appears to be fixed in Go commit golang/go@758ac37#diff-50c9536f867ce96d4731b5239b25952e38ad65800cebb9f76fbc880d34e62e94, but as of Go 1.15.5 this change has not been included in a release of the compiler.

The Fix

In order to work with Go v1.13 I had to modify the code which sets a property global.fs and tests whether or not to use the fs stub functions provided in wasm_exec.js. The error I was encountering was that these functions were NOT being used, leading to the following error on loading my Go/wasm app, when using the plugin:

Uncaught (in promise) TypeError: fs.writeSync is not a function
    wasmWrite wasm_exec.js:248
    run wasm_exec.js:475
    init gobridge.js:17
    default_1 gobridge.js:20
    go main.go:2
    Webpack 9

The diff between Go v1.13's wasm_exec.js and the one provided in this plugin is:

< 		global.fs = require("fs");
---
> 		// global.fs = require("fs");
> 		const fs = require("fs");
> 		if (Object.keys(fs) !== 0) {
> 			global.fs = fs;
> 		}
33c37
< 	if (!global.fs) {
---
> 		if (!global.fs || Object.keys(global.fs) === 0 || Object.keys(global.fs).length === 0) {

An equivalent fix appears to have been applied in this commit golang/go@758ac37#diff-50c9536f867ce96d4731b5239b25952e38ad65800cebb9f76fbc880d34e62e94 (Aug 25, 2020) but is not yet included in a release. Go 1.15 was release in August 2020. 1.16 will be six months after in February 2021.

The relevant commit is currently only in master, so presumably this change will appear in Go 1.16 (or possibly an point release of 1.15). The following checks which branches include this change:

git branch --contains 758ac371ab930734053ed226ac62681e62ab8eea

Once the fix is applied to the file distributed with Go, this plugin could just copy the compiler's file rather than use its own. This would be accomplished by the following edit of src/index.ts:
Replace:
join(__dirname, "..", "lib", "wasm_exec.js"),
with:
join(process.env.GOROOT, "/misc/wasm/wasm_exec.js"),

Plan (Feb 2021)

When Go v1.16 is released, test the plugin with the new unmodified wasm_exec.js, and if it works apply the above change so that:

  • Plugin ^1.2.0 - supports Go v1.16 and later
  • Plugin 1.0.6 - supports Go v.1.13 only

Hi, I'm trying to get this to work again for https://github.com/orangecms/utk-web to build an in-browser back-end with tools written in Go. :)

I had picked up your work already some months ago, but wasn't exactly sure what to do, and now got back to it.
See https://github.com/orangecms/webpack-golang-wasm-async-loader for what I'm trying etc..

For more context: The React example is the interesting part for me. I will need an ArrayBuffer to pass a file from a file picker, have it analyzed, and retrieve back a large string (serialized JSON which can be parsed again).

I got it to work in a PoC! Thank you so much! \o/

See https://hostile.education/fmap :-)

I'll see that I clean things up a bit and file some PRs. :)

Great stuff!

I'm no longer working with Go or doing much code at all now so of you'd be interested in maintaining this I'd be very happy.

Sure, I'll check if we can get this to live under an org at npm, possibly owned by the Golang team. So people could npm install -D @go/webpack-golang-wasm-async-loader. Or otherwise under @webpack maybe. I'll get back to you so that you can point to that. :)

That'd be great Daniel, thanks.

Alright, I've gone a little back and forth and come to the conclusion that I'd just host the fork under a new org that I just created, which is where I'm using the loader now (evolved from my previous PoC app). Please welcome: https://github.com/fiedka/webpack-golang-wasm-async-loader / https://www.npmjs.com/package/@fiedka/golang-wasm-async-loader

I have upgraded the tooling to build with the latest TypeScript compiler and support Webpack 5. It works! \o/

The README, especially some import paths, references etc need some overhaul. So far I have migrated the React example.

Thank you again for your previous work! ๐Ÿ™‡