From c086b0c34334b2475975ce94cf54481795b166a3 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Tue, 24 Mar 2015 19:34:12 -0700 Subject: [PATCH] Second Update from Tue 24 Mar --- .../DependencyResolver/ModuleDescriptor.js | 2 + .../__tests__/DependencyGraph-test.js | 108 +++++++++++++++--- .../haste/DependencyGraph/index.js | 21 ++++ .../__tests__/HasteDependencyResolver-test.js | 2 +- react-packager/src/Packager/Package.js | 10 +- .../src/Packager/__tests__/Package-test.js | 8 +- react-packager/src/Server/index.js | 9 +- 7 files changed, 129 insertions(+), 31 deletions(-) diff --git a/react-packager/src/DependencyResolver/ModuleDescriptor.js b/react-packager/src/DependencyResolver/ModuleDescriptor.js index 1388a610..c56593cf 100644 --- a/react-packager/src/DependencyResolver/ModuleDescriptor.js +++ b/react-packager/src/DependencyResolver/ModuleDescriptor.js @@ -32,6 +32,8 @@ function ModuleDescriptor(fields) { this.isAsset = fields.isAsset || false; + this.altId = fields.altId; + this._fields = fields; } diff --git a/react-packager/src/DependencyResolver/haste/DependencyGraph/__tests__/DependencyGraph-test.js b/react-packager/src/DependencyResolver/haste/DependencyGraph/__tests__/DependencyGraph-test.js index 8ebe9c5a..962a0694 100644 --- a/react-packager/src/DependencyResolver/haste/DependencyGraph/__tests__/DependencyGraph-test.js +++ b/react-packager/src/DependencyResolver/haste/DependencyGraph/__tests__/DependencyGraph-test.js @@ -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'] }, diff --git a/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js b/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js index 4622864d..adb01282 100644 --- a/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js +++ b/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js @@ -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; + } }; /** diff --git a/react-packager/src/DependencyResolver/haste/__tests__/HasteDependencyResolver-test.js b/react-packager/src/DependencyResolver/haste/__tests__/HasteDependencyResolver-test.js index 90ae5888..69354590 100644 --- a/react-packager/src/DependencyResolver/haste/__tests__/HasteDependencyResolver-test.js +++ b/react-packager/src/DependencyResolver/haste/__tests__/HasteDependencyResolver-test.js @@ -220,7 +220,7 @@ describe('HasteDependencyResolver', function() { }); describe('wrapModule', function() { - it('should ', function() { + it('should resolve modules', function() { var depResolver = new HasteDependencyResolver({ projectRoot: '/root', }); diff --git a/react-packager/src/Packager/Package.js b/react-packager/src/Packager/Package.js index 99edbbe0..0f55c8ed 100644 --- a/react-packager/src/Packager/Package.js +++ b/react-packager/src/Packager/Package.js @@ -68,10 +68,9 @@ Package.prototype._getSource = function() { Package.prototype._getInlineSourceMap = function() { if (this._inlineSourceMap == null) { var sourceMap = this.getSourceMap({excludeSource: true}); - this._inlineSourceMap = '\nRAW_SOURCE_MAP = ' + - JSON.stringify(sourceMap) + ';'; + var encoded = new Buffer(JSON.stringify(sourceMap)).toString('base64'); + this._inlineSourceMap = 'data:application/json;base64,' + encoded; } - return this._inlineSourceMap; }; @@ -85,13 +84,14 @@ Package.prototype.getSource = function(options) { } var source = this._getSource(); + source += '\n\/\/@ sourceMappingURL='; if (options.inlineSourceMap) { source += this._getInlineSourceMap(); + } else { + source += this._sourceMapUrl; } - source += '\n\/\/@ sourceMappingURL=' + this._sourceMapUrl; - return source; }; diff --git a/react-packager/src/Packager/__tests__/Package-test.js b/react-packager/src/Packager/__tests__/Package-test.js index ee94437d..5a7438d2 100644 --- a/react-packager/src/Packager/__tests__/Package-test.js +++ b/react-packager/src/Packager/__tests__/Package-test.js @@ -29,11 +29,10 @@ describe('Package', function() { ppackage.addModule('transformed foo;', 'source foo', 'foo path'); ppackage.addModule('transformed bar;', 'source bar', 'bar path'); ppackage.finalize({}); - expect(ppackage.getSource({inlineSourceMap: true})).toBe([ + expect(ppackage.getSource()).toBe([ 'transformed foo;', 'transformed bar;', - 'RAW_SOURCE_MAP = "test-source-map";', - '\/\/@ sourceMappingURL=test_url', + '\/\/@ sourceMappingURL=test_url' ].join('\n')); }); @@ -42,11 +41,10 @@ describe('Package', function() { ppackage.addModule('transformed bar;', 'source bar', 'bar path'); ppackage.setMainModuleId('foo'); ppackage.finalize({runMainModule: true}); - expect(ppackage.getSource({inlineSourceMap: true})).toBe([ + expect(ppackage.getSource()).toBe([ 'transformed foo;', 'transformed bar;', ';require("foo");', - 'RAW_SOURCE_MAP = "test-source-map";', '\/\/@ sourceMappingURL=test_url', ].join('\n')); }); diff --git a/react-packager/src/Server/index.js b/react-packager/src/Server/index.js index 19ec0039..f40ebeea 100644 --- a/react-packager/src/Server/index.js +++ b/react-packager/src/Server/index.js @@ -98,7 +98,7 @@ Server.prototype._rebuildPackages = function() { packages[key] = buildPackage(options).then(function(p) { // Make a throwaway call to getSource to cache the source string. p.getSource({ - inlineSourceMap: options.dev, + inlineSourceMap: options.inlineSourceMap, minify: options.minify, }); return p; @@ -228,7 +228,7 @@ Server.prototype.processRequest = function(req, res, next) { function(p) { if (requestType === 'bundle') { res.end(p.getSource({ - inlineSourceMap: options.dev, + inlineSourceMap: options.inlineSourceMap, minify: options.minify, })); Activity.endEvent(startReqEventId); @@ -264,6 +264,11 @@ function getOptionsFromUrl(reqUrl) { dev: getBoolOptionFromQuery(urlObj.query, 'dev', true), minify: getBoolOptionFromQuery(urlObj.query, 'minify'), runModule: getBoolOptionFromQuery(urlObj.query, 'runModule', true), + inlineSourceMap: getBoolOptionFromQuery( + urlObj.query, + 'inlineSourceMap', + false + ), }; }