Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found a solution, although it is not what is first had in mind, with different build environments.</p> <p>In my case, I had a pretty small library which were using the Node's <code>http</code>, <code>https</code>, <code>querystring</code> and <code>url</code> packages. I did not want to use something like <a href="http://browserify.org/" rel="nofollow">Browserify</a>, because it seemed not appropriate to bundle all these packages for a small api-library. Instead I replaced the <code>http</code> and <code>https</code> functionality with the <a href="https://npmjs.org/package/xmlhttprequest" rel="nofollow">XMLHttpRequest</a> package. The small functionalities provided by <code>querystring</code> and <code>url</code> could easily be rewritten.</p> <p>In my library I check, run-time, if the <code>window.XMLHttpRequest</code> object is available. If so, use that (native) object. Otherwise, it uses the one provided by the package. As such:</p> <pre class="lang-cs prettyprint-override"><code>_getRequestObject: () -&gt; if window? and window.XMLHttpRequest? return new window.XMLHttpRequest() if window? and window.ActiveXObject? try request = new ActiveXObject('Msxml2.XMLHTTP') catch e try request = new ActiveXObject('Microsoft.XMLHTTP') catch e XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest return new XMLHttpRequest() </code></pre> <p>An other problem was that <code>exports</code> is not defined in the browser. There are packages to simulate this behaviour, but again, it did not want to bloat the library. So I check, again runtime, if the <code>module</code> variable is set. If so, the object I have defined is set to that, otherwise it is set to the <code>window</code> object:</p> <pre class="lang-cs prettyprint-override"><code>if typeof module is 'undefined' window['My-module'] = My_module_object else module.exports = exports = My_module_object </code></pre> <p>This all works for me, because I have no real required dependencies of Node.js which are not present in the browser environment. I am afraid that with larger projects a solution like <a href="http://browserify.org/" rel="nofollow">Browserify</a> is really needed, but I am still curious if there are other solutions, like creating different build-environments when packaging the library for Node.js or when for (e.g.) Bower.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload