From 74e94fe8ac7b2c2a66b8118b5eaa3a81e5f9296a Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Mon, 24 Jan 2022 14:53:10 -0700 Subject: [PATCH] fix: Handle JS global in non-node environments (#125) --- js/dune | 2 +- js/postlude.js | 4 ++-- js/prelude.js | 11 +++++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 js/prelude.js diff --git a/js/dune b/js/dune index f4f9d5fd..b11add45 100644 --- a/js/dune +++ b/js/dune @@ -10,4 +10,4 @@ (names stubs)) (js_of_ocaml (flags --no-sourcemap) - (javascript_files binaryen.es5.js postlude.js))) + (javascript_files prelude.js binaryen.es5.js postlude.js))) diff --git a/js/postlude.js b/js/postlude.js index c44ccc5f..23fc50fc 100644 --- a/js/postlude.js +++ b/js/postlude.js @@ -1,2 +1,2 @@ -// Attach binaryen to our global -global.binaryen = module.exports; +// Attach binaryen to globalThis because JSOO wraps in an iife +globalThis.binaryen = module.exports; diff --git a/js/prelude.js b/js/prelude.js new file mode 100644 index 00000000..bf2be20c --- /dev/null +++ b/js/prelude.js @@ -0,0 +1,11 @@ +if (typeof module === 'undefined') { + module = {}; +} + +if (typeof exports === 'undefined') { + exports = {}; +} + +if (typeof module.exports === 'undefined') { + module.exports = exports; +}