[react-packager] Default to index.js from main if it's a dir

This commit is contained in:
Amjad Masad 2015-03-23 15:12:34 -07:00
parent 2cafe228e4
commit 540cb4bb8e
2 changed files with 69 additions and 0 deletions

View File

@ -166,6 +166,69 @@ describe('DependencyGraph', function() {
});
});
pit('should default main package to index.js', function() {
var root = '/root';
fs.__setMockFilesystem({
'root': {
'index.js': 'require("aPackage")',
'aPackage': {
'package.json': JSON.stringify({
name: 'aPackage',
}),
'index.js': 'lol',
}
}
});
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: '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({
'root': {
'index.js': 'require("aPackage")',
'aPackage': {
'package.json': JSON.stringify({
name: 'aPackage',
main: '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: '/root/index.js', path: '/root/index.js', dependencies: ['aPackage']},
{ id: 'aPackage/lib/index',
path: '/root/aPackage/lib/index.js',
dependencies: []
},
]);
});
});
pit('should ignore malformed packages', function() {
var root = '/root';
fs.__setMockFilesystem({

View File

@ -184,6 +184,12 @@ DependecyGraph.prototype.resolveDependency = function(
var main = packageJson.main || 'index';
modulePath = withExtJs(path.join(packageJson._root, main));
dep = this._graph[modulePath];
// Some packages use just a dir and rely on an index.js inside that dir.
if (dep == null) {
dep = this._graph[path.join(packageJson._root, main, 'index.js')];
}
if (dep == null) {
throw new Error(
'Cannot find package main file for package: ' + packageJson._root