From a6b154332bbdf13d766e0136c6eb6f96b6e3b44c Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Mon, 23 Mar 2015 11:36:20 -0700 Subject: [PATCH] [react-packager] Pick up package changes while running --- packager/blacklist.js | 1 - .../haste/DependencyGraph/index.js | 84 ++++++++++++------- .../react-packager/src/FileWatcher/index.js | 2 +- packager/react-packager/src/Server/index.js | 2 +- 4 files changed, 57 insertions(+), 32 deletions(-) diff --git a/packager/blacklist.js b/packager/blacklist.js index 850a87244..60560312d 100644 --- a/packager/blacklist.js +++ b/packager/blacklist.js @@ -8,7 +8,6 @@ var sharedBlacklist = [ __dirname, 'website', - 'node_modules/parse/node_modules/xmlhttprequest/lib/XMLHttpRequest.js', 'node_modules/react-tools/src/utils/ImmutableObject.js', 'node_modules/react-tools/src/core/ReactInstanceHandles.js', 'node_modules/react-tools/src/event/EventPropagators.js' diff --git a/packager/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js b/packager/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js index 918b1e064..42a15c005 100644 --- a/packager/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js +++ b/packager/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js @@ -296,35 +296,50 @@ DependecyGraph.prototype._findAndProcessPackage = function(files, root) { } if (packagePath != null) { - return readFile(packagePath, 'utf8') - .then(function(content) { - var packageJson; - try { - packageJson = JSON.parse(content); - } catch (e) { - debug('WARNING: malformed package.json: ', packagePath); - return q(); - } - - if (packageJson.name == null) { - debug( - 'WARNING: package.json `%s` is missing a name field', - packagePath - ); - return q(); - } - - packageJson._root = root; - self._packageByRoot[root] = packageJson; - self._packagesById[packageJson.name] = packageJson; - - return packageJson; - }); + return this._processPackage(packagePath); } else { return q(); } }; +DependecyGraph.prototype._processPackage = function(packagePath) { + var packageRoot = path.dirname(packagePath); + var self = this; + return readFile(packagePath, 'utf8') + .then(function(content) { + var packageJson; + try { + packageJson = JSON.parse(content); + } catch (e) { + debug('WARNING: malformed package.json: ', packagePath); + return q(); + } + + if (packageJson.name == null) { + debug( + 'WARNING: package.json `%s` is missing a name field', + packagePath + ); + return q(); + } + + packageJson._root = packageRoot; + self._addPackageToIndices(packageJson); + + return packageJson; + }); +}; + +DependecyGraph.prototype._addPackageToIndices = function(packageJson) { + this._packageByRoot[packageJson._root] = packageJson; + this._packagesById[packageJson.name] = packageJson; +}; + +DependecyGraph.prototype._removePackageFromIndices = function(packageJson) { + delete this._packageByRoot[packageJson._root]; + delete this._packagesById[packageJson.name]; +}; + /** * Parse a module and update indices. */ @@ -436,16 +451,27 @@ DependecyGraph.prototype._processFileChange = function(eventType, filePath, root this._debugUpdateEvents.push({event: eventType, path: filePath}); + var isPackage = path.basename(filePath) === 'package.json'; if (eventType === 'delete') { - var module = this._graph[absPath]; - if (module == null) { - return; - } + if (isPackage) { + var packageJson = this._packageByRoot[path.dirname(absPath)]; + if (packageJson) { + this._removePackageFromIndices(packageJson); + } + } else { + var module = this._graph[absPath]; + if (module == null) { + return; + } - this._deleteModule(module); + this._deleteModule(module); + } } else if (!(stat && stat.isDirectory())) { var self = this; this._loading = this._loading.then(function() { + if (isPackage) { + return self._processPackage(absPath); + } return self._processModule(absPath); }); } diff --git a/packager/react-packager/src/FileWatcher/index.js b/packager/react-packager/src/FileWatcher/index.js index f2721d8cf..03e5452fe 100644 --- a/packager/react-packager/src/FileWatcher/index.js +++ b/packager/react-packager/src/FileWatcher/index.js @@ -56,7 +56,7 @@ function createWatcher(root) { } return detectingWatcherClass.then(function(Watcher) { - var watcher = new Watcher(root, {glob: '**/*.js'}); + var watcher = new Watcher(root, {glob: ['**/*.js', '**/package.json']}); return new Promise(function(resolve, reject) { var rejectTimeout = setTimeout(function() { diff --git a/packager/react-packager/src/Server/index.js b/packager/react-packager/src/Server/index.js index 3043903dd..a144c9bc0 100644 --- a/packager/react-packager/src/Server/index.js +++ b/packager/react-packager/src/Server/index.js @@ -67,7 +67,7 @@ function Server(options) { this._debouncedFileChangeHandler = _.debounce(function(filePath) { self._rebuildPackages(filePath); self._informChangeWatchers(); - }, 50, true); + }, 50); } Server.prototype._onFileChange = function(type, filepath, root) {