Skip to content

Instantly share code, notes, and snippets.

@hhuuggoo
Created February 2, 2013 19:23
Show Gist options
  • Save hhuuggoo/4698893 to your computer and use it in GitHub Desktop.
Save hhuuggoo/4698893 to your computer and use it in GitHub Desktop.
// This file was generated by modules-webmake (modules for web) project.
// See: https://github.com/medikoo/modules-webmake
(function (modules) {
var getModule, getRequire, require;
getModule = (function (wrap) {
return function (scope, tree, path, fullpath) {
var name, dir, exports, module, fn, isDir;
path = path.split('/');
name = path.pop();
if (!name) {
isDir = true;
name = path.pop();
}
if ((name === '.') || (name === '..')) {
path.push(name);
name = '';
}
while ((dir = path.shift())) {
if (dir === '..') {
scope = tree.pop();
} else if (dir !== '.') {
tree.push(scope);
scope = scope[dir];
if (!scope) {
throw new Error("Could not find module '" + fullpath + "'");
}
}
}
if (name) {
if (!isDir && scope[name + '.js']) {
name += '.js';
}
if (typeof scope[name] === 'object') {
tree.push(scope);
scope = scope[name];
name = 'index.js';
}
} else {
name = 'index.js';
}
fn = scope[name];
if (!fn) throw new Error("Could not find module '" + fullpath + "'");
if (fn.hasOwnProperty('module')) return fn.module.exports;
exports = {};
fn.module = module = { exports: exports };
fn.call(exports, exports, module, getRequire(scope, tree));
return module.exports;
};
}(function (cmodule) {
return function (ignore, module) {
module.exports = cmodule.exports;
};
}));
require = function (scope, tree, fullpath) {
var name, path = fullpath, t = fullpath.charAt(0);
if (t === '/') {
path = path.slice(1);
scope = modules['/'];
tree = [];
} else if (t !== '.') {
name = path.split('/', 1)[0];
scope = modules[name];
tree = [];
path = path.slice(name.length + 1) || scope[':mainpath:'];
}
return getModule(scope, tree, path, fullpath);
};
getRequire = function (scope, tree) {
return function (path) {
return require(scope, [].concat(tree), path);
};
};
return getRequire(modules, []);
})({
"foo": {
"add.js": function (exports, module, require) {
module.exports = function() {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) sum += args[i++];
return sum;
};
},
"increment.js": function (exports, module, require) {
var add = require('./add');
module.exports = function(val) {
return add(val, 1);
};
},
"program.js": function (exports, module, require) {
var inc = require('./increment');
var a = 1;
inc(a); // 2
}
}
}
})("foo/program");
// add.js file
if (!window._modules.foo){
window._modules.foo = {}
}
window._modules.foo['add.js'] = function (exports, module, require) {
module.exports = function () {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) sum += args[i++];
return sum;
};
}
// increment.js
if (!window._modules.foo){
window._modules.foo = {}
}
window._modules.foo['increment.js'] = function (exports, module, require) {
var add = require('./add');
module.exports = function (val) {
return add(val, 1);
};
}
// file
if (!window._modules.foo){
window._modules.foo = {}
}
window._modules.foo['program.js'] = function (exports, module, require) {
var inc = require('./increment');
var a = 1;
inc(a); // 2
}
//
(function (modules) {
var getModule, getRequire, require;
getModule = (function (wrap) {
return function (scope, tree, path, fullpath) {
var name, dir, exports, module, fn, isDir;
path = path.split('/');
name = path.pop();
if (!name) {
isDir = true;
name = path.pop();
}
if ((name === '.') || (name === '..')) {
path.push(name);
name = '';
}
while ((dir = path.shift())) {
if (dir === '..') {
scope = tree.pop();
} else if (dir !== '.') {
tree.push(scope);
scope = scope[dir];
if (!scope) {
throw new Error("Could not find module '" + fullpath + "'");
}
}
}
if (name) {
if (!isDir && scope[name + '.js']) {
name += '.js';
}
if (typeof scope[name] === 'object') {
tree.push(scope);
scope = scope[name];
name = 'index.js';
}
} else {
name = 'index.js';
}
fn = scope[name];
if (!fn) throw new Error("Could not find module '" + fullpath + "'");
if (fn.hasOwnProperty('module')) return fn.module.exports;
exports = {};
fn.module = module = { exports: exports };
fn.call(exports, exports, module, getRequire(scope, tree));
return module.exports;
};
}(function (cmodule) {
return function (ignore, module) {
module.exports = cmodule.exports;
};
}));
require = function (scope, tree, fullpath) {
var name, path = fullpath, t = fullpath.charAt(0);
if (t === '/') {
path = path.slice(1);
scope = modules['/'];
tree = [];
} else if (t !== '.') {
name = path.split('/', 1)[0];
scope = modules[name];
tree = [];
path = path.slice(name.length + 1) || scope[':mainpath:'];
}
return getModule(scope, tree, path, fullpath);
};
getRequire = function (scope, tree) {
return function (path) {
return require(scope, [].concat(tree), path);
};
};
return getRequire(modules, []);
})(window._modules)("foo/program");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment