Skip to content

Commit

Permalink
faster package lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
E.Azer Koçulu committed Jun 4, 2012
1 parent 5496c27 commit fe7dd74
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
31 changes: 10 additions & 21 deletions templates/wrapper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var {{ name }} = (function(global, undefined){

var DEBUG = {{#debug}}true{{/debug}}{{^debug}}false{{/debug}},
pkgdefs = {},
pkgmap = {},
global = {},
lib = undefined,
Expand All @@ -15,20 +16,7 @@ ties = {{{ ties }}};
{{>library}}

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.parents[0];
} while(!pkg && parent);

return pkg;
return pkgmap[uri];
}

function findModule(workingModule, uri){
Expand Down Expand Up @@ -88,7 +76,7 @@ ties = {{{ ties }}};
}

function module(parentId, wrapper){
var parent = pkgmap[parentId],
var parent = pkgdefs[parentId],
mod = wrapper(parent),
cached = false;

Expand Down Expand Up @@ -118,10 +106,10 @@ ties = {{{ ties }}};
function pkg(/* [ parentId ...], wrapper */){

var wrapper = arguments[ arguments.length - 1 ],
parents = Array.prototype.slice.call(arguments, 0, arguments.length - 1).map(function(id){ return pkgmap[id]; }),
parents = Array.prototype.slice.call(arguments, 0, arguments.length - 1).map(function(id){ return pkgdefs[id]; }),
ctx = wrapper(parents);

if(pkgmap.hasOwnProperty(ctx.id)){
if(pkgdefs.hasOwnProperty(ctx.id)){
throw new Error('Package#'+ctx.id+' "' + ctx.name + '" has duplication of itself.');
}

Expand All @@ -130,13 +118,14 @@ ties = {{{ ties }}};
parents[i].dependencies.push(ctx);
}

pkgmap[ctx.id] = ctx;
pkgdefs[ctx.id] = ctx;
pkgmap[ctx.name] = ctx;

arguments.length == 1 && ( pkgmap['main'] = ctx );
arguments.length == 1 && ( pkgdefs['main'] = ctx );
}

function mainRequire(uri){
return pkgmap.main.main.require(uri);
return pkgdefs.main.main.require(uri);
}

function stderr(){
Expand All @@ -156,7 +145,7 @@ ties = {{{ ties }}};
'findPkg' : findPkg,
'findModule' : findModule,
'name' : '{{ name }}',
'map' : pkgmap,
'map' : pkgdefs,
'module' : module,
'pkg' : pkg,
'stderr' : stderr,
Expand Down
6 changes: 1 addition & 5 deletions test/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function moduleIds(modules){
}

function init(options, callback){
// return callback( undefined, require('../tmp/built') );
one.pkg.id = one.id();
one.build({ 'manifestPath':'example-project/package.json', 'tie':[{ 'pkg':'proc', 'obj':'process' }, { 'pkg': 'env', 'obj': 'process.env' }], 'exclude':['exclude'] }, function(error, sourceCode){

if(error) {
Expand Down Expand Up @@ -104,10 +104,6 @@ function test_moduleCtx(mod, callback){
assert.ok(a.require('dependency').f);
assert.ok(a.require('./b').b);

mod.map.main.dependencies.forEach(function(el){
console.log(333, el.name);
});

var n = mod.map.main.dependencies[ mod.map.main.dependencies[0].name == 'sibling' ? 0 :1 ].main;

assert.equal(n.id, 'n');
Expand Down

0 comments on commit fe7dd74

Please sign in to comment.