Skip to content

feat: improve host option #3112

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

Merged
merged 3 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ Options:
--setup-exit-signals Close and exit the process on SIGINT and SIGTERM.
--no-setup-exit-signals Do not close and exit the process on SIGNIT and SIGTERM.
--open [value] Open the default browser, or optionally specify a browser name.
--use-local-ip Open default browser with local IP.
--open-page <value...> Open default browser with the specified page.
--client-logging <value> Log level in the browser (none, error, warn, info, log, verbose).
--history-api-fallback Fallback to /index.html for Single Page Applications.
Expand Down
10 changes: 0 additions & 10 deletions bin/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,6 @@ module.exports = {
description:
'Open the default browser, or optionally specify a browser name.',
},
{
name: 'use-local-ip',
type: Boolean,
configs: [
{
type: 'boolean',
},
],
description: 'Open default browser with local IP.',
},
{
name: 'open-page',
type: String,
Expand Down
22 changes: 16 additions & 6 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,8 @@ class Server {
this.server = http.createServer(this.app);
}

this.server.on('error', (err) => {
throw err;
this.server.on('error', (error) => {
throw error;
});
}

Expand Down Expand Up @@ -724,7 +724,16 @@ class Server {
}

listen(port, hostname, fn) {
this.hostname = hostname;
if (hostname === 'local-ip') {
this.hostname =
internalIp.v4.sync() || '0.0.0.0' || internalIp.v6.sync() || '::';
Copy link
Contributor

@ylemkimon ylemkimon Mar 29, 2021

Choose a reason for hiding this comment

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

Wouldn't this be a dead code, since '0.0.0.0' can be never false?

Copy link
Member

Choose a reason for hiding this comment

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

Oh, yep, we need change it to internalIp.v4.sync() || internalIp.v6.sync() || '0.0.0.0'

Copy link
Member

Choose a reason for hiding this comment

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

Feel free to send a fix

Copy link
Member

Choose a reason for hiding this comment

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

fixed here #3128

} else if (hostname === 'local-ipv4') {
this.hostname = internalIp.v4.sync() || '0.0.0.0';
} else if (hostname === 'local-ipv6') {
this.hostname = internalIp.v6.sync() || '::';
} else {
this.hostname = hostname;
}

if (typeof port !== 'undefined' && port !== this.options.port) {
this.logger.warn(
Expand All @@ -737,8 +746,7 @@ class Server {
// eslint-disable-next-line no-shadow
.then((port) => {
this.port = port;

return this.server.listen(port, hostname, (error) => {
return this.server.listen(port, this.hostname, (error) => {
if (this.options.hot || this.options.liveReload) {
this.createSocketServer();
}
Expand Down Expand Up @@ -944,11 +952,13 @@ class Server {
if (!isFile) {
return next();
}

// Serve a page that executes the javascript
const queries = req._parsedUrl.search || '';
const responsePage = `<!DOCTYPE html><html><head><meta charset="utf-8"/></head><body><script type="text/javascript" charset="utf-8" src="${_path}.js${queries}"></script></body></html>`;

res.send(responsePage);
} catch (err) {
} catch (error) {
return next();
}
}
Expand Down
6 changes: 1 addition & 5 deletions lib/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,6 @@
"enum": ["sockjs", "ws"]
}
]
},
"useLocalIp": {
"type": "boolean"
}
},
"errorMessage": {
Expand All @@ -403,8 +400,7 @@
"public": "should be {String} (https://webpack.js.org/configuration/dev-server/#devserverpublic)",
"setupExitSignals": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserversetupexitsignals)",
"static": "should be {Boolean|String|Object|Array} (https://webpack.js.org/configuration/dev-server/#devserverstatic)",
"transportMode": "should be {String|Object} (https://webpack.js.org/configuration/dev-server/#devservertransportmode)",
"useLocalIp": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserveruselocalip)"
"transportMode": "should be {String|Object} (https://webpack.js.org/configuration/dev-server/#devservertransportmode)"
}
},
"additionalProperties": false
Expand Down
5 changes: 1 addition & 4 deletions lib/utils/createDomain.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
'use strict';

const url = require('url');
const ip = require('internal-ip');

function createDomain(options, server) {
const protocol = options.https ? 'https' : 'http';
// use location hostname and port by default in createSocketUrl
// ipv6 detection is not required as 0.0.0.0 is just used as a placeholder
let hostname;

if (options.useLocalIp) {
hostname = ip.v4.sync() || '0.0.0.0';
} else if (server) {
if (server) {
hostname = server.address().address;
} else {
hostname = '0.0.0.0';
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/Validation.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ exports[`Validation validation should fail validation for invalid \`static\` con
exports[`Validation validation should fail validation for no additional properties 1`] = `
"Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
- configuration has an unknown property 'additional'. These properties are valid:
object { bonjour?, client?, compress?, dev?, firewall?, headers?, historyApiFallback?, host?, hot?, http2?, https?, liveReload?, onAfterSetupMiddleware?, onBeforeSetupMiddleware?, onListening?, open?, openPage?, port?, proxy?, public?, setupExitSignals?, static?, transportMode?, useLocalIp? }"
object { bonjour?, client?, compress?, dev?, firewall?, headers?, historyApiFallback?, host?, hot?, http2?, https?, liveReload?, onAfterSetupMiddleware?, onBeforeSetupMiddleware?, onListening?, open?, openPage?, port?, proxy?, public?, setupExitSignals?, static?, transportMode? }"
`;
12 changes: 12 additions & 0 deletions test/cli/__snapshots__/cli.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ exports[`CLI --host <IPv4>: stderr 1`] = `
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
`;

exports[`CLI --host <local-ip>: stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> [webpack-dev-server] On Your Network (IPv4): http://<network-ip-v4>:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
`;

exports[`CLI --host <local-ipv4>: stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> [webpack-dev-server] On Your Network (IPv4): http://<network-ip-v4>:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
`;

exports[`CLI --host 0.0.0.0 (IPv4): stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> [webpack-dev-server] Loopback: http://localhost:<port>/
Expand Down
20 changes: 20 additions & 0 deletions test/cli/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,26 @@ describe('CLI', () => {
.catch(done);
});

it('--host <local-ip>', (done) => {
testBin('--host local-ip')
.then((output) => {
expect(normalizeStderr(output.stderr)).toMatchSnapshot('stderr');

done();
})
.catch(done);
});

it('--host <local-ipv4>', (done) => {
testBin('--host local-ipv4')
.then((output) => {
expect(normalizeStderr(output.stderr)).toMatchSnapshot('stderr');

done();
})
.catch(done);
});

it('--host localhost --port 9999', (done) => {
testBin('--host localhost --port 9999')
.then((output) => {
Expand Down
4 changes: 0 additions & 4 deletions test/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,6 @@ describe('options', () => {
},
],
},
useLocalIp: {
success: [false],
failure: [''],
},
};

Object.keys(cases).forEach((key) => {
Expand Down
14 changes: 0 additions & 14 deletions test/server/utils/createDomain.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const webpack = require('webpack');
const internalIp = require('internal-ip');
const Server = require('../../../lib/Server');
const createDomain = require('../../../lib/utils/createDomain');
const [port1, port2] = require('../../ports-map').createDomain;
Expand Down Expand Up @@ -89,19 +88,6 @@ describe('createDomain', () => {
},
expected: [`https://myhost.test:${port2}`],
},
{
name: 'localIp',
options: {
useLocalIp: true,
port: port1,
},
expected: [
`http://${internalIp.v4.sync()}:${port1}`,
`https://localhost:${port1}`,
`https://127.0.0.1:${port1}`,
`https://[::1]:${port1}`,
],
},
];

tests.forEach((test) => {
Expand Down