[react-packager] Fix more issues with node modules

This commit is contained in:
Amjad Masad 2015-03-24 16:55:27 -07:00
parent 9b03bec662
commit 8b79808a90
4 changed files with 114 additions and 19 deletions

View File

@ -32,6 +32,8 @@ function ModuleDescriptor(fields) {
this.isAsset = fields.isAsset || false;
this.altId = fields.altId;
this._fields = fields;
}

View File

@ -58,8 +58,8 @@ describe('DependencyGraph', function() {
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{id: 'index', path: '/root/index.js', dependencies: ['a']},
{id: 'a', path: '/root/a.js', dependencies: []},
{id: 'index', altId: '/root/index.js', path: '/root/index.js', dependencies: ['a']},
{id: 'a', altId: '/root/a.js', path: '/root/a.js', dependencies: []},
]);
});
});
@ -88,7 +88,7 @@ describe('DependencyGraph', function() {
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{id: 'index', path: '/root/index.js', dependencies: ['image!a']},
{id: 'index', altId: '/root/index.js', path: '/root/index.js', dependencies: ['image!a']},
{ id: 'image!a',
path: '/root/imgs/a.png',
dependencies: [],
@ -124,8 +124,8 @@ describe('DependencyGraph', function() {
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{id: 'index', path: '/root/index.js', dependencies: ['a']},
{id: 'a', path: '/root/a.js', dependencies: ['index']},
{id: 'index', altId: '/root/index.js', path: '/root/index.js', dependencies: ['a']},
{id: 'a', altId: '/root/a.js', path: '/root/a.js', dependencies: ['index']},
]);
});
});
@ -157,7 +157,7 @@ describe('DependencyGraph', function() {
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{id: 'index', path: '/root/index.js', dependencies: ['aPackage']},
{id: 'index', altId: '/root/index.js', path: '/root/index.js', dependencies: ['aPackage']},
{ id: 'aPackage/main',
path: '/root/aPackage/main.js',
dependencies: []
@ -196,6 +196,41 @@ describe('DependencyGraph', function() {
});
});
pit('should have altId for a package with providesModule', function() {
var root = '/root';
fs.__setMockFilesystem({
'root': {
'index.js': 'require("aPackage")',
'aPackage': {
'package.json': JSON.stringify({
name: 'aPackage',
}),
'index.js': [
'/**',
' * @providesModule EpicModule',
' */',
].join('\n'),
}
}
});
var dgraph = new DependencyGraph({
roots: [root],
fileWatcher: fileWatcher
});
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{id: '/root/index.js', path: '/root/index.js', dependencies: ['aPackage']},
{ id: 'EpicModule',
altId: 'aPackage/index',
path: '/root/aPackage/index.js',
dependencies: []
},
]);
});
});
pit('should default use index.js if main is a dir', function() {
var root = '/root';
fs.__setMockFilesystem({
@ -229,6 +264,36 @@ describe('DependencyGraph', function() {
});
});
pit('should resolve require to index if it is a dir', function() {
var root = '/root';
fs.__setMockFilesystem({
'root': {
'package.json': JSON.stringify({
name: 'test',
}),
'index.js': 'require("./lib/")',
lib: {
'index.js': 'lol',
},
}
});
var dgraph = new DependencyGraph({
roots: [root],
fileWatcher: fileWatcher
});
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{id: 'test/index', path: '/root/index.js', dependencies: ['./lib/']},
{ id: 'test/lib/index',
path: '/root/lib/index.js',
dependencies: []
},
]);
});
});
pit('should ignore malformed packages', function() {
var root = '/root';
fs.__setMockFilesystem({
@ -253,7 +318,7 @@ describe('DependencyGraph', function() {
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{id: 'index', path: '/root/index.js', dependencies: ['aPackage']},
{id: 'index', altId: '/root/index.js', path: '/root/index.js', dependencies: ['aPackage']},
]);
});
});
@ -297,10 +362,12 @@ describe('DependencyGraph', function() {
expect(dgraph.getOrderedDependencies('/root/somedir/somefile.js'))
.toEqual([
{ id: 'index',
altId: '/root/somedir/somefile.js',
path: '/root/somedir/somefile.js',
dependencies: ['c']
},
{ id: 'c',
altId: '/root/c.js',
path: '/root/c.js',
dependencies: []
},
@ -340,11 +407,12 @@ describe('DependencyGraph', function() {
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{ id: 'index',
{ id: 'index', altId: '/root/index.js',
path: '/root/index.js',
dependencies: ['aPackage']
},
{ id: 'aPackage',
altId: '/root/b.js',
path: '/root/b.js',
dependencies: []
},
@ -372,7 +440,7 @@ describe('DependencyGraph', function() {
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{ id: 'index',
{ id: 'index', altId: '/root/index.js',
path: '/root/index.js',
dependencies: ['lolomg']
}
@ -410,7 +478,7 @@ describe('DependencyGraph', function() {
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{ id: 'index',
{ id: 'index', altId: '/root/index.js',
path: '/root/index.js',
dependencies: ['aPackage/subdir/lolynot']
},
@ -453,7 +521,7 @@ describe('DependencyGraph', function() {
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{ id: 'index',
{ id: 'index', altId: '/root/index.js',
path: '/root/index.js',
dependencies: ['aPackage/subdir/lolynot']
},
@ -496,7 +564,7 @@ describe('DependencyGraph', function() {
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{ id: 'index',
{ id: 'index', altId: '/root/index.js',
path: '/root/index.js',
dependencies: ['aPackage']
},
@ -570,7 +638,7 @@ describe('DependencyGraph', function() {
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{ id: 'index',
{ id: 'index', altId: '/root/index.js',
path: '/root/index.js',
dependencies: ['aPackage']
},
@ -621,7 +689,7 @@ describe('DependencyGraph', function() {
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{ id: 'index',
{ id: 'index', altId: '/root/index.js',
path: '/root/index.js',
dependencies: ['aPackage']
},
@ -671,7 +739,7 @@ describe('DependencyGraph', function() {
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{ id: 'index',
{ id: 'index', altId: '/root/index.js',
path: '/root/index.js',
dependencies: ['aPackage', 'foo']
},
@ -730,7 +798,7 @@ describe('DependencyGraph', function() {
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{ id: 'index',
{ id: 'index', altId: '/root/index.js',
path: '/root/index.js',
dependencies: ['aPackage', 'foo']
},
@ -739,10 +807,12 @@ describe('DependencyGraph', function() {
dependencies: ['bar']
},
{ id: 'bar',
altId: '/root/bar.js',
path: '/root/bar.js',
dependencies: ['foo']
},
{ id: 'foo',
altId: '/root/foo.js',
path: '/root/foo.js',
dependencies: ['aPackage']
},
@ -803,7 +873,7 @@ describe('DependencyGraph', function() {
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{ id: 'index',
{ id: 'index', altId: '/root/index.js',
path: '/root/index.js',
dependencies: ['aPackage', 'foo']
},
@ -812,6 +882,7 @@ describe('DependencyGraph', function() {
dependencies: ['bar']
},
{ id: 'foo',
altId: '/root/foo.js',
path: '/root/foo.js',
dependencies: ['aPackage']
},
@ -857,7 +928,7 @@ describe('DependencyGraph', function() {
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{ id: 'index',
{ id: 'index', altId: '/root/index.js',
path: '/root/index.js',
dependencies: ['aPackage', 'foo']
},
@ -866,6 +937,7 @@ describe('DependencyGraph', function() {
dependencies: []
},
{ id: 'foo',
altId: '/root/foo.js',
path: '/root/foo.js',
dependencies: ['aPackage']
},

View File

@ -216,6 +216,13 @@ DependecyGraph.prototype.resolveDependency = function(
modulePath = withExtJs(path.join(dir, depModuleId));
dep = this._graph[modulePath];
if (dep == null) {
modulePath = path.join(dir, depModuleId, 'index.js');
}
dep = this._graph[modulePath];
if (dep == null) {
debug(
'WARNING: Cannot find required module `%s` from module `%s`.' +
@ -366,6 +373,10 @@ DependecyGraph.prototype._processModule = function(modulePath) {
if (moduleDocBlock.providesModule || moduleDocBlock.provides) {
moduleData.id =
moduleDocBlock.providesModule || moduleDocBlock.provides;
// Incase someone wants to require this module via
// packageName/path/to/module
moduleData.altId = self._lookupName(modulePath);
} else {
moduleData.id = self._lookupName(modulePath);
}
@ -401,6 +412,10 @@ DependecyGraph.prototype._deleteModule = function(module) {
if (this._moduleById[module.id] === module) {
delete this._moduleById[module.id];
}
if (module.altId && this._moduleById[module.altId] === module) {
delete this._moduleById[module.altId];
}
};
/**
@ -424,6 +439,12 @@ DependecyGraph.prototype._updateGraphWithModule = function(module) {
}
this._moduleById[module.id] = module;
// Some module maybe refrenced by both @providesModule and
// require(package/moduleName).
if (module.altId != null && this._moduleById[module.altId] == null) {
this._moduleById[module.altId] = module;
}
};
/**

View File

@ -220,7 +220,7 @@ describe('HasteDependencyResolver', function() {
});
describe('wrapModule', function() {
it('should ', function() {
it('should resolve modules', function() {
var depResolver = new HasteDependencyResolver({
projectRoot: '/root',
});