mirror of
https://github.com/status-im/react-native.git
synced 2025-01-18 05:23:26 +00:00
fa574c6092
Summary: Related to #15126, and this would be useful for use React DevTools on real device without modify `setupDevtools.js`. In Android emulator, the host of `SourceCode.scriptURL` is same with `PlatformConstants.ServerHost` so we can just replace it. * Tested on iOS device with [react-devtools](https://github.com/facebook/react-devtools/tree/master/packages/react-devtools) package. * Tested on Android emulator, the `getDevServer` module got the correctly hostname so that don't need `adb reverse`. [ENHANCEMENT] [setupDevtools] Set host of development server for setupDevtools Closes https://github.com/facebook/react-native/pull/15547 Differential Revision: D6544980 Pulled By: javache fbshipit-source-id: a286874bcef0501c5d2e0be2251d58c236a5534a
65 lines
2.0 KiB
JavaScript
65 lines
2.0 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*
|
|
* @providesModule setupDevtools
|
|
* @flow
|
|
*/
|
|
'use strict';
|
|
|
|
type DevToolsPluginConnection = {
|
|
isAppActive: () => boolean,
|
|
host: string,
|
|
port: number,
|
|
};
|
|
|
|
type DevToolsPlugin = {
|
|
connectToDevTools: (connection: DevToolsPluginConnection) => void,
|
|
};
|
|
|
|
let register = function () {
|
|
// noop
|
|
};
|
|
|
|
if (__DEV__) {
|
|
const AppState = require('AppState');
|
|
const WebSocket = require('WebSocket');
|
|
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an
|
|
* error found when Flow v0.54 was deployed. To see the error delete this
|
|
* comment and run Flow. */
|
|
const reactDevTools = require('react-devtools-core');
|
|
const getDevServer = require('getDevServer');
|
|
|
|
// Initialize dev tools only if the native module for WebSocket is available
|
|
if (WebSocket.isAvailable) {
|
|
// Don't steal the DevTools from currently active app.
|
|
// Note: if you add any AppState subscriptions to this file,
|
|
// you will also need to guard against `AppState.isAvailable`,
|
|
// or the code will throw for bundles that don't have it.
|
|
const isAppActive = () => AppState.currentState !== 'background';
|
|
|
|
// Get hostname from development server (packager)
|
|
const devServer = getDevServer();
|
|
const host = devServer.bundleLoadedFromServer
|
|
? devServer.url.replace(/https?:\/\//, '').split(':')[0]
|
|
: 'localhost';
|
|
|
|
reactDevTools.connectToDevTools({
|
|
isAppActive,
|
|
host,
|
|
// Read the optional global variable for backward compatibility.
|
|
// It was added in https://github.com/facebook/react-native/commit/bf2b435322e89d0aeee8792b1c6e04656c2719a0.
|
|
port: window.__REACT_DEVTOOLS_PORT__,
|
|
resolveRNStyle: require('flattenStyle'),
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
register,
|
|
};
|