2016-06-01 20:50:30 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-06-01 20:50:30 +00:00
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2016-06-01 20:50:30 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
2016-06-01 20:50:30 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const {SourceCode} = require('NativeModules');
|
|
|
|
|
|
|
|
let _cachedDevServerURL: ?string;
|
|
|
|
const FALLBACK = 'http://localhost:8081/';
|
|
|
|
|
|
|
|
type DevServerInfo = {
|
2016-08-09 13:32:41 +00:00
|
|
|
url: string,
|
|
|
|
bundleLoadedFromServer: boolean,
|
2016-06-01 20:50:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Many RN development tools rely on the development server (packager) running
|
|
|
|
* @return URL to packager with trailing slash
|
|
|
|
*/
|
|
|
|
function getDevServer(): DevServerInfo {
|
|
|
|
if (_cachedDevServerURL === undefined) {
|
2018-05-11 02:06:46 +00:00
|
|
|
const match =
|
|
|
|
SourceCode &&
|
|
|
|
SourceCode.scriptURL &&
|
|
|
|
SourceCode.scriptURL.match(/^https?:\/\/.*?\//);
|
2016-06-01 20:50:30 +00:00
|
|
|
_cachedDevServerURL = match ? match[0] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
url: _cachedDevServerURL || FALLBACK,
|
|
|
|
bundleLoadedFromServer: _cachedDevServerURL !== null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getDevServer;
|