-
-
Save flevour/4134515 to your computer and use it in GitHub Desktop.
Universal JavaScript Module, supports AMD (RequireJS), Node.js, and the browser.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jslint plusplus: true */ | |
/*global define: false, require: false, module: false, exports: false */ | |
(function (root, name, deps, factory) { | |
"use strict"; | |
if (typeof exports === 'object') { | |
// Node. Does not work with strict CommonJS, but | |
// only CommonJS-like enviroments that support module.exports, | |
// like Node. | |
module.exports = factory.apply(root, deps.map(require)); | |
} else if (typeof define === 'function' && define.amd) { | |
// Registering as an unnamed module, more flexible and match CommonJS | |
define(deps, factory); | |
} else { | |
// Browser globals | |
var d, i = 0, global = root, old = global[name], mod; | |
while((d = deps[i]) !== undefined) { | |
deps[i] = root[d]; | |
i++; | |
} | |
global[name] = mod = factory.apply(global, deps); | |
mod.noConflict = function(){ | |
global[name] = old; | |
return mod; | |
}; | |
} | |
}(this, "myModule", ['someDependency', 'somethingElse'], function (someDependency, somethingElse) { | |
"use strict"; | |
return { | |
}; | |
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// AMD | |
require(['myModule'], function (myModule){ | |
// use myModule here | |
}); | |
// Node.js | |
var myModule = require('myModule'); | |
// Global | |
myModule | |
// if myModule is already defined, `noConflict` gives it back | |
var myNonConflictingModule = myModule.noConflict(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment