Skip to content

Commit bba35f0

Browse files
martinbigiofacebook-github-bot-9
authored andcommitted
Make Hot Loading work on OSS
Summary: public - Tweak OSS server to enable the HMR connection - Remove client gating code. - Resolve internal transforms plugins After this diff, Hot Loading should work on OSS. Reviewed By: javache Differential Revision: D2803620 fb-gh-sync-id: b678180c884d2bfaf454edf9e7abe6b3b3b32ebe
1 parent 0185df5 commit bba35f0

4 files changed

Lines changed: 47 additions & 21 deletions

File tree

local-cli/server/runServer.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99
'use strict';
1010

11+
const attachHMRServer = require('./util/attachHMRServer');
1112
const connect = require('connect');
1213
const cpuProfilerMiddleware = require('./middleware/cpuProfilerMiddleware');
1314
const getDevToolsMiddleware = require('./middleware/getDevToolsMiddleware');
@@ -23,6 +24,7 @@ const webSocketProxy = require('./util/webSocketProxy.js');
2324

2425
function runServer(args, config, readyCallback) {
2526
var wsProxy = null;
27+
const packagerServer = getPackagerServer(args, config);
2628
const app = connect()
2729
.use(loadRawBodyMiddleware)
2830
.use(connect.compress())
@@ -33,7 +35,7 @@ function runServer(args, config, readyCallback) {
3335
.use(cpuProfilerMiddleware)
3436
// Temporarily disable flow check until it's more stable
3537
//.use(getFlowTypeCheckMiddleware(args))
36-
.use(getAppMiddleware(args, config));
38+
.use(packagerServer.processRequest.bind(packagerServer));
3739

3840
args.projectRoots.forEach(root => app.use(connect.static(root)));
3941

@@ -44,6 +46,12 @@ function runServer(args, config, readyCallback) {
4446
args.port,
4547
'::',
4648
function() {
49+
attachHMRServer({
50+
httpServer: serverInstance,
51+
path: '/hot',
52+
packagerServer,
53+
});
54+
4755
wsProxy = webSocketProxy.attachToServer(serverInstance, '/debugger-proxy');
4856
webSocketProxy.attachToServer(serverInstance, '/devtools');
4957
readyCallback();
@@ -55,13 +63,13 @@ function runServer(args, config, readyCallback) {
5563
serverInstance.timeout = 0;
5664
}
5765

58-
function getAppMiddleware(args, config) {
66+
function getPackagerServer(args, config) {
5967
let transformerPath = args.transformer;
6068
if (!isAbsolutePath(transformerPath)) {
6169
transformerPath = path.resolve(process.cwd(), transformerPath);
6270
}
6371

64-
return ReactPackager.middleware({
72+
return ReactPackager.createServer({
6573
nonPersistent: args.nonPersistent,
6674
projectRoots: args.projectRoots,
6775
blacklistRE: config.getBlacklistRE(),
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
'use strict';
10+
11+
/**
12+
* Manually resolve all default Babel plugins.
13+
* `babel.transform` will attempt to resolve all base plugins relative to
14+
* the file it's compiling. This makes sure that we're using the plugins
15+
* installed in the react-native package.
16+
*/
17+
function resolvePlugins(plugins) {
18+
return plugins.map(function(plugin) {
19+
// Normalise plugin to an array.
20+
if (!Array.isArray(plugin)) {
21+
plugin = [plugin];
22+
}
23+
// Only resolve the plugin if it's a string reference.
24+
if (typeof plugin[0] === 'string') {
25+
plugin[0] = require('babel-plugin-' + plugin[0]);
26+
plugin[0] = plugin[0].__esModule ? plugin[0].default : plugin[0];
27+
}
28+
return plugin;
29+
});
30+
}
31+
32+
module.exports = resolvePlugins;

packager/react-packager/src/JSTransformer/worker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
'use strict';
1010

1111
var babel = require('babel-core');
12+
var resolvePlugins = require('./resolvePlugins');
1213
var Transforms = require('../transforms');
1314

1415
// Runs internal transforms on the given sourceCode. Note that internal
1516
// transforms should be run after the external ones to ensure that they run on
1617
// Javascript code
1718
function internalTransforms(sourceCode, filename, options) {
18-
var plugins = Transforms.getAll(options);
19+
var plugins = resolvePlugins(Transforms.getAll(options));
1920
if (plugins.length === 0) {
2021
return {
2122
code: sourceCode,

packager/transformer.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const inlineRequires = require('fbjs-scripts/babel-6/inline-requires');
1616
const json5 = require('json5');
1717
const path = require('path');
1818
const ReactPackager = require('./react-packager');
19+
const resolvePlugins = require('./react-packager/src/JSTransformer/resolvePlugins');
1920

2021
const babelRC =
2122
json5.parse(
@@ -36,23 +37,7 @@ function transform(src, filename, options) {
3637
if (options.inlineRequires) {
3738
extraPlugins.push(inlineRequires);
3839
}
39-
config.plugins = extraPlugins.concat(config.plugins);
40-
41-
// Manually resolve all default Babel plugins. babel.transform will attempt to resolve
42-
// all base plugins relative to the file it's compiling. This makes sure that we're
43-
// using the plugins installed in the react-native package.
44-
config.plugins = config.plugins.map(function(plugin) {
45-
// Normalise plugin to an array.
46-
if (!Array.isArray(plugin)) {
47-
plugin = [plugin];
48-
}
49-
// Only resolve the plugin if it's a string reference.
50-
if (typeof plugin[0] === 'string') {
51-
plugin[0] = require(`babel-plugin-${plugin[0]}`);
52-
plugin[0] = plugin[0].__esModule ? plugin[0].default : plugin[0];
53-
}
54-
return plugin;
55-
});
40+
config.plugins = resolvePlugins(extraPlugins.concat(config.plugins));
5641

5742
const result = babel.transform(src, Object.assign({}, babelRC, config));
5843

0 commit comments

Comments
 (0)