diff --git a/src/bridge.js b/src/bridge.js index 6073124..0fc7aed 100644 --- a/src/bridge.js +++ b/src/bridge.js @@ -7,7 +7,6 @@ const maxTime = 3 * 1000; const bridge = g.__go_wasm__; - /** * Wrapper is used by Go to run all Go functions in JS. * Go functions always return an object of the following spec: @@ -25,28 +24,33 @@ function wrapper(goFunc) { return result.result; } } -bridge.__wrapper__ = wrapper function sleep() { - return new Promise(requestAnimationFrame); + return new Promise((res) => { + requestAnimationFrame(() => res()) + setTimeout(() => { + res() + }, 50); + }); } export default function (getBytes) { let proxy; async function init() { + bridge.__wrapper__ = wrapper + const go = new g.Go(); let bytes = await getBytes; let result = await WebAssembly.instantiate(bytes, go.importObject); go.run(result.instance); - bridge.__proxy__ = proxy - setTimeout(() => { - if (bridge.__ready__ !== true) { - console.warn("Golang Wasm Bridge (__go_wasm__.__ready__) still not true after max time"); - } - }, maxTime); } init(); + setTimeout(() => { + if (bridge.__ready__ !== true) { + console.warn("Golang Wasm Bridge (__go_wasm__.__ready__) still not true after max time"); + } + }, maxTime); proxy = new Proxy( @@ -75,5 +79,6 @@ export default function (getBytes) { } ); + bridge.__proxy__ = proxy return proxy; } diff --git a/src/index.js b/src/index.js index d12ff1c..a68600d 100644 --- a/src/index.js +++ b/src/index.js @@ -43,11 +43,14 @@ module.exports = function (source) { } modDir = path.join(modDir, ".."); } - if (!found) { return cb(new Error("Could not find go.mod in any parent directory of " + this.resourcePath)); } + // Having context dependency before compilation means if any file apart from the imported one fails in + // compilation, the updates are still watched. + this.addContextDependency(modDir); + const wasmOrigPath = path.join(process.env.GOROOT, "misc", "wasm", "wasm_exec.js"); const wasmSavePath = path.join(__dirname, 'wasm_exec.js'); const errorPaths = ["\t" + wasmOrigPath, "\t" + wasmSavePath]; @@ -73,7 +76,6 @@ module.exports = function (source) { const emitPath = path.basename(outFile); this.emitFile(emitPath, contents); - this.addContextDependency(modDir); cb(null, `require('!${wasmSavePath}');