mirror of
https://github.com/status-im/metro.git
synced 2025-02-28 10:40:32 +00:00
[react-packager] Fix more issues with node modules
This commit is contained in:
parent
fc92348d8b
commit
bef3d46cee
@ -32,6 +32,8 @@ function ModuleDescriptor(fields) {
|
|||||||
|
|
||||||
this.isAsset = fields.isAsset || false;
|
this.isAsset = fields.isAsset || false;
|
||||||
|
|
||||||
|
this.altId = fields.altId;
|
||||||
|
|
||||||
this._fields = fields;
|
this._fields = fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,8 +58,8 @@ describe('DependencyGraph', function() {
|
|||||||
return dgraph.load().then(function() {
|
return dgraph.load().then(function() {
|
||||||
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
||||||
.toEqual([
|
.toEqual([
|
||||||
{id: 'index', path: '/root/index.js', dependencies: ['a']},
|
{id: 'index', altId: '/root/index.js', path: '/root/index.js', dependencies: ['a']},
|
||||||
{id: 'a', path: '/root/a.js', dependencies: []},
|
{id: 'a', altId: '/root/a.js', path: '/root/a.js', dependencies: []},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -88,7 +88,7 @@ describe('DependencyGraph', function() {
|
|||||||
return dgraph.load().then(function() {
|
return dgraph.load().then(function() {
|
||||||
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
||||||
.toEqual([
|
.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',
|
{ id: 'image!a',
|
||||||
path: '/root/imgs/a.png',
|
path: '/root/imgs/a.png',
|
||||||
dependencies: [],
|
dependencies: [],
|
||||||
@ -124,8 +124,8 @@ describe('DependencyGraph', function() {
|
|||||||
return dgraph.load().then(function() {
|
return dgraph.load().then(function() {
|
||||||
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
||||||
.toEqual([
|
.toEqual([
|
||||||
{id: 'index', path: '/root/index.js', dependencies: ['a']},
|
{id: 'index', altId: '/root/index.js', path: '/root/index.js', dependencies: ['a']},
|
||||||
{id: 'a', path: '/root/a.js', dependencies: ['index']},
|
{id: 'a', altId: '/root/a.js', path: '/root/a.js', dependencies: ['index']},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -157,7 +157,7 @@ describe('DependencyGraph', function() {
|
|||||||
return dgraph.load().then(function() {
|
return dgraph.load().then(function() {
|
||||||
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
||||||
.toEqual([
|
.toEqual([
|
||||||
{id: 'index', path: '/root/index.js', dependencies: ['aPackage']},
|
{id: 'index', altId: '/root/index.js', path: '/root/index.js', dependencies: ['aPackage']},
|
||||||
{ id: 'aPackage/main',
|
{ id: 'aPackage/main',
|
||||||
path: '/root/aPackage/main.js',
|
path: '/root/aPackage/main.js',
|
||||||
dependencies: []
|
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() {
|
pit('should default use index.js if main is a dir', function() {
|
||||||
var root = '/root';
|
var root = '/root';
|
||||||
fs.__setMockFilesystem({
|
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() {
|
pit('should ignore malformed packages', function() {
|
||||||
var root = '/root';
|
var root = '/root';
|
||||||
fs.__setMockFilesystem({
|
fs.__setMockFilesystem({
|
||||||
@ -253,7 +318,7 @@ describe('DependencyGraph', function() {
|
|||||||
return dgraph.load().then(function() {
|
return dgraph.load().then(function() {
|
||||||
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
||||||
.toEqual([
|
.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'))
|
expect(dgraph.getOrderedDependencies('/root/somedir/somefile.js'))
|
||||||
.toEqual([
|
.toEqual([
|
||||||
{ id: 'index',
|
{ id: 'index',
|
||||||
|
altId: '/root/somedir/somefile.js',
|
||||||
path: '/root/somedir/somefile.js',
|
path: '/root/somedir/somefile.js',
|
||||||
dependencies: ['c']
|
dependencies: ['c']
|
||||||
},
|
},
|
||||||
{ id: 'c',
|
{ id: 'c',
|
||||||
|
altId: '/root/c.js',
|
||||||
path: '/root/c.js',
|
path: '/root/c.js',
|
||||||
dependencies: []
|
dependencies: []
|
||||||
},
|
},
|
||||||
@ -340,11 +407,12 @@ describe('DependencyGraph', function() {
|
|||||||
return dgraph.load().then(function() {
|
return dgraph.load().then(function() {
|
||||||
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
||||||
.toEqual([
|
.toEqual([
|
||||||
{ id: 'index',
|
{ id: 'index', altId: '/root/index.js',
|
||||||
path: '/root/index.js',
|
path: '/root/index.js',
|
||||||
dependencies: ['aPackage']
|
dependencies: ['aPackage']
|
||||||
},
|
},
|
||||||
{ id: 'aPackage',
|
{ id: 'aPackage',
|
||||||
|
altId: '/root/b.js',
|
||||||
path: '/root/b.js',
|
path: '/root/b.js',
|
||||||
dependencies: []
|
dependencies: []
|
||||||
},
|
},
|
||||||
@ -372,7 +440,7 @@ describe('DependencyGraph', function() {
|
|||||||
return dgraph.load().then(function() {
|
return dgraph.load().then(function() {
|
||||||
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
||||||
.toEqual([
|
.toEqual([
|
||||||
{ id: 'index',
|
{ id: 'index', altId: '/root/index.js',
|
||||||
path: '/root/index.js',
|
path: '/root/index.js',
|
||||||
dependencies: ['lolomg']
|
dependencies: ['lolomg']
|
||||||
}
|
}
|
||||||
@ -410,7 +478,7 @@ describe('DependencyGraph', function() {
|
|||||||
return dgraph.load().then(function() {
|
return dgraph.load().then(function() {
|
||||||
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
||||||
.toEqual([
|
.toEqual([
|
||||||
{ id: 'index',
|
{ id: 'index', altId: '/root/index.js',
|
||||||
path: '/root/index.js',
|
path: '/root/index.js',
|
||||||
dependencies: ['aPackage/subdir/lolynot']
|
dependencies: ['aPackage/subdir/lolynot']
|
||||||
},
|
},
|
||||||
@ -453,7 +521,7 @@ describe('DependencyGraph', function() {
|
|||||||
return dgraph.load().then(function() {
|
return dgraph.load().then(function() {
|
||||||
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
||||||
.toEqual([
|
.toEqual([
|
||||||
{ id: 'index',
|
{ id: 'index', altId: '/root/index.js',
|
||||||
path: '/root/index.js',
|
path: '/root/index.js',
|
||||||
dependencies: ['aPackage/subdir/lolynot']
|
dependencies: ['aPackage/subdir/lolynot']
|
||||||
},
|
},
|
||||||
@ -496,7 +564,7 @@ describe('DependencyGraph', function() {
|
|||||||
return dgraph.load().then(function() {
|
return dgraph.load().then(function() {
|
||||||
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
||||||
.toEqual([
|
.toEqual([
|
||||||
{ id: 'index',
|
{ id: 'index', altId: '/root/index.js',
|
||||||
path: '/root/index.js',
|
path: '/root/index.js',
|
||||||
dependencies: ['aPackage']
|
dependencies: ['aPackage']
|
||||||
},
|
},
|
||||||
@ -570,7 +638,7 @@ describe('DependencyGraph', function() {
|
|||||||
return dgraph.load().then(function() {
|
return dgraph.load().then(function() {
|
||||||
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
||||||
.toEqual([
|
.toEqual([
|
||||||
{ id: 'index',
|
{ id: 'index', altId: '/root/index.js',
|
||||||
path: '/root/index.js',
|
path: '/root/index.js',
|
||||||
dependencies: ['aPackage']
|
dependencies: ['aPackage']
|
||||||
},
|
},
|
||||||
@ -621,7 +689,7 @@ describe('DependencyGraph', function() {
|
|||||||
return dgraph.load().then(function() {
|
return dgraph.load().then(function() {
|
||||||
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
||||||
.toEqual([
|
.toEqual([
|
||||||
{ id: 'index',
|
{ id: 'index', altId: '/root/index.js',
|
||||||
path: '/root/index.js',
|
path: '/root/index.js',
|
||||||
dependencies: ['aPackage']
|
dependencies: ['aPackage']
|
||||||
},
|
},
|
||||||
@ -671,7 +739,7 @@ describe('DependencyGraph', function() {
|
|||||||
return dgraph.load().then(function() {
|
return dgraph.load().then(function() {
|
||||||
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
||||||
.toEqual([
|
.toEqual([
|
||||||
{ id: 'index',
|
{ id: 'index', altId: '/root/index.js',
|
||||||
path: '/root/index.js',
|
path: '/root/index.js',
|
||||||
dependencies: ['aPackage', 'foo']
|
dependencies: ['aPackage', 'foo']
|
||||||
},
|
},
|
||||||
@ -730,7 +798,7 @@ describe('DependencyGraph', function() {
|
|||||||
return dgraph.load().then(function() {
|
return dgraph.load().then(function() {
|
||||||
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
||||||
.toEqual([
|
.toEqual([
|
||||||
{ id: 'index',
|
{ id: 'index', altId: '/root/index.js',
|
||||||
path: '/root/index.js',
|
path: '/root/index.js',
|
||||||
dependencies: ['aPackage', 'foo']
|
dependencies: ['aPackage', 'foo']
|
||||||
},
|
},
|
||||||
@ -739,10 +807,12 @@ describe('DependencyGraph', function() {
|
|||||||
dependencies: ['bar']
|
dependencies: ['bar']
|
||||||
},
|
},
|
||||||
{ id: 'bar',
|
{ id: 'bar',
|
||||||
|
altId: '/root/bar.js',
|
||||||
path: '/root/bar.js',
|
path: '/root/bar.js',
|
||||||
dependencies: ['foo']
|
dependencies: ['foo']
|
||||||
},
|
},
|
||||||
{ id: 'foo',
|
{ id: 'foo',
|
||||||
|
altId: '/root/foo.js',
|
||||||
path: '/root/foo.js',
|
path: '/root/foo.js',
|
||||||
dependencies: ['aPackage']
|
dependencies: ['aPackage']
|
||||||
},
|
},
|
||||||
@ -803,7 +873,7 @@ describe('DependencyGraph', function() {
|
|||||||
return dgraph.load().then(function() {
|
return dgraph.load().then(function() {
|
||||||
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
||||||
.toEqual([
|
.toEqual([
|
||||||
{ id: 'index',
|
{ id: 'index', altId: '/root/index.js',
|
||||||
path: '/root/index.js',
|
path: '/root/index.js',
|
||||||
dependencies: ['aPackage', 'foo']
|
dependencies: ['aPackage', 'foo']
|
||||||
},
|
},
|
||||||
@ -812,6 +882,7 @@ describe('DependencyGraph', function() {
|
|||||||
dependencies: ['bar']
|
dependencies: ['bar']
|
||||||
},
|
},
|
||||||
{ id: 'foo',
|
{ id: 'foo',
|
||||||
|
altId: '/root/foo.js',
|
||||||
path: '/root/foo.js',
|
path: '/root/foo.js',
|
||||||
dependencies: ['aPackage']
|
dependencies: ['aPackage']
|
||||||
},
|
},
|
||||||
@ -857,7 +928,7 @@ describe('DependencyGraph', function() {
|
|||||||
return dgraph.load().then(function() {
|
return dgraph.load().then(function() {
|
||||||
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
expect(dgraph.getOrderedDependencies('/root/index.js'))
|
||||||
.toEqual([
|
.toEqual([
|
||||||
{ id: 'index',
|
{ id: 'index', altId: '/root/index.js',
|
||||||
path: '/root/index.js',
|
path: '/root/index.js',
|
||||||
dependencies: ['aPackage', 'foo']
|
dependencies: ['aPackage', 'foo']
|
||||||
},
|
},
|
||||||
@ -866,6 +937,7 @@ describe('DependencyGraph', function() {
|
|||||||
dependencies: []
|
dependencies: []
|
||||||
},
|
},
|
||||||
{ id: 'foo',
|
{ id: 'foo',
|
||||||
|
altId: '/root/foo.js',
|
||||||
path: '/root/foo.js',
|
path: '/root/foo.js',
|
||||||
dependencies: ['aPackage']
|
dependencies: ['aPackage']
|
||||||
},
|
},
|
||||||
|
@ -216,6 +216,13 @@ DependecyGraph.prototype.resolveDependency = function(
|
|||||||
modulePath = withExtJs(path.join(dir, depModuleId));
|
modulePath = withExtJs(path.join(dir, depModuleId));
|
||||||
|
|
||||||
dep = this._graph[modulePath];
|
dep = this._graph[modulePath];
|
||||||
|
|
||||||
|
if (dep == null) {
|
||||||
|
modulePath = path.join(dir, depModuleId, 'index.js');
|
||||||
|
}
|
||||||
|
|
||||||
|
dep = this._graph[modulePath];
|
||||||
|
|
||||||
if (dep == null) {
|
if (dep == null) {
|
||||||
debug(
|
debug(
|
||||||
'WARNING: Cannot find required module `%s` from module `%s`.' +
|
'WARNING: Cannot find required module `%s` from module `%s`.' +
|
||||||
@ -366,6 +373,10 @@ DependecyGraph.prototype._processModule = function(modulePath) {
|
|||||||
if (moduleDocBlock.providesModule || moduleDocBlock.provides) {
|
if (moduleDocBlock.providesModule || moduleDocBlock.provides) {
|
||||||
moduleData.id =
|
moduleData.id =
|
||||||
moduleDocBlock.providesModule || moduleDocBlock.provides;
|
moduleDocBlock.providesModule || moduleDocBlock.provides;
|
||||||
|
|
||||||
|
// Incase someone wants to require this module via
|
||||||
|
// packageName/path/to/module
|
||||||
|
moduleData.altId = self._lookupName(modulePath);
|
||||||
} else {
|
} else {
|
||||||
moduleData.id = self._lookupName(modulePath);
|
moduleData.id = self._lookupName(modulePath);
|
||||||
}
|
}
|
||||||
@ -401,6 +412,10 @@ DependecyGraph.prototype._deleteModule = function(module) {
|
|||||||
if (this._moduleById[module.id] === module) {
|
if (this._moduleById[module.id] === module) {
|
||||||
delete this._moduleById[module.id];
|
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;
|
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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -220,7 +220,7 @@ describe('HasteDependencyResolver', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('wrapModule', function() {
|
describe('wrapModule', function() {
|
||||||
it('should ', function() {
|
it('should resolve modules', function() {
|
||||||
var depResolver = new HasteDependencyResolver({
|
var depResolver = new HasteDependencyResolver({
|
||||||
projectRoot: '/root',
|
projectRoot: '/root',
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user