Note that there are some explanatory texts on larger screens.

plurals
  1. POPackaging a js library for AMD, CommonJS and more
    primarykey
    data
    text
    <p>When packaging a javascript library that needs to be used with AMD, CommonJS or as a global, it is common to use a defition such as the following:</p> <pre><code>(function(name, definition) { if (typeof module != 'undefined') module.exports = definition(); else if (typeof define == 'function' &amp;&amp; typeof define.amd == 'object') define(definition); else this[name] = definition(); }('mod', function() { //Here goes the code you would normally have inside define() or add to module.exports return MyModule }; })); </code></pre> <p>This works well assuming your library consists of only a single file, with no external dependencies. I am not sure what to do when publishing a library that consists of various interdependent modules, possibly with external dependencies.</p> <p>Even if I only want to support AMD (AMD + Bower is my main use case, i.e. the way I will use the library myself), I am not sure which is the way to go. Assume my library publishes modules <code>foo</code> and <code>bar</code>, with <code>bar</code> depending on <code>foo</code>, both having <code>external</code> as a dependency. I could try to do something like</p> <pre><code>// mylib/foo.js define(['external'], function(external) { // whatever }); // mylib/bar.js define(['./foo', 'external'], function(foo, external) { // whatever }); </code></pre> <p>Still, I am not guaranteed that a client of mine would resolve 'external' correctly. For this to work, the client would need to explicitly add the path for <code>external</code> in its RequireJS definition. This is still doable, if there is just one dependency, but I would prefer to shield the client from having to know that the library uses external. This should be declared in my <code>bower.json</code> and <code>package.json</code> files, but the client should not mess with transitive dependencies manually. As soon as a library has multiple dependencies, each one having other dependencies, things can get hairy.</p> <blockquote> <p>What is the correct way to handle dependencies in a library, especially considering the possibility of supporting multiple module standards?</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    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