Skip to content

Instantly share code, notes, and snippets.

@ademilsonfp
Last active December 16, 2015 09:58
Show Gist options
  • Select an option

  • Save ademilsonfp/5416367 to your computer and use it in GitHub Desktop.

Select an option

Save ademilsonfp/5416367 to your computer and use it in GitHub Desktop.
Simple wrapper template to join multiple Node.js module files into one, used to create libraries that work on browsers.
// Simple wrapper template to join multiple Node.js module files into one to
// create libraries to work on browsers.
//
// Tokens:
//
// __name__ -> replace with library name
// '__aliases__' -> replace with object which contains aliases definition
// '__modules__' -> replace with object which contains modules definition
//
// Each module must be wrapped in a definer function like it:
//
// function(module, require) {
// // SOURCE CODE HERE
// return module;
// }
//
// There is a required module (index) which will be the library access point.
var __name__ = (function(aliases, modules) {
'use strict';
// Cache to store executed modules.
var calls = {};
var cache = {};
// Resolves relative module ID.
var resolve = function(id) {
var spl = id.split('/');
id = [];
for (var i = 0; i < spl.length; i++) {
var dir = spl[i];
if ('..' === dir) {
// Return back one directory.
id.pop();
} else if ('.' !== dir && '' !== dir) {
// Jump current directory.
id.push(dir);
}
}
return id.join('/');
};
// Require method used in local modules.
var require = function(id) {
// Checks if id is not relative or try to require a local module.
var fc = id.charAt(0);
if ('.' !== fc && '/' !== fc) {
id = resolve(id);
if (aliases.hasOwnProperty(id)) {
return aliases[id];
}
} else {
id = resolve(id);
// Obtain module definition.
var def;
if (modules.hasOwnProperty(id)) {
def = modules[id];
} else {
var index = (0 > id.length) ? id + '/index' : 'index';
if (modules.hasOwnProperty(index)) {
id = index;
def = modules[id];
}
}
// Check if module is already cached or try to call it.
if (cache.hasOwnProperty(id)) {
return cache[id].exports;
} else if (calls.hasOwnProperty(id)) {
// Return undefined if reached a infinite loop.
return undefined;
} else if ('function' === typeof def) {
// Registering module call.
calls[id] = 1;
// Builds a require function which is relative to current package.
var bp = id.lastIndexOf('/');
var pkg = (-1 < bp) ? id.slice(0, bp) : '';
var module = {};
module.require = function(id) {
if (0 < pkg.length) {
id = pkg + '/' + id;
}
return require(id);
};
// Caching module and returning it.
cache[id] = def(module, module.require);
return cache[id].exports;
}
// Adding the prefix to show the full path in error message.
id = './' + id;
}
// Trhows an error like Node.js.
var error = new Error("Undefined module '" + id + '"');
error.code = 'MODULE_NOT_FOUND';
throw error;
};
// Require index module.
return require('.');
// Wrap function end and call.
})('__aliases__', '__modules__');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment