Skip to content

Commit

Permalink
Send assets updates through HRM interface
Browse files Browse the repository at this point in the history
Reviewed By: frantic

Differential Revision: D2832693

fb-gh-sync-id: 816a01fa2f1f7cc8ca218de86b3e2e847ee005c9
  • Loading branch information
martinbigio authored and facebook-github-bot-1 committed Jan 15, 2016
1 parent 38486dc commit c9f274c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion local-cli/server/util/attachHMRServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function attachHMRServer({httpServer, path, packagerServer}) {
}

// build list of modules for which we'll send HMR updates
const modulesToUpdate = [];
const modulesToUpdate = [packagerServer.getModuleForPath(filename)];
Object.keys(dependenciesModulesCache).forEach(module => {
if (!client.dependenciesModulesCache[module]) {
modulesToUpdate.push(dependenciesModulesCache[module]);
Expand Down
46 changes: 33 additions & 13 deletions packager/react-packager/src/Bundler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,7 @@ class Bundler {
modules.map(module => {
return Promise.all([
module.getName(),
this._transformer.loadFileAndTransform(
module.path,
// TODO(martinb): pass non null main (t9527509)
this._getTransformOptions({main: null}, {hot: true}),
),
this._transformModuleForHMR(module, platform),
]).then(([moduleName, transformed]) => {
return this._resolver.resolveRequires(response,
module,
Expand All @@ -317,6 +313,24 @@ class Bundler {
.then(modules => modules.join('\n'));
}

_transformModuleForHMR(module, platform) {
if (module.isAsset()) {
return this._generateAssetObjAndCode(module, platform).then(
({asset, code}) => {
return {
code,
};
}
);
} else {
return this._transformer.loadFileAndTransform(
module.path,
// TODO(martinb): pass non null main (t9527509)
this._getTransformOptions({main: null}, {hot: true}),
);
}
}

invalidateFile(filePath) {
this._transformer.invalidateFile(filePath);
}
Expand Down Expand Up @@ -371,9 +385,9 @@ class Bundler {

_transformModule(bundle, response, module, platform = null, hot = false) {
if (module.isAsset_DEPRECATED()) {
return this.generateAssetModule_DEPRECATED(bundle, module);
return this._generateAssetModule_DEPRECATED(bundle, module);
} else if (module.isAsset()) {
return this.generateAssetModule(bundle, module, platform);
return this._generateAssetModule(bundle, module, platform);
} else if (module.isJSON()) {
return generateJSONModule(module);
} else {
Expand Down Expand Up @@ -408,7 +422,7 @@ class Bundler {
return this._resolver.getDebugInfo();
}

generateAssetModule_DEPRECATED(bundle, module) {
_generateAssetModule_DEPRECATED(bundle, module) {
return Promise.all([
sizeOf(module.path),
module.getName(),
Expand All @@ -435,7 +449,7 @@ class Bundler {
});
}

generateAssetModule(bundle, module, platform = null) {
_generateAssetObjAndCode(module, platform = null) {
const relPath = getPathRelativeToRoot(this._projectRoots, module.path);
var assetUrlPath = path.join('/assets', path.dirname(relPath));

Expand All @@ -450,7 +464,7 @@ class Bundler {
]).then(function(res) {
const dimensions = res[0];
const assetData = res[1];
const img = {
const asset = {
__packager_asset: true,
fileSystemLocation: path.dirname(module.path),
httpServerLocation: assetUrlPath,
Expand All @@ -463,11 +477,17 @@ class Bundler {
type: assetData.type,
};

bundle.addAsset(img);

const ASSET_TEMPLATE = 'module.exports = require("AssetRegistry").registerAsset(%json);';
const code = ASSET_TEMPLATE.replace('%json', JSON.stringify(img));
const code = ASSET_TEMPLATE.replace('%json', JSON.stringify(asset));

return {asset, code};
});
}


_generateAssetModule(bundle, module, platform = null) {
return this._generateAssetObjAndCode(module, platform).then(({asset, code}) => {
bundle.addAsset(asset);
return new ModuleTransport({
code: code,
sourceCode: code,
Expand Down

0 comments on commit c9f274c

Please sign in to comment.