Skip to content

Instantly share code, notes, and snippets.

@RB-Lab
Last active August 29, 2015 14:22
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 RB-Lab/6973d904a9aec07e1aed to your computer and use it in GitHub Desktop.
Save RB-Lab/6973d904a9aec07e1aed to your computer and use it in GitHub Desktop.
Synchronous module micro framework.
/**
* @name module
* @global
* @description Synchronous module micro framework.
* Allows to define and require modules with only one variable (module)
* in global scope.
* @exports module
* @example
* module('my-module', function(require, module){
* var lib = require('lib');
*
* module.exports = function(arg){
* console.log('from my-module', arg);
* }
* });
*/
(function(){
var registry = {};
function require(module){
return registry[module].exports;
}
/**
* @function module
* @description registers module definition
* @param {String} name - name of the module
* @param {function} constructor - callback that will be called to
* register the module.
*/
this.module = function module(name, constructor){
registry[name] = {
exports: {}
};
/**
* @callback constructor
* @param {function} require - function to require modules
* @param {Object} module - the object which `exports` property should
* be used to define module itself
*/
constructor(require, registry[name]);
};
}).call(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment