From 478a089e521de81ca47fc54518bc6a241823d9ae Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Sat, 27 May 2017 22:55:52 +0200 Subject: fix module loading for node under fake web workers --- src/crypto/emscLoader.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'src/crypto/emscLoader.js') diff --git a/src/crypto/emscLoader.js b/src/crypto/emscLoader.js index eebe20c02..190f007f1 100644 --- a/src/crypto/emscLoader.js +++ b/src/crypto/emscLoader.js @@ -31,6 +31,22 @@ * be globally available. Inside node, require is used. */ export function getLib() { + if (typeof require !== "undefined") { + // Make sure that TypeScript doesn't try + // to check the taler-emscripten-lib. + const indirectRequire = require; + const g = global as any; + // unavoidable hack, so that emscripten detects + // the environment as node even though importScripts + // is present. + const savedImportScripts = g.importScripts; + delete g.importScripts; + // Assume that the code is run from the build/ directory. + const lib = indirectRequire("../../../emscripten/taler-emscripten-lib.js"); + g.importScripts = savedImportScripts; + return lib; + } + if (typeof importScripts !== "undefined") { importScripts('/src/emscripten/taler-emscripten-lib.js') if (TalerEmscriptenLib) { @@ -39,14 +55,6 @@ export function getLib() { return TalerEmscriptenLib } - if (typeof require !== "undefined") { - // Make sure that TypeScript doesn't try - // to check the taler-emscripten-lib. - const fn = require; - // Assume that the code is run from the build/ directory. - return fn("../../../emscripten/taler-emscripten-lib.js"); - } - if (typeof window !== "undefined") { if (window.TalerEmscriptenLib) { return TalerEmscriptenLib; -- cgit v1.2.3