From 8f5d3678d8bb9adfccd29eecc525bde343d3315d Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Wed, 11 Feb 2015 16:37:51 -0800 Subject: [PATCH] 2015-02-09 updates [react-packager] Update other instances of projectRoot to projectRoots | Amjad Masad [react-packager] Debug page | Amjad Masad --- packager.js | 12 ++++-- .../haste/DependencyGraph/index.js | 24 +++++++++++- .../src/DependencyResolver/haste/index.js | 5 +++ react-packager/src/Packager/Package.js | 22 +++++++++++ react-packager/src/Packager/index.js | 6 +++ .../src/Server/__tests__/Server-test.js | 1 + react-packager/src/Server/index.js | 37 +++++++++++++++++++ 7 files changed, 101 insertions(+), 6 deletions(-) diff --git a/packager.js b/packager.js index e20bb99d..58f93a58 100644 --- a/packager.js +++ b/packager.js @@ -81,7 +81,7 @@ function openStackFrameInEditor(req, res, next) { function getAppMiddleware(options) { return ReactPackager.middleware({ dev: true, - projectRoot: options.projectRoot, + projectRoots: options.projectRoots, blacklistRE: blacklist(false), cacheVersion: '2', polyfillModuleNames: [ @@ -98,9 +98,13 @@ function runServer( var app = connect() .use(loadRawBody) .use(openStackFrameInEditor) - .use(getAppMiddleware(options)) - .use(connect.static(options.projectRoot)) - .use(connect.logger()) + .use(getAppMiddleware(options)); + + options.projectRoots.forEach(function(root) { + app.use(connect.static(root)); + }); + + app.use(connect.logger()) .use(connect.compress()) .use(connect.errorHandler()); diff --git a/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js b/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js index 0cf05411..9a939620 100644 --- a/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js +++ b/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js @@ -7,6 +7,7 @@ var docblock = require('./docblock'); var path = require('path'); var isAbsolutePath = require('absolute-path'); var debug = require('debug')('DependecyGraph'); +var util = require('util'); var readFile = q.nfbind(fs.readFile); var readDir = q.nfbind(fs.readdir); @@ -22,6 +23,7 @@ function DependecyGraph(options) { this._packageByRoot = Object.create(null); this._packagesById = Object.create(null); this._moduleById = Object.create(null); + this._debugUpdateEvents = []; this._fileWatcher = options.fileWatcher; // Kick off the search process to precompute the dependency graph. @@ -196,16 +198,20 @@ DependecyGraph.prototype._search = function() { return readDir(dir) .then(function(files){ return q.all(files.map(function(filePath) { - return realpath(path.join(dir, filePath)); + return realpath(path.join(dir, filePath)).catch(handleBrokenLink); })); }) .then(function(filePaths) { filePaths = filePaths.filter(function(filePath) { + if (filePath == null) { + return false + } + return !self._ignoreFilePath(filePath); }); var statsP = filePaths.map(function(filePath) { - return lstat(filePath); + return lstat(filePath).catch(handleBrokenLink); }); return [ @@ -397,6 +403,8 @@ DependecyGraph.prototype._processFileChange = function(eventType, filePath, root return; } + this._debugUpdateEvents.push({event: eventType, path: filePath}); + if (eventType === 'delete') { var module = this._graph[absPath]; if (module == null) { @@ -412,6 +420,13 @@ DependecyGraph.prototype._processFileChange = function(eventType, filePath, root } }; +DependecyGraph.prototype.getDebugInfo = function() { + return '

FileWatcher Update Events

' + + '
' + util.inspect(this._debugUpdateEvents) + '
' + + '

Graph dump

' + + '
' + util.inspect(this._graph) + '
'; +}; + /** * Searches all roots for the file and returns the first one that has file of the same path. */ @@ -471,4 +486,9 @@ function withExtJs(file) { } } +function handleBrokenLink(e) { + debug('WARNING: error stating, possibly broken symlink', e.message); + return q(); +} + module.exports = DependecyGraph; diff --git a/react-packager/src/DependencyResolver/haste/index.js b/react-packager/src/DependencyResolver/haste/index.js index feb69cce..211da365 100644 --- a/react-packager/src/DependencyResolver/haste/index.js +++ b/react-packager/src/DependencyResolver/haste/index.js @@ -119,8 +119,13 @@ HasteDependencyResolver.prototype.wrapModule = function(module, code) { }); }; + HasteDependencyResolver.prototype.end = function() { return this._fileWatcher.end(); }; +HasteDependencyResolver.prototype.getDebugInfo = function() { + return this._depGraph.getDebugInfo(); +}; + module.exports = HasteDependencyResolver; diff --git a/react-packager/src/Packager/Package.js b/react-packager/src/Packager/Package.js index f3f5d992..787684bc 100644 --- a/react-packager/src/Packager/Package.js +++ b/react-packager/src/Packager/Package.js @@ -108,3 +108,25 @@ Package.prototype._getMappings = function() { } return mappings; }; + +Package.prototype.getDebugInfo = function() { + return [ + '

Main Module:

' + this._mainModuleId + '
', + '', + '

Module paths and transformed code:

', + this._modules.map(function(m) { + return '

Path:

' + m.sourcePath + '

Source:

' + + '
'; + }).join('\n'), + ].join('\n'); +}; diff --git a/react-packager/src/Packager/index.js b/react-packager/src/Packager/index.js index 0bf3e7d8..3aef649d 100644 --- a/react-packager/src/Packager/index.js +++ b/react-packager/src/Packager/index.js @@ -117,9 +117,15 @@ Packager.prototype._transformModule = function(module) { }); }; + function verifyRootExists(root) { // Verify that the root exists. assert(fs.statSync(root).isDirectory(), 'Root has to be a valid directory'); } +Packager.prototype.getGraphDebugInfo = function() { + return this._resolver.getDebugInfo(); +}; + + module.exports = Packager; diff --git a/react-packager/src/Server/__tests__/Server-test.js b/react-packager/src/Server/__tests__/Server-test.js index 3b37c76e..511ec8a3 100644 --- a/react-packager/src/Server/__tests__/Server-test.js +++ b/react-packager/src/Server/__tests__/Server-test.js @@ -144,6 +144,7 @@ describe('processRequest', function(){ expect(response).toEqual("this is the first source"); expect(packageFunc.mock.calls.length).toBe(1); triggerFileChange('all','path/file.js', options.projectRoots[0]); + jest.runAllTimers(); }) .then(function(){ expect(packageFunc.mock.calls.length).toBe(2); diff --git a/react-packager/src/Server/index.js b/react-packager/src/Server/index.js index 36681d63..e71572ed 100644 --- a/react-packager/src/Server/index.js +++ b/react-packager/src/Server/index.js @@ -67,12 +67,49 @@ Server.prototype.buildPackageFromUrl = function(reqUrl) { return this._buildPackage(options); }; +Server.prototype._processDebugRequest = function(reqUrl, res) { + var ret = ''; + var pathname = url.parse(reqUrl).pathname; + var parts = pathname.split('/').filter(Boolean); + if (parts.length === 1) { + ret += '
Cached Packages
'; + ret += '
Dependency Graph
'; + res.end(ret); + } else if (parts[1] === 'packages') { + ret += '

Cached Packages

'; + q.all(Object.keys(this._packages).map(function(url) { + return this._packages[url].then(function(p) { + ret += '

' + url + '

'; + ret += p.getDebugInfo(); + }); + }, this)).then( + function() { res.end(ret); }, + function(e) { + res.wrteHead(500); + res.end('Internal Error'); + console.log(e.stack); + } + ); + } else if (parts[1] === 'graph'){ + ret += '

Dependency Graph

'; + ret += this._packager.getGraphDebugInfo(); + res.end(ret); + } else { + res.writeHead('404'); + res.end('Invalid debug request'); + return; + } +}; + Server.prototype.processRequest = function(req, res, next) { var requestType; if (req.url.match(/\.bundle$/)) { requestType = 'bundle'; } else if (req.url.match(/\.map$/)) { requestType = 'map'; + } else if (req.url.match(/^\/debug/)) { + this._processDebugRequest(req.url, res); + return; } else { return next(); }