Second Update from Tue 24 Mar

This commit is contained in:
Christopher Chedeau 2015-03-24 19:34:12 -07:00
parent 074065331a
commit c086b0c343
7 changed files with 129 additions and 31 deletions

View File

@ -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;
} }

View File

@ -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']
}, },

View File

@ -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;
}
}; };
/** /**

View File

@ -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',
}); });

View File

@ -68,10 +68,9 @@ Package.prototype._getSource = function() {
Package.prototype._getInlineSourceMap = function() { Package.prototype._getInlineSourceMap = function() {
if (this._inlineSourceMap == null) { if (this._inlineSourceMap == null) {
var sourceMap = this.getSourceMap({excludeSource: true}); var sourceMap = this.getSourceMap({excludeSource: true});
this._inlineSourceMap = '\nRAW_SOURCE_MAP = ' + var encoded = new Buffer(JSON.stringify(sourceMap)).toString('base64');
JSON.stringify(sourceMap) + ';'; this._inlineSourceMap = 'data:application/json;base64,' + encoded;
} }
return this._inlineSourceMap; return this._inlineSourceMap;
}; };
@ -85,13 +84,14 @@ Package.prototype.getSource = function(options) {
} }
var source = this._getSource(); var source = this._getSource();
source += '\n\/\/@ sourceMappingURL=';
if (options.inlineSourceMap) { if (options.inlineSourceMap) {
source += this._getInlineSourceMap(); source += this._getInlineSourceMap();
} else {
source += this._sourceMapUrl;
} }
source += '\n\/\/@ sourceMappingURL=' + this._sourceMapUrl;
return source; return source;
}; };

View File

@ -29,11 +29,10 @@ describe('Package', function() {
ppackage.addModule('transformed foo;', 'source foo', 'foo path'); ppackage.addModule('transformed foo;', 'source foo', 'foo path');
ppackage.addModule('transformed bar;', 'source bar', 'bar path'); ppackage.addModule('transformed bar;', 'source bar', 'bar path');
ppackage.finalize({}); ppackage.finalize({});
expect(ppackage.getSource({inlineSourceMap: true})).toBe([ expect(ppackage.getSource()).toBe([
'transformed foo;', 'transformed foo;',
'transformed bar;', 'transformed bar;',
'RAW_SOURCE_MAP = "test-source-map";', '\/\/@ sourceMappingURL=test_url'
'\/\/@ sourceMappingURL=test_url',
].join('\n')); ].join('\n'));
}); });
@ -42,11 +41,10 @@ describe('Package', function() {
ppackage.addModule('transformed bar;', 'source bar', 'bar path'); ppackage.addModule('transformed bar;', 'source bar', 'bar path');
ppackage.setMainModuleId('foo'); ppackage.setMainModuleId('foo');
ppackage.finalize({runMainModule: true}); ppackage.finalize({runMainModule: true});
expect(ppackage.getSource({inlineSourceMap: true})).toBe([ expect(ppackage.getSource()).toBe([
'transformed foo;', 'transformed foo;',
'transformed bar;', 'transformed bar;',
';require("foo");', ';require("foo");',
'RAW_SOURCE_MAP = "test-source-map";',
'\/\/@ sourceMappingURL=test_url', '\/\/@ sourceMappingURL=test_url',
].join('\n')); ].join('\n'));
}); });

View File

@ -98,7 +98,7 @@ Server.prototype._rebuildPackages = function() {
packages[key] = buildPackage(options).then(function(p) { packages[key] = buildPackage(options).then(function(p) {
// Make a throwaway call to getSource to cache the source string. // Make a throwaway call to getSource to cache the source string.
p.getSource({ p.getSource({
inlineSourceMap: options.dev, inlineSourceMap: options.inlineSourceMap,
minify: options.minify, minify: options.minify,
}); });
return p; return p;
@ -228,7 +228,7 @@ Server.prototype.processRequest = function(req, res, next) {
function(p) { function(p) {
if (requestType === 'bundle') { if (requestType === 'bundle') {
res.end(p.getSource({ res.end(p.getSource({
inlineSourceMap: options.dev, inlineSourceMap: options.inlineSourceMap,
minify: options.minify, minify: options.minify,
})); }));
Activity.endEvent(startReqEventId); Activity.endEvent(startReqEventId);
@ -264,6 +264,11 @@ function getOptionsFromUrl(reqUrl) {
dev: getBoolOptionFromQuery(urlObj.query, 'dev', true), dev: getBoolOptionFromQuery(urlObj.query, 'dev', true),
minify: getBoolOptionFromQuery(urlObj.query, 'minify'), minify: getBoolOptionFromQuery(urlObj.query, 'minify'),
runModule: getBoolOptionFromQuery(urlObj.query, 'runModule', true), runModule: getBoolOptionFromQuery(urlObj.query, 'runModule', true),
inlineSourceMap: getBoolOptionFromQuery(
urlObj.query,
'inlineSourceMap',
false
),
}; };
} }