module-grapher
Advanced tools
Comparing version 0.8.0 to 0.9.0
@@ -75,3 +75,3 @@ var identifier = require('./identifier'), | ||
result.setMain(module); | ||
module.resolve(self, function(err) { | ||
module.resolve(this, function(err) { | ||
if (err) { | ||
@@ -99,6 +99,6 @@ callback(err); | ||
try { | ||
self.parse(src).forEach(function(ident) { | ||
var m = self.createModule(ident); | ||
this.parse(src).forEach(function(ident) { | ||
var m = this.createModule(ident); | ||
modules[m.id] = m; | ||
}); | ||
}, this); | ||
this.resolveModules(modules, result, function(err) { | ||
@@ -124,8 +124,57 @@ result.resolve(self); | ||
} else { | ||
module.resolve(self, function(err) { | ||
module.resolve(self, function(err, isDir) { | ||
if (err) { | ||
cont(err); | ||
} else { | ||
result.addDependency(module); | ||
self.resolveModules(module.getDirectDependencies(), result, cont); | ||
if (isDir) { | ||
// Find the identifier of the related module. This is `foo/index` | ||
// for `foo`. | ||
var indexId = module.identifier.toDirIdentifier(); | ||
// Looks to see if there already exists a module with this id. | ||
var indexModule = result.dependencies[indexId]; | ||
if (indexModule) { | ||
if (indexModule.fullPath !== module.fullPath) { | ||
// if it doesn't point to the same file, error out. | ||
var msg = '', err; | ||
msg += 'Module ' + module.id + ' references module ' + indexModule.id; | ||
msg += ' which mistakenly points to ' + indexModule.fullPath + '.'; | ||
err = new Error(msg); | ||
err.file = module.fullPath; | ||
err.longDesc = err.toString() + '\n in ' + file; | ||
err.toString = function() { return err.longDesc; }; | ||
cont(err); | ||
} else { | ||
// If it does, we'll want to make the module point to it. | ||
module.pointTo(indexModule); | ||
// Now we just need to add our module to the results object. | ||
result.addDependency(module); | ||
// We can stop here as there's no work to be done. (indexModule | ||
// has already been resolved). | ||
cont(null); | ||
} | ||
} else { | ||
// If there's no pre-exisiting indexModule module, | ||
// we'll have to generate one from scratch. | ||
indexModule = module.clone(); | ||
// Set it with a proper identifier (the one that ends in /index). | ||
indexModule.setIdentifier(indexId); | ||
// It's a clone so we'll need to wipe out its requirers as | ||
// it's only current requirer is the module itself. | ||
indexModule.clearRequirers(); | ||
// Our original module will just need to point to this clone. | ||
module.pointTo(indexModule); | ||
// And add both to the results object. | ||
result.addDependency(indexModule); | ||
result.addDependency(module); | ||
// We'll have to continue resolving the modules required by indexModule. | ||
self.resolveModules(indexModule.getDirectDependencies(), result, cont); | ||
} | ||
} else { | ||
result.addDependency(module); | ||
self.resolveModules(module.getDirectDependencies(), result, cont); | ||
} | ||
} | ||
@@ -140,16 +189,18 @@ }); | ||
var self = this; | ||
this.srcResolver.resolve(module, function(err) { | ||
this.srcResolver.resolve(module, function(err, isDir) { | ||
if (err) { | ||
module.missing = true; | ||
self.config.allowMissingModules ? callback(null) : callback(err); | ||
self.config.allowMissingModules ? callback(null, isDir) : callback(err, isDir); | ||
} else { | ||
try { | ||
self.compile(module); | ||
self.parse(module.src, module).forEach(function(ident) { | ||
var result = self.parse(module.src, module, isDir); | ||
module.ast = result.ast; | ||
result.forEach(function(ident) { | ||
var dep = self.createModule(ident); | ||
module.addDependency(dep); | ||
}); | ||
callback(null); | ||
callback(null, isDir); | ||
} catch(err) { | ||
callback(err); | ||
callback(err, isDir); | ||
} | ||
@@ -161,3 +212,3 @@ } | ||
p.parse = parse; | ||
function parse(src, requirer) { | ||
function parse(src, requirer, isDir) { | ||
var file = requirer ? requirer.fullPath : '@', // firebug convention | ||
@@ -168,3 +219,3 @@ reqIdent = requirer ? requirer.identifier : null, | ||
requirer.ast = parserOutput.ast; | ||
results.ast = parserOutput.ast; | ||
@@ -197,3 +248,3 @@ parserOutput.forEach(function(arg) { | ||
try { | ||
ident = ident.resolve(reqIdent); | ||
ident = ident.resolve(reqIdent, isDir); | ||
} catch(e) { | ||
@@ -209,3 +260,5 @@ err = e; | ||
throw err; | ||
} else if (ident) { | ||
} | ||
if (ident) { | ||
results.push(ident); | ||
@@ -212,0 +265,0 @@ } |
@@ -22,3 +22,3 @@ exports.Identifier = Identifier; | ||
p.resolve = resolve; | ||
function resolve(otherIdentifier) { | ||
function resolve(otherIdentifier, isDir) { | ||
if (this.isTopLevel()) { | ||
@@ -28,3 +28,3 @@ return this.clone(); | ||
var otherTerms = otherIdentifier ? otherIdentifier.getDirTerms() : [], | ||
var otherTerms = otherIdentifier ? otherIdentifier.getDirTerms(isDir) : [], | ||
terms = this.resolveTerms(otherTerms); | ||
@@ -80,11 +80,19 @@ return createIdentifier(terms); | ||
function clone() { | ||
return createIdentifier(this.terms.slice(0)); | ||
return createIdentifier(this.toArray()); | ||
} | ||
p.getDirTerms = getDirTerms; | ||
function getDirTerms() { | ||
var t = this.terms; | ||
return t.slice(0, t.length - 1); | ||
function getDirTerms(isDir) { | ||
var terms = this.terms, | ||
length = isDir ? terms.length : terms.length - 1; | ||
return terms.slice(0, length); | ||
} | ||
p.toDirIdentifier = toDirIdentifier; | ||
function toDirIdentifier() { | ||
var terms = this.toArray(); | ||
terms.push('index'); | ||
return createIdentifier(terms); | ||
} | ||
p.toString = toString; | ||
@@ -91,0 +99,0 @@ function toString() { |
@@ -9,9 +9,3 @@ exports.createModule = createModule; | ||
function Module(ident) { | ||
if (!ident.isTopLevel()) { | ||
throw new TypeError('Cannot instantiate Module from unresolved identifier: ' + ident); | ||
} | ||
this.identifier = ident; | ||
this.id = ident.toString(); | ||
this.searchPath = null; | ||
this.setIdentifier(ident); | ||
} | ||
@@ -34,10 +28,27 @@ | ||
p._totalSloc = 0; | ||
p.isDir = false; | ||
p.setIdentifier = setIdentifier; | ||
function setIdentifier(identifier) { | ||
if (!identifier.isTopLevel()) { | ||
throw new TypeError('Cannot instantiate Module from unresolved identifier: ' + identifier); | ||
} | ||
this.identifier = identifier; | ||
this.id = identifier.toString(); | ||
} | ||
p.resolve = resolve; | ||
function resolve(resolver, callback) { | ||
var self = this; | ||
if (resolver && this._resolver === resolver) { | ||
process.nextTick(callback); | ||
process.nextTick(function() { | ||
callback(null, self.isDir); | ||
}); | ||
} else { | ||
this._resolver = resolver; | ||
resolver.resolveModule(this, callback); | ||
resolver.resolveModule(this, function(err, isDir) { | ||
self.isDir = isDir; | ||
callback(err, isDir); | ||
}); | ||
} | ||
@@ -56,2 +67,8 @@ } | ||
p.clearDependencies = clearDependencies; | ||
function clearDependencies() { | ||
this._directDependencies = null; | ||
this._dependencies = null; | ||
} | ||
p.addRequirer = addRequirer; | ||
@@ -65,2 +82,8 @@ function addRequirer(m) { | ||
p.clearRequirers = clearRequirers; | ||
function clearRequirers() { | ||
this._requirers = null; | ||
this.lastRequiredBy = null; | ||
} | ||
p.getDirectDependencies = getDirectDependencies; | ||
@@ -96,2 +119,21 @@ function getDirectDependencies() { | ||
p.pointTo = pointTo; | ||
function pointTo(m) { | ||
this.src = 'modules.exports = require("' + m.id + '");'; | ||
this.clearDependencies(); | ||
this.addDependency(m); | ||
this._referencedModule = m; // TODO better name. | ||
} | ||
p.clone = clone; | ||
function clone() { | ||
var clone = createModule(this.identifier); | ||
for (var prop in this) { | ||
clone[prop] = this[prop]; | ||
} | ||
return clone; | ||
} | ||
p.getRequirers = getRequirers; | ||
@@ -98,0 +140,0 @@ function getRequirers() { |
var uglify = require('uglify-js'), | ||
processor = uglify.uglify, | ||
parser = uglify.parser, | ||
identifier = require('./identifier'); | ||
parser = uglify.parser; | ||
@@ -6,0 +5,0 @@ var _toString = Object.prototype.toString; |
@@ -55,3 +55,3 @@ var path = require('path'), | ||
var resolvedPath = path.resolve(self.config.root, currentPath, relativePath); | ||
self.resolveExtension(resolvedPath, module, function(p) { | ||
self.resolveExtension(resolvedPath, module, function(p, isDir) { | ||
if (p) { | ||
@@ -62,10 +62,10 @@ module.searchPath = currentPath; | ||
module.missing = true; | ||
callback(err) | ||
callback(err, isDir); | ||
} else { | ||
module.raw = src; | ||
callback(null); | ||
callback(null, isDir); | ||
} | ||
}); | ||
} else { | ||
checkNextPath() | ||
checkNextPath(); | ||
} | ||
@@ -99,3 +99,3 @@ }); | ||
module.ext = ext; | ||
callback(p); | ||
callback(p, false); | ||
} else if (self.config.allowDirModules) { | ||
@@ -108,3 +108,3 @@ // look for [modName]/index[.ext] | ||
module.ext = ext; | ||
callback(p); | ||
callback(p, true); | ||
} else { | ||
@@ -111,0 +111,0 @@ checkNextExtension(); |
@@ -5,3 +5,3 @@ { | ||
"main": "./main", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"dependencies": { | ||
@@ -12,24 +12,9 @@ "async-it": ">=0.2.0", | ||
}, | ||
"author": { | ||
"name": "Tobie Langel", | ||
"email": "tobie.langel@gmail.com", | ||
"web": "http://tobielangel.com" | ||
}, | ||
"author": "Tobie Langel <tobie.langel@gmail.com> (http://tobielangel.com)", | ||
"maintainers": [ | ||
{ | ||
"name": "Tobie Langel", | ||
"email": "tobie.langel@gmail.com", | ||
"web": "http://tobielangel.com" | ||
} | ||
"Tobie Langel <tobie.langel@gmail.com> (http://tobielangel.com)" | ||
], | ||
"contributors": [ | ||
{ | ||
"name": "Tobie Langel", | ||
"email": "tobie.langel@gmail.com", | ||
"web": "http://tobielangel.com" | ||
}, | ||
{ | ||
"name": "Chris Tice", | ||
"email": "chris.tice@gmail.com" | ||
} | ||
"Tobie Langel <tobie.langel@gmail.com> (http://tobielangel.com)", | ||
"Jamie Wong <jamie.lf.wong@gmail.com> (http://www.jamie-wong.com)" | ||
], | ||
@@ -36,0 +21,0 @@ "repository": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39999
20
1075