Skip to content

Instantly share code, notes, and snippets.

@jquense
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jquense/8689961 to your computer and use it in GitHub Desktop.
Save jquense/8689961 to your computer and use it in GitHub Desktop.
Mock sm2 as a CommonJs module
var SoundMan = require('SoundManager').SoundManager
, soundMan = require('SoundManager').soundManager
//profit!
(function(window, _undefined){
if ( window === _undefined )
throw new Error('SoundManager requries a window object!');
// here be the lib code
// use can attach what you must to the window object here!
if ( typeof module === "object" && module && typeof module.exports === "object" ) {
//CommonJs
// the "strict" way is to never expose as a global object if possible, so no sm2 on the window
// you could still export it via module.exports and also add to window if you want though. no added benefit
// unless your app already expects both, which should not be the case
module.exports.SoundManager = SoundManager;
module.exports.soundManager = soundManager;
} else if ( typeof define === "function" && define.amd ) {
// AMD - requireJs
define( "SoundManager", [], function () {
return {
SoundManager: SoundManager
soundManager: soundManager
};
});
} else {
//Normal Browser
window.SoundManager = SoundManager;
window.soundManager = soundManager;
}
})(window);
//--------------------------------
// my file.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment