Commit fbccb820 authored by ekaradon's avatar ekaradon Committed by Dan Abramov
Browse files

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
parent 92afcaf8
Showing with 12 additions and 10 deletions
+12 -10
...@@ -83,7 +83,7 @@ function clearConsole() { ...@@ -83,7 +83,7 @@ function clearConsole() {
isFirstClear = false; isFirstClear = false;
} }
function setupCompiler(port, protocol) { function setupCompiler(host, port, protocol) {
// "Compiler" is a low-level interface to Webpack. // "Compiler" is a low-level interface to Webpack.
// It lets us listen to some events and provide our own custom messages. // It lets us listen to some events and provide our own custom messages.
compiler = webpack(config, handleCompile); compiler = webpack(config, handleCompile);
...@@ -108,7 +108,7 @@ function setupCompiler(port, protocol) { ...@@ -108,7 +108,7 @@ function setupCompiler(port, protocol) {
console.log(); console.log();
console.log('The app is running at:'); console.log('The app is running at:');
console.log(); console.log();
console.log(' ' + chalk.cyan(protocol + '://localhost:' + port + '/')); console.log(' ' + chalk.cyan(protocol + '://' + host + ':' + port + '/'));
console.log(); console.log();
console.log('Note that the development build is not optimized.'); console.log('Note that the development build is not optimized.');
console.log('To create a production build, use ' + chalk.cyan('npm run build') + '.'); console.log('To create a production build, use ' + chalk.cyan('npm run build') + '.');
...@@ -159,14 +159,14 @@ function setupCompiler(port, protocol) { ...@@ -159,14 +159,14 @@ function setupCompiler(port, protocol) {
}); });
} }
function openBrowser(port, protocol) { function openBrowser(host, port, protocol) {
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
try { try {
// Try our best to reuse existing tab // Try our best to reuse existing tab
// on OS X Google Chrome with AppleScript // on OS X Google Chrome with AppleScript
execSync('ps cax | grep "Google Chrome"'); execSync('ps cax | grep "Google Chrome"');
execSync( execSync(
'osascript chrome.applescript ' + protocol + '://localhost:' + port + '/', 'osascript chrome.applescript ' + protocol + '://' + host + ':' + port + '/',
{cwd: path.join(__dirname, 'utils'), stdio: 'ignore'} {cwd: path.join(__dirname, 'utils'), stdio: 'ignore'}
); );
return; return;
...@@ -177,7 +177,7 @@ function openBrowser(port, protocol) { ...@@ -177,7 +177,7 @@ function openBrowser(port, protocol) {
// Fallback to opn // Fallback to opn
// (It will always open new tab) // (It will always open new tab)
try { try {
opn(protocol + '://localhost:' + port + '/'); opn(protocol + '://' + host + ':' + port + '/');
} catch (err) { } catch (err) {
// Ignore errors. // Ignore errors.
} }
...@@ -260,7 +260,7 @@ function addMiddleware(devServer) { ...@@ -260,7 +260,7 @@ function addMiddleware(devServer) {
devServer.use(devServer.middleware); devServer.use(devServer.middleware);
} }
function runDevServer(port, protocol) { function runDevServer(host, port, protocol) {
var devServer = new WebpackDevServer(compiler, { var devServer = new WebpackDevServer(compiler, {
// Silence WebpackDevServer's own logs since they're generally not useful. // Silence WebpackDevServer's own logs since they're generally not useful.
// It will still show compile warnings and errors with this setting. // It will still show compile warnings and errors with this setting.
...@@ -298,7 +298,8 @@ function runDevServer(port, protocol) { ...@@ -298,7 +298,8 @@ function runDevServer(port, protocol) {
ignored: /node_modules/ ignored: /node_modules/
}, },
// Enable HTTPS if the HTTPS environment variable is set to 'true' // Enable HTTPS if the HTTPS environment variable is set to 'true'
https: protocol === "https" ? true : false https: protocol === "https" ? true : false,
host: host
}); });
// Our custom middleware proxies requests to /index.html or a remote API. // Our custom middleware proxies requests to /index.html or a remote API.
...@@ -313,15 +314,16 @@ function runDevServer(port, protocol) { ...@@ -313,15 +314,16 @@ function runDevServer(port, protocol) {
clearConsole(); clearConsole();
console.log(chalk.cyan('Starting the development server...')); console.log(chalk.cyan('Starting the development server...'));
console.log(); console.log();
openBrowser(port, protocol); openBrowser(host, port, protocol);
}); });
} }
function run(port) { function run(port) {
var protocol = process.env.HTTPS === 'true' ? "https" : "http"; var protocol = process.env.HTTPS === 'true' ? "https" : "http";
var host = process.env.HOST || 'localhost';
checkRequiredFiles(); checkRequiredFiles();
setupCompiler(port, protocol); setupCompiler(host, port, protocol);
runDevServer(port, protocol); runDevServer(host, port, protocol);
} }
// We attempt to use the default port but if it is busy, we offer the user to // We attempt to use the default port but if it is busy, we offer the user to
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment