Search code examples
javascriptelectronbitcoin

How to include all the content of a module in a different one (in Electron JS)


I am trying to write a backend for some bitcoin framework in javascript. I am fairly new to the language. But I have done a couple of projects with it.

I am looking at https://coinb.in/ atm. If you have a look at the source @ index file, they include scripts like so:

<script type="text/javascript" src="js/qrcode.js"></script>
<script type="text/javascript" src="js/qcode-decoder.min.js"></script>
<script type="text/javascript" src="js/jsbn.js"></script>
<script type="text/javascript" src="js/ellipticcurve.js"></script>

Elliptic curve is an anonymous function that attaches all of its methods to the window object in the browser. My project is in Electron JS so I simulated this with global.EllipticCurve. However, this is unimportant. What is important is that jsbn.js, for example, is not an anonymous function and it is too large to refactor. Functions from jsbn.js are visible in ellipticcurve.js because it was imported before it. I am wondering how I can achieve this effect in Electron JS, I cannot just do require('./jsbn.js'), since that would give me undefined. Is there a way to module.exports everything? (Including all of the contents of jsbn.js in ellipticcurve.js also works, and that is how I manged to make it work, but this makes the code extremely cluttered)


Solution

  • Found a way to do this by inspecting the jsbn oldest version. The whole code is wrapped in anonymous function with the following ending:

    if (typeof exports !== 'undefined') {
        exports = module.exports = BigInteger;
    } else {
       this.BigInteger = BigInteger;
    }
    }).call(this);
    

    I do not understand the typeof exports !== 'undefined' though.