Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest a new port for development server if default port is in use. #196

Merged
merged 7 commits into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@
"@webpack-blocks/webpack2": "^0.4.0",
"autoprefixer": "^7.1.0",
"babel-loader": "^7.0.0",
"babel-preset-stage-1": "^6.24.1",
"babel-plugin-jsx-pragmatic": "^1.0.2",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-object-assign": "^6.22.0",
"babel-plugin-transform-react-constant-elements": "^6.23.0",
"babel-plugin-transform-react-jsx": "^6.24.1",
"babel-plugin-transform-react-remove-prop-types": "^0.4.5",
"babel-preset-env": "^1.3.3",
"babel-preset-stage-1": "^6.24.1",
"babel-register": "^6.24.1",
"copy-webpack-plugin": "^4.0.1",
"cross-spawn-promise": "^0.10.1",
Expand All @@ -123,6 +123,7 @@
"es6-promisify": "^5.0.0",
"file-loader": "^0.11.1",
"fs.promised": "^3.0.0",
"get-port": "^3.1.0",
"html-webpack-exclude-assets-plugin": "0.0.5",
"html-webpack-plugin": "^2.28.0",
"ip": "^1.1.5",
Expand Down
17 changes: 11 additions & 6 deletions src/lib/webpack/run-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ip from 'ip';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
import chalk from 'chalk';
import getPort from 'get-port';
import clientConfig from './webpack-client-config';
import serverConfig from './webpack-server-config';
import transformConfig from './transform-config';
Expand All @@ -20,6 +21,8 @@ const devBuild = async (env, onprogress) => {
let config = clientConfig(env);
await transformConfig(env, config);

let port = await getPort(config.devServer.port);

let compiler = webpack(config);
return await new Promise((resolve, reject) => {
let first = true;
Expand All @@ -30,13 +33,15 @@ const devBuild = async (env, onprogress) => {

let protocol = devServer.https ? 'https' : 'http';
let host = process.env.HOST || devServer.host || 'localhost';
let port = process.env.PORT || devServer.port || 8080;

let serverAddr = `${protocol}://${host}:${port}`;
let localIpAddr = `${protocol}://${ip.address()}:${port}`;
let serverAddr = `${protocol}://${host}:${chalk.bold(port)}`;
let localIpAddr = `${protocol}://${ip.address()}:${chalk.bold(port)}`;

process.stdout.write(chalk.green('Compiled successfully!!\n\n'));
process.stdout.write('You can view the application in the browser.\n\n');
process.stdout.write(chalk.green('\nCompiled successfully!!\n\n'));
if (parseInt(port, 10) !== parseInt(config.devServer.port, 10)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Showing this message only when it is not using default port. So, it will be easy for the users to understand what port it has been changed to !!

process.stdout.write(`Another process is running on ${chalk.bold(config.devServer.port)} port, development server is running on ${chalk.bold(port)}\n\n`);
}
process.stdout.write('You can view the application in browser.\n\n');
process.stdout.write(`${chalk.bold('Local:')} ${serverAddr}\n`);
process.stdout.write(`${chalk.bold('On Your Network:')} ${localIpAddr}\n`);
}
Expand All @@ -45,7 +50,7 @@ const devBuild = async (env, onprogress) => {
compiler.plugin('failed', reject);

let server = new WebpackDevServer(compiler, config.devServer);
server.listen(config.devServer.port);
server.listen(port);
});
};

Expand Down