Hot Loading E2E basic flow

Summary:
public

Implement all the necessary glue code for several diffs submitted before to get Hot Loading work end to end:

- Simplify `HMRClient`: we don't need to make it stateful allowing to enable and disable it because both when we enable and disable the interface we need to reload the bundle.
- On the native side we introduced a singleton to process the bundle URL. This new class might alter the url to include the `hot` attribute. I'm not 100% sure this is the best way to implement this but we cannot use `CTLSettings` for this as it's are not available on oss and I didn't want to contaminate `RCTBridge` with something specific to hot loading. Also, we could potentially use this processor for other things in the future. Please let me know if you don't like this approach or you have a better idea :).
- Use this processor to alter the default bundle URL and request a `hot` bundle when hot loading is enabled. Also make sure to enable the HMR interface when the client activates it on the dev menu.
- Add packager `hot` option.
- Include gaeron's `react-transform` on Facebook's JS transformer.

The current implementation couples a bit React Native to this feature because `react-transform-hmr` is required on `InitializeJavaScriptAppEngine`. Ideally, the packager should accept an additional list of requires and include them on the bundle among all their dependencies. Note this is not the same as the option `runBeforeMainModule` as that one only adds a require to the provided module but doesn't include all the dependencies that module amy have that the entry point doesn't. I'll address this in a follow up task to enable asap hot loading (9536142)

I had to remove 2 `.babelrc` files from `react-proxy` and `react-deep-force-update`. There's an internal task for fixing the underlaying issue to avoid doing this horrible hack (t9515889).

Reviewed By: vjeux

Differential Revision: D2790806

fb-gh-sync-id: d4b78a2acfa071d6b3accc2e6716ef5611ad4fda
This commit is contained in:
Martín Bigio 2015-12-28 16:43:21 -08:00 committed by facebook-github-bot-6
parent 5bbd33051a
commit 724fa73adf
4 changed files with 49 additions and 11 deletions

View File

@ -137,7 +137,7 @@ class Bundler {
this._assetServer = opts.assetServer;
if (opts.getTransformOptionsModulePath) {
this._getTransformOptions = require(opts.getTransformOptionsModulePath);
this._getTransformOptionsModule = require(opts.getTransformOptionsModulePath);
}
}
@ -158,6 +158,7 @@ class Bundler {
dev: isDev,
platform,
unbundle: isUnbundle,
hot: hot,
}) {
// Const cannot have the same name as the method (babel/babel#2834)
const bbundle = new Bundle(sourceMapUrl);
@ -194,7 +195,8 @@ class Bundler {
bbundle,
response,
module,
platform
platform,
hot,
).then(transformed => {
if (bar) {
bar.tick();
@ -286,12 +288,16 @@ class Bundler {
return Promise.all([
module.getName(),
this._transformer.loadFileAndTransform(path.resolve(entryFile)),
this._transformer.loadFileAndTransform(
path.resolve(entryFile),
// TODO(martinb): pass non null main (t9527509)
this._getTransformOptions({main: null}, {hot: true}),
),
]).then(([moduleName, transformedSource]) => {
return (`
__accept(
'${moduleName}',
function(global, require, module, exports) {
'${moduleName}',
function(global, require, module, exports) {
${transformedSource.code}
}
);
@ -340,7 +346,7 @@ class Bundler {
);
}
_transformModule(bundle, response, module, platform = null) {
_transformModule(bundle, response, module, platform = null, hot = false) {
if (module.isAsset_DEPRECATED()) {
return this.generateAssetModule_DEPRECATED(bundle, module);
} else if (module.isAsset()) {
@ -350,8 +356,10 @@ class Bundler {
} else {
return this._transformer.loadFileAndTransform(
path.resolve(module.path),
this._getTransformOptions ?
this._getTransformOptions({bundle, module, platform}) : {}
this._getTransformOptions(
{bundleEntry: bundle.getMainModuleId(), modulePath: module.path},
{hot: hot},
),
);
}
}
@ -445,6 +453,14 @@ class Bundler {
});
});
}
_getTransformOptions(config, options) {
const transformerOptions = this._getTransformOptionsModule
? this._getTransformOptionsModule(config)
: null;
return {...options, ...transformerOptions};
}
}
function generateJSONModule(module) {

View File

@ -89,6 +89,7 @@ class Resolver {
// should work after this release and we can
// remove it from here.
'parse',
'react-transform-hmr',
],
platforms: ['ios', 'android'],
fileWatcher: opts.fileWatcher,

View File

@ -16,7 +16,7 @@
hot: {
acceptCallback: null,
accept: function(callback) {
this.acceptCallback = callback;
modules[id].module.hot.acceptCallback = callback;
}
}
});
@ -97,12 +97,28 @@
if (__DEV__) { // HMR
function accept(id, factory) {
var mod = modules[id];
if (!mod) {
console.error(
'Cannot accept unknown module `' + id + '`. Make sure you\'re not ' +
'trying to modify something else other than a module ' +
'(i.e.: a polyfill).'
);
}
if (!mod.module.hot) {
console.error(
'Cannot accept module because Hot Module Replacement ' +
'API was not installed.'
);
}
if (mod.module.hot.acceptCallback) {
mod.factory = factory;
mod.isInitialized = false;
require(id);
mod.hot.acceptCallback();
mod.module.hot.acceptCallback();
} else {
console.log(
'[HMR] Module `' + id + '` cannot be accepted. ' +

View File

@ -109,7 +109,11 @@ const bundleOpts = declareOpts({
unbundle: {
type: 'boolean',
default: false,
}
},
hot: {
type: 'boolean',
default: false,
},
});
const hmrBundleOpts = declareOpts({
@ -501,6 +505,7 @@ class Server {
entryFile: entryFile,
dev: this._getBoolOptionFromQuery(urlObj.query, 'dev', true),
minify: this._getBoolOptionFromQuery(urlObj.query, 'minify'),
hot: this._getBoolOptionFromQuery(urlObj.query, 'hot', false),
runModule: this._getBoolOptionFromQuery(urlObj.query, 'runModule', true),
inlineSourceMap: this._getBoolOptionFromQuery(
urlObj.query,