Skip to content

Commit c7ef8e8

Browse files
Timerromaindso
authored andcommitted
Show network address on start (facebook#2155)
* Show network address on start * Tweak visual representation
1 parent 9452e7e commit c7ef8e8

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

packages/react-scripts/config/webpackDevServer.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const paths = require('./paths');
1717
const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
1818
const host = process.env.HOST || '0.0.0.0';
1919

20-
module.exports = function(proxy) {
20+
module.exports = function(proxy, allowedHost) {
2121
return {
2222
// Enable gzip compression of generated files.
2323
compress: true,
@@ -67,6 +67,7 @@ module.exports = function(proxy) {
6767
// See https://github.com/facebookincubator/create-react-app/issues/387.
6868
disableDotRule: true,
6969
},
70+
public: allowedHost,
7071
proxy,
7172
setup(app) {
7273
// This lets us open files from the crash overlay.

packages/react-scripts/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
"react-scripts": "./bin/react-scripts.js"
2222
},
2323
"dependencies": {
24+
"@timer/detect-port": "1.1.2",
25+
"address": "1.0.1",
2426
"autoprefixer": "6.7.7",
2527
"babel-core": "6.23.1",
2628
"babel-eslint": "7.1.1",
@@ -33,7 +35,6 @@
3335
"connect-history-api-fallback": "1.3.0",
3436
"cross-spawn": "4.0.2",
3537
"css-loader": "0.28.0",
36-
"@timer/detect-port": "1.1.2",
3738
"dotenv": "2.0.0",
3839
"eslint": "3.16.1",
3940
"eslint-config-react-app": "^0.6.1",

packages/react-scripts/scripts/start.js

+30-18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ process.env.NODE_ENV = 'development';
2222
// Ensure environment variables are read.
2323
require('../config/env');
2424

25+
const address = require('address');
2526
const fs = require('fs');
2627
const chalk = require('chalk');
2728
const detect = require('@timer/detect-port');
@@ -53,25 +54,23 @@ const HOST = process.env.HOST || '0.0.0.0';
5354

5455
function run(port) {
5556
const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
56-
const formattedUrl = url.format({
57-
protocol,
58-
hostname: HOST,
59-
port,
60-
pathname: '/',
61-
});
6257

63-
let prettyHost;
64-
if (HOST === '0.0.0.0' || HOST === '::') {
58+
const formatUrl = hostname =>
59+
url.format({ protocol, hostname, port, pathname: '/' });
60+
61+
const isUnspecifiedAddress = HOST === '0.0.0.0' || HOST === '::';
62+
let prettyHost, lanAddress;
63+
if (isUnspecifiedAddress) {
6564
prettyHost = 'localhost';
65+
try {
66+
lanAddress = address.ip();
67+
} catch (_e) {
68+
// ignored
69+
}
6670
} else {
6771
prettyHost = HOST;
6872
}
69-
const prettyUrl = url.format({
70-
protocol,
71-
hostname: prettyHost,
72-
port,
73-
pathname: '/',
74-
});
73+
const prettyUrl = formatUrl(prettyHost);
7574

7675
// Create a webpack compiler that is configured with custom messages.
7776
const compiler = createWebpackCompiler(
@@ -81,9 +80,22 @@ function run(port) {
8180
return;
8281
}
8382
console.log();
84-
console.log('The app is running at:');
83+
console.log(
84+
`You can now view ${chalk.bold(require(paths.appPackageJson).name)} in the browser.`
85+
);
8586
console.log();
86-
console.log(` ${chalk.cyan(formattedUrl)}`);
87+
88+
if (isUnspecifiedAddress && lanAddress) {
89+
console.log(
90+
` ${chalk.bold('Local:')} ${chalk.cyan(prettyUrl)}`
91+
);
92+
console.log(
93+
` ${chalk.bold('On Your Network:')} ${chalk.cyan(formatUrl(lanAddress))}`
94+
);
95+
} else {
96+
console.log(` ${chalk.cyan(prettyUrl)}`);
97+
}
98+
8799
console.log();
88100
console.log('Note that the development build is not optimized.');
89101
console.log(
@@ -98,7 +110,7 @@ function run(port) {
98110
// Serve webpack assets generated by the compiler over a web sever.
99111
const devServer = new WebpackDevServer(
100112
compiler,
101-
devServerConfig(prepareProxy(proxy))
113+
devServerConfig(prepareProxy(proxy), lanAddress)
102114
);
103115

104116
// Launch WebpackDevServer.
@@ -113,7 +125,7 @@ function run(port) {
113125
console.log(chalk.cyan('Starting the development server...'));
114126
console.log();
115127

116-
openBrowser(prettyUrl);
128+
openBrowser(formatUrl(prettyHost));
117129
});
118130
}
119131

0 commit comments

Comments
 (0)