Skip to content

Instantly share code, notes, and snippets.

@tkellen
Created March 26, 2012 15:54
Show Gist options
  • Save tkellen/2206123 to your computer and use it in GitHub Desktop.
Save tkellen/2206123 to your computer and use it in GitHub Desktop.
requirejs library for node and browser with and without amd
(function(global, define) {
var loader = false;
// detect if there is an amd loader on the global namespace
if(typeof global.define === 'function') {
loader=true;
}
{{ALMOND_SHIMMED_LIBRARY}}
if(typeof module !== 'undefined' && module.exports) {
// export library for node
module.exports = require('library');
} else if(loader === true) {
// define library for global amd loader that is already present on page
global.define('library',[],function(){return require('library');});
} else {
// define library on global namespace for inline script loading
global['library'] = require('library');
}
}(this));
@jrburke
Copy link

jrburke commented Mar 26, 2012

This makes this an anonymous module that can be found by tools like the r.js optimizer to insert the right name.

(function(global) {
  var globalDefine = global.define;

  {{ALMOND_SHIMMED_LIBRARY}}

  //Grab the library exports for use in the registration paths below.
  var library = require('library');

  if(typeof module !== 'undefined' && module.exports) {
     // export library for node
     module.exports = library;
  } else if(globalDefine) {
    // define library for global amd loader that is already present on page
    (function (define) {
      define(function () { return library; });
    }(globalDefine));
  } else {
    // define library on global namespace for inline script loading
    global['library'] = library;
  }
}(this));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment