From b5380cd04209771c4ba01d4f666966c3adc990be Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Tue, 12 Sep 2017 23:57:43 -0700 Subject: [PATCH] fix(preprocessor): support electron with node context --- preprocessor.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/preprocessor.js b/preprocessor.js index 5d7c3c7..0b9d5c6 100644 --- a/preprocessor.js +++ b/preprocessor.js @@ -10,8 +10,25 @@ // we are building with MODULARIZE option, // which pre-generates module object in preprocessor context - simply use it + +// Electron's renderer process is special environment that emscripten can't detect correctly if module loader +// would like to use node.js context (overriding context via ENVIRONMENT==='NODE' but doesn't provide endpoint via locateFile override) +// cause emscripten's getBinaryPromise simply checks existence of `fetch` api. In here, do global object patching while init module +// if given config is set to use NODE but process is actually Electron. +var originalFetch = null; +if (Module["ENVIRONMENT"] === "NODE" && !!window && !!window.process && !!window.require && !!window.fetch) { + originalFetch = window.fetch; + window.fetch = null; +} + // expose filesystem api selectively Module["preRun"] = function () { + //restore fetch context once wasm binary is loaded + if (!!originalFetch) { + window.fetch = originalFetch; + originalFetch = null; + } + Module.FS = { filesystems: FS.filesystems, stat: FS.stat,