Skip to content

Instantly share code, notes, and snippets.

@rhengles rhengles/mmdp-example.js
Last active Aug 29, 2015

Embed
What would you like to do?
mmdp - packaged (or namespaced) AMD loader
(function() {
var define = mmd.package('core').define;
define('core/1', function(){/*...*/});
define('core/2', ['core/1'], function(){/*...*/});
})();
(function() {
var define = mmd.package('lib1', ['core']).define;
define('lib1/file-a', ['core/1'], function(){/*...*/});
define('lib1/file-b', ['core/2', 'lib1/file-a'], function(){/*...*/});
})();
var fileB = mmd.package('lib1').require('lib1/file-b');
// Micro Module Definition (MMD)
// A tiny module definition and dependency management framework.
// (c) 2012 Greg MacWilliam, Threespot.
// Freely distributed under the MIT license.
var mmd = (function() {
function mmdFactory(packagesImport) {
function moduleGet(id) {
var mod = modules[id];
// Known module reference:
// Pull module definition from key table.
if ( mod ) return {local: mod};
for ( var i = 0, ii = packagesImport && packagesImport.length || 0; i < ii; i++ ) {
try {
mod = packagesImport[i].require(id);
return {imported: mod};
} catch (e) {
}
}
// Error for undefined module references.
throw(id + ' is undefined');
}
// Defines a new module.
// @param string-id
// @param array-dependencies?
// @param function-factory
function define() {
var definition = arguments,
getParam = function( type, i ) {
for ( i = 0; i < definition.length; i++ ) {
if ( typeof(definition[i]) === type ) return definition[i];
}
},
id = getParam('string'),
dependencies = getParam('object'),
factory = getParam('function');
// Error if a name or factory were not provided.
if (!id || !factory) throw('invalid definition');
// Set new module definition.
modules[ id ] = {
d: dependencies instanceof Array ? dependencies : [],
f: factory
};
}
// Requires a module. This fetches the module and all of its dependencies.
// @param string|array-moduleId
// @param function-callback
function require( req, callback ) {
var single = !(req instanceof Array),
nil = null,
id,
mod,
i;
// Wrap a single dependency definition in an array.
if (single) req = [ req ];
for ( i = 0; i < req.length; i++ ) {
id = req[i];
if (id === 'mmd') {
// MMD framework reference:
// Populate with self.
req[ i ] = api;
}
mod = moduleGet(id); // modules[ id ];
if ( mod.local ) {
mod = mod.local;
// If the module has no existing export,
// Resolve dependencies and create module.
if ( !mod.e ) {
// If module is active within the working dependency path chain,
// throw a circular reference error.
if (mod.p) throw('circular reference to ' + id);
// Flag module as active within the path chain.
mod.p = 1;
// Run factory function with recursive require call to fetch dependencies.
mod.e = mod.f.apply(nil, require(mod.d));
// Release module from the active path.
mod.p = 0;
}
// Replace dependency reference with the resolved module.
req[ i ] = mod.e;
} else if ( mod.imported ) {
req[ i ] = mod.imported;
}
}
// If a callback function was provided,
// Inject dependency array into the callback.
if (callback && callback.apply) callback.apply(nil, req);
// If directly referenced by ID, return module.
// otherwise, return array of all required modules.
return single ? req[0] : req;
}
function package(name, deps) {
var pkg;
try {
pkg = require(name);
} catch (e) {
}
if ( !pkg ) {
pkg = mmdFactory(deps && require(deps));
define(name, function() {
return pkg;
});
}
return pkg;
}
var modules = {}
, api =
{ define : define
, require: require
, factory: mmdFactory
, package: package
};
return api;
}
return mmdFactory();
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.