Allow parents to accept children modules

Summary:In order to be able to Hot Load Redux stores and modules that export functions, we need to build infrastructure to bubble up the HMR updates similar to how webpack does: https://webpack.github.io/docs/hot-module-replacement-with-webpack.html.

In here we introduce the minimum of this infrastructure we need to make this work. The Packager server needs to send the inverse dependencies to the HMR runtime that runs on the client so that it can bubble up the patches if they cannot be self accepted by the module that was changed.

This diff relies on https://github.com/facebook/node-haste/pull/40/files which adds support for getting the inverse dependencies.

Reviewed By: davidaurelio

Differential Revision: D2950662

fb-gh-sync-id: 26dcd4aa15da76a727026a9d7ee06e7ae4d22eaa
shipit-source-id: 26dcd4aa15da76a727026a9d7ee06e7ae4d22eaa
This commit is contained in:
Martín Bigio
2016-02-26 15:14:47 -08:00
committed by Facebook Github Bot 4
parent d7cee3a5f9
commit 436db67126
4 changed files with 84 additions and 30 deletions

View File

@@ -8,6 +8,7 @@
*/
'use strict';
const getInverseDependencies = require('node-haste').getInverseDependencies;
const querystring = require('querystring');
const url = require('url');
@@ -23,9 +24,10 @@ function attachHMRServer({httpServer, path, packagerServer}) {
packagerServer.setHMRFileChangeListener(null);
}
// Returns a promise with the full list of dependencies and the shallow
// dependencies each file on the dependency list has for the give platform
// and entry file.
// For the give platform and entry file, returns a promise with:
// - The full list of dependencies.
// - The shallow dependencies each file on the dependency list has
// - Inverse shallow dependencies map
function getDependencies(platform, bundleEntry) {
return packagerServer.getDependencies({
platform: platform,
@@ -70,12 +72,16 @@ function attachHMRServer({httpServer, path, packagerServer}) {
dependenciesModulesCache[depName] = dep;
});
})).then(() => {
return {
dependenciesCache,
dependenciesModulesCache,
shallowDependencies,
resolutionResponse: response,
};
return getInverseDependencies(response)
.then(inverseDependenciesCache => {
return {
dependenciesCache,
dependenciesModulesCache,
shallowDependencies,
inverseDependenciesCache,
resolutionResponse: response,
};
});
});
});
});
@@ -97,6 +103,7 @@ function attachHMRServer({httpServer, path, packagerServer}) {
dependenciesCache,
dependenciesModulesCache,
shallowDependencies,
inverseDependenciesCache,
}) => {
client = {
ws,
@@ -105,6 +112,7 @@ function attachHMRServer({httpServer, path, packagerServer}) {
dependenciesCache,
dependenciesModulesCache,
shallowDependencies,
inverseDependenciesCache,
};
packagerServer.setHMRFileChangeListener((filename, stat) => {
@@ -151,6 +159,7 @@ function attachHMRServer({httpServer, path, packagerServer}) {
dependenciesCache,
dependenciesModulesCache,
shallowDependencies,
inverseDependenciesCache,
resolutionResponse,
}) => {
if (!client) {
@@ -211,7 +220,8 @@ function attachHMRServer({httpServer, path, packagerServer}) {
return JSON.stringify({
type: 'update',
body: {
modules: bundle.getModulesCode(),
modules: bundle.getModulesNamesAndCode(),
inverseDependencies: inverseDependenciesCache,
sourceURLs: bundle.getSourceURLs(),
sourceMappingURLs: bundle.getSourceMappingURLs(),
},