title: What are the best practices to make a plugin CJS, AMD, and script-tag friendly to also be CJS/AMD/script-tag ready?
body: <p>I'm attempting to write a plugin for a library (MomentJS) that is usable almost everywhere.  I'm planning on using it with RequireJS, so it has to be AMD friendly, but I'd also like to go ahead and make it usable by those who load it via script tags in the browser or in Node.</p>
  
  <p>After poking around, I slapped this together:</p>
  
  <pre><code>(function() {
      var hasModule = typeof module !== "undefined"  &amp;&amp; module.exports;
  
      var MY_LIB_DEF = function (moment, global) {
          if(typeof moment == "undefined") {
              throw "Can't find moment";
          }
  
          var MY_LIB = {
              //
              //DEFINE PLUGIN
              //
          };
  
          if(hasModule) {
              module.exports = LIB
          } else if(global) {
              global.LIB = LIB;
          } else {
              return LIB;
          }
      };
  
      if(hasModule) {
          moment = require('moment');
      }
  
      if (typeof define === "function" &amp;&amp; define.amd) {
          define(["moment"], MY_LIB_DEF);
      } else {
          MY_LIB_DEF(moment, this);
      }
  })();
  </code></pre>
  
  <p>The bottom section of MY_LIB_DEF where I determine if I'm exporting for CJS, attatching to window, or returning for AMD seems a bit clunky as does my picking of which way to kick off (CJS and script loading would share the running of the defining function.  But the "global" passed into it will never be used).</p>
  
  <p>While the above works, I'm thinking that this problem <em>has</em> to have been solved already.  I just can't seem to find any examples to follow.</p>
  
  <p>Anyone aware of better practices for this?</p>