Send assets updates through HRM interface

Reviewed By: frantic

Differential Revision: D2832693

fb-gh-sync-id: 816a01fa2f1f7cc8ca218de86b3e2e847ee005c9
This commit is contained in:
Martín Bigio 2016-01-15 10:51:56 -08:00 committed by facebook-github-bot-1
parent 85df6cf2ca
commit 8a36625689
1 changed files with 33 additions and 13 deletions

View File

@ -291,11 +291,7 @@ class Bundler {
modules.map(module => { modules.map(module => {
return Promise.all([ return Promise.all([
module.getName(), module.getName(),
this._transformer.loadFileAndTransform( this._transformModuleForHMR(module, platform),
module.path,
// TODO(martinb): pass non null main (t9527509)
this._getTransformOptions({main: null}, {hot: true}),
),
]).then(([moduleName, transformed]) => { ]).then(([moduleName, transformed]) => {
return this._resolver.resolveRequires(response, return this._resolver.resolveRequires(response,
module, module,
@ -317,6 +313,24 @@ class Bundler {
.then(modules => modules.join('\n')); .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) { invalidateFile(filePath) {
this._transformer.invalidateFile(filePath); this._transformer.invalidateFile(filePath);
} }
@ -371,9 +385,9 @@ class Bundler {
_transformModule(bundle, response, module, platform = null, hot = false) { _transformModule(bundle, response, module, platform = null, hot = false) {
if (module.isAsset_DEPRECATED()) { if (module.isAsset_DEPRECATED()) {
return this.generateAssetModule_DEPRECATED(bundle, module); return this._generateAssetModule_DEPRECATED(bundle, module);
} else if (module.isAsset()) { } else if (module.isAsset()) {
return this.generateAssetModule(bundle, module, platform); return this._generateAssetModule(bundle, module, platform);
} else if (module.isJSON()) { } else if (module.isJSON()) {
return generateJSONModule(module); return generateJSONModule(module);
} else { } else {
@ -408,7 +422,7 @@ class Bundler {
return this._resolver.getDebugInfo(); return this._resolver.getDebugInfo();
} }
generateAssetModule_DEPRECATED(bundle, module) { _generateAssetModule_DEPRECATED(bundle, module) {
return Promise.all([ return Promise.all([
sizeOf(module.path), sizeOf(module.path),
module.getName(), module.getName(),
@ -435,7 +449,7 @@ class Bundler {
}); });
} }
generateAssetModule(bundle, module, platform = null) { _generateAssetObjAndCode(module, platform = null) {
const relPath = getPathRelativeToRoot(this._projectRoots, module.path); const relPath = getPathRelativeToRoot(this._projectRoots, module.path);
var assetUrlPath = path.join('/assets', path.dirname(relPath)); var assetUrlPath = path.join('/assets', path.dirname(relPath));
@ -450,7 +464,7 @@ class Bundler {
]).then(function(res) { ]).then(function(res) {
const dimensions = res[0]; const dimensions = res[0];
const assetData = res[1]; const assetData = res[1];
const img = { const asset = {
__packager_asset: true, __packager_asset: true,
fileSystemLocation: path.dirname(module.path), fileSystemLocation: path.dirname(module.path),
httpServerLocation: assetUrlPath, httpServerLocation: assetUrlPath,
@ -463,11 +477,17 @@ class Bundler {
type: assetData.type, type: assetData.type,
}; };
bundle.addAsset(img);
const ASSET_TEMPLATE = 'module.exports = require("AssetRegistry").registerAsset(%json);'; 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({ return new ModuleTransport({
code: code, code: code,
sourceCode: code, sourceCode: code,