Skip to content

Instantly share code, notes, and snippets.

@azer

azer/gist:1286969

Created Oct 14, 2011
Embed
What would you like to do?
Example OneJS Output
var exampleProject = (function(global, undefined){
var pkgmap = {},
global = {},
_;
var lib = (function(){
var exports = {};
// Copyright Joyent, Inc. and other Node contributors.
// Minimized fork of NodeJS' path module, based on its an early version.
exports.join = function () {
return exports.normalize(Array.prototype.join.call(arguments, "/"));
};
exports.normalizeArray = function (parts, keepBlanks) {
var directories = [], prev;
for (var i = 0, l = parts.length - 1; i <= l; i++) {
var directory = parts[i];
// if it's blank, but it's not the first thing, and not the last thing, skip it.
if (directory === "" && i !== 0 && i !== l && !keepBlanks) continue;
// if it's a dot, and there was some previous dir already, then skip it.
if (directory === "." && prev !== undefined) continue;
if (
directory === ".."
&& directories.length
&& prev !== ".."
&& prev !== "."
&& prev !== undefined
&& (prev !== "" || keepBlanks)
) {
directories.pop();
prev = directories.slice(-1)[0]
} else {
if (prev === ".") directories.pop();
directories.push(directory);
prev = directory;
}
}
return directories;
};
exports.normalize = function (path, keepBlanks) {
return exports.normalizeArray(path.split("/"), keepBlanks).join("/");
};
exports.dirname = function (path) {
return path && path.substr(0, path.lastIndexOf("/")) || ".";
};
exports.basename = function (path, ext) {
var f = path.substr(path.lastIndexOf("/") + 1);
if (ext && f.substr(-1 * ext.length) === ext) {
f = f.substr(0, f.length - ext.length);
}
return f;
};
return exports;
})();
function findPkg(workingPkg, uri){
var pkg = undefined,
parent = workingPkg;
var i, len;
do {
i = parent.dependencies.length;
while(i-->0){
parent.dependencies[i].name == uri && ( pkg = parent.dependencies[i] );
}
parent = parent.parent;
} while(!pkg && parent);
return pkg;
}
function findModule(workingModule, uri){
var module = undefined,
moduleId = lib.join(lib.dirname(workingModule.id), uri),
pkg = workingModule.pkg;
var i = pkg.modules.length;
while(i-->0){
if(pkg.modules[i].id==moduleId){
module = pkg.modules[i];
break;
}
}
return module;
}
function genRequire(callingModule){
return function require(uri){
var module;
if(/^\./.test(uri)){
module = findModule(callingModule, uri);
} else {
module = findPkg(callingModule.pkg, uri).main;
}
if(!module) throw new Error('Cannot find module "'+uri+'"');
return module.call();
}
}
function module(parentId, wrapper){
var parent = pkgmap[parentId],
ctx = wrapper(parent),
exports = undefined;
ctx.require = genRequire(ctx);
ctx.call = function(){
if(exports) return exports;
exports = {};
ctx.wrapper(ctx, exports, ctx.require, global);
return exports;
}
if(parent.mainModuleId == ctx.id){
parent.main = ctx;
!parent.parent && ( _.main = ctx.call );
}
parent.modules.push(ctx);
}
function pkg(parentId, wrapper){
var parent = pkgmap[parentId],
ctx = wrapper(parent);
pkgmap[ctx.id] = ctx;
!parent && ( pkgmap['main'] = ctx );
parent && parent.dependencies.push(ctx);
}
function require(uri){
return pkgmap.main.main.require(uri);
}
return (_ = {
'lib':lib,
'findPkg':findPkg,
'findModule':findModule,
'name':'exampleProject',
'map':pkgmap,
'module':module,
'pkg':pkg,
'require':require
});
})(this);
exampleProject.pkg(undefined, function(parent){
return {
'id':1,
'name':'example-project',
'main':undefined,
'mainModuleId':'a',
'dependencies':[],
'modules':[],
'parent':parent
};
});
exampleProject.module(1, function(parent){
return {
'id':'a',
'pkg':parent,
'wrapper':function(module, exports, require, global, undefined){
require('dependency');
exports.a = true;
}
};
});
exampleProject.module(1, function(parent){
return {
'id':'b',
'pkg':parent,
'wrapper':function(module, exports, require, global, undefined){
exports.b = true;
}
};
});
exampleProject.pkg(1, function(parent){
return {
'id':2,
'name':'dependency',
'main':undefined,
'mainModuleId':'f',
'dependencies':[],
'modules':[],
'parent':parent
};
});
exampleProject.module(2, function(parent){
return {
'id':'f',
'pkg':parent,
'wrapper':function(module, exports, require, global, undefined){
require('subdependency');
exports.f = true;
}
};
});
exampleProject.module(2, function(parent){
return {
'id':'g',
'pkg':parent,
'wrapper':function(module, exports, require, global, undefined){
exports.g = true;
}
};
});
exampleProject.pkg(2, function(parent){
return {
'id':3,
'name':'subdependency',
'main':undefined,
'mainModuleId':'i',
'dependencies':[],
'modules':[],
'parent':parent
};
});
exampleProject.module(3, function(parent){
return {
'id':'i',
'pkg':parent,
'wrapper':function(module, exports, require, global, undefined){
require('sibling');
exports.i = true;
}
};
});
exampleProject.module(3, function(parent){
return {
'id':'j',
'pkg':parent,
'wrapper':function(module, exports, require, global, undefined){
exports.j = true;
}
};
});
exampleProject.pkg(1, function(parent){
return {
'id':4,
'name':'sibling',
'main':undefined,
'mainModuleId':'n',
'dependencies':[],
'modules':[],
'parent':parent
};
});
exampleProject.module(4, function(parent){
return {
'id':'p/r',
'pkg':parent,
'wrapper':function(module, exports, require, global, undefined){
require('../s/t');
exports.r = true;
}
};
});
exampleProject.module(4, function(parent){
return {
'id':'n',
'pkg':parent,
'wrapper':function(module, exports, require, global, undefined){
require('./p/r');
exports.n = true;
}
};
});
exampleProject.module(4, function(parent){
return {
'id':'s/t',
'pkg':parent,
'wrapper':function(module, exports, require, global, undefined){
exports.t = true;
}
};
});
if(typeof module != 'undefined' && module.exports ){
module.exports = exampleProject;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment