UNPKG

978 BJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 * @flow
9 */
10
11'use strict';
12
13const {SourceCode} = require('NativeModules');
14
15let _cachedDevServerURL: ?string;
16const FALLBACK = 'http://localhost:8081/';
17
18type DevServerInfo = {
19 url: string,
20 bundleLoadedFromServer: boolean,
21};
22
23/**
24 * Many RN development tools rely on the development server (packager) running
25 * @return URL to packager with trailing slash
26 */
27function getDevServer(): DevServerInfo {
28 if (_cachedDevServerURL === undefined) {
29 const match =
30 SourceCode &&
31 SourceCode.scriptURL &&
32 SourceCode.scriptURL.match(/^https?:\/\/.*?\//);
33 _cachedDevServerURL = match ? match[0] : null;
34 }
35
36 return {
37 url: _cachedDevServerURL || FALLBACK,
38 bundleLoadedFromServer: _cachedDevServerURL !== null,
39 };
40}
41
42module.exports = getDevServer;