Skip to content

Commit fbccb82

Browse files
ekaradongaearon
authored andcommitted
Add host as adjustable parameter through env variables (#717)
* Add host as adjustable parameter through env variables eg: `HOST=test.dev.local npm start` * Style nit
1 parent 92afcaf commit fbccb82

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

packages/react-scripts/scripts/start.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function clearConsole() {
8383
isFirstClear = false;
8484
}
8585

86-
function setupCompiler(port, protocol) {
86+
function setupCompiler(host, port, protocol) {
8787
// "Compiler" is a low-level interface to Webpack.
8888
// It lets us listen to some events and provide our own custom messages.
8989
compiler = webpack(config, handleCompile);
@@ -108,7 +108,7 @@ function setupCompiler(port, protocol) {
108108
console.log();
109109
console.log('The app is running at:');
110110
console.log();
111-
console.log(' ' + chalk.cyan(protocol + '://localhost:' + port + '/'));
111+
console.log(' ' + chalk.cyan(protocol + '://' + host + ':' + port + '/'));
112112
console.log();
113113
console.log('Note that the development build is not optimized.');
114114
console.log('To create a production build, use ' + chalk.cyan('npm run build') + '.');
@@ -159,14 +159,14 @@ function setupCompiler(port, protocol) {
159159
});
160160
}
161161

162-
function openBrowser(port, protocol) {
162+
function openBrowser(host, port, protocol) {
163163
if (process.platform === 'darwin') {
164164
try {
165165
// Try our best to reuse existing tab
166166
// on OS X Google Chrome with AppleScript
167167
execSync('ps cax | grep "Google Chrome"');
168168
execSync(
169-
'osascript chrome.applescript ' + protocol + '://localhost:' + port + '/',
169+
'osascript chrome.applescript ' + protocol + '://' + host + ':' + port + '/',
170170
{cwd: path.join(__dirname, 'utils'), stdio: 'ignore'}
171171
);
172172
return;
@@ -177,7 +177,7 @@ function openBrowser(port, protocol) {
177177
// Fallback to opn
178178
// (It will always open new tab)
179179
try {
180-
opn(protocol + '://localhost:' + port + '/');
180+
opn(protocol + '://' + host + ':' + port + '/');
181181
} catch (err) {
182182
// Ignore errors.
183183
}
@@ -260,7 +260,7 @@ function addMiddleware(devServer) {
260260
devServer.use(devServer.middleware);
261261
}
262262

263-
function runDevServer(port, protocol) {
263+
function runDevServer(host, port, protocol) {
264264
var devServer = new WebpackDevServer(compiler, {
265265
// Silence WebpackDevServer's own logs since they're generally not useful.
266266
// It will still show compile warnings and errors with this setting.
@@ -298,7 +298,8 @@ function runDevServer(port, protocol) {
298298
ignored: /node_modules/
299299
},
300300
// Enable HTTPS if the HTTPS environment variable is set to 'true'
301-
https: protocol === "https" ? true : false
301+
https: protocol === "https" ? true : false,
302+
host: host
302303
});
303304

304305
// Our custom middleware proxies requests to /index.html or a remote API.
@@ -313,15 +314,16 @@ function runDevServer(port, protocol) {
313314
clearConsole();
314315
console.log(chalk.cyan('Starting the development server...'));
315316
console.log();
316-
openBrowser(port, protocol);
317+
openBrowser(host, port, protocol);
317318
});
318319
}
319320

320321
function run(port) {
321322
var protocol = process.env.HTTPS === 'true' ? "https" : "http";
323+
var host = process.env.HOST || 'localhost';
322324
checkRequiredFiles();
323-
setupCompiler(port, protocol);
324-
runDevServer(port, protocol);
325+
setupCompiler(host, port, protocol);
326+
runDevServer(host, port, protocol);
325327
}
326328

327329
// We attempt to use the default port but if it is busy, we offer the user to

0 commit comments

Comments
 (0)