Skip to content

Commit 7b3a42a

Browse files
sliweyshellscape
authored andcommitted
Add 'lan' option (modify the option name to ‘useLocalIp’ for more semantic) (#901)
* Add 'lan' option Let the browser open with local IP. * Add 'lan' option Let the browser open with local IP. * extract function getLocalIP and add some tests. * Change the option name to 'useLocalIp' and use 'internal-ip' to get local ip. * Fix confilcts.
1 parent 8d5f252 commit 7b3a42a

File tree

5 files changed

+15
-2
lines changed

5 files changed

+15
-2
lines changed

bin/webpack-dev-server.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ yargs.options({
9696
type: "boolean",
9797
describe: "Open default browser"
9898
},
99+
"useLocalIp": {
100+
type: "boolean",
101+
describe: "Open default browser with local IP"
102+
},
99103
"open-page": {
100104
type: "string",
101105
describe: "Open default browser with the specified page",
@@ -330,6 +334,9 @@ function processOptions(wpOpt) {
330334
options.openPage = argv["open-page"] || "";
331335
}
332336

337+
if(argv["useLocalIp"])
338+
options.useLocalIp = true;
339+
333340
// Kind of weird, but ensures prior behavior isn't broken in cases
334341
// that wouldn't throw errors. E.g. both argv.port and options.port
335342
// were specified, but since argv.port is 8080, options.port will be

lib/optionsSchema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@
199199
"description": "Let the CLI open your browser.",
200200
"type": "boolean"
201201
},
202+
"useLocalIp": {
203+
"description": "Let the browser open with your local IP.",
204+
"type": "boolean"
205+
},
202206
"openPage": {
203207
"description": "Let the CLI open your browser to a specific page on the site.",
204208
"type": "string"

lib/util/createDomain.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
"use strict";
22
const url = require("url");
3+
const internalIp = require("internal-ip");
34

45
module.exports = function createDomain(options) {
56
const protocol = options.https ? "https" : "http";
67

78
// the formatted domain (url without path) of the webpack server
89
return options.public ? `${protocol}://${options.public}` : url.format({
910
protocol: protocol,
10-
hostname: options.host,
11+
hostname: options.useLocalIp ? internalIp.v4() : options.host,
1112
port: options.socket ? 0 : options.port.toString()
1213
});
1314
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"express": "^4.13.3",
1717
"html-entities": "^1.2.0",
1818
"http-proxy-middleware": "~0.17.4",
19+
"internal-ip": "^1.2.0",
1920
"opn": "4.0.2",
2021
"portfinder": "^1.0.9",
2122
"selfsigned": "^1.9.1",

test/Validation.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe("Validation", function() {
5050
" - configuration has an unknown property 'asdf'. These properties are valid:",
5151
" object { hot?, hotOnly?, lazy?, bonjour?, host?, allowedHosts?, filename?, publicPath?, port?, socket?, " +
5252
"watchOptions?, headers?, clientLogLevel?, overlay?, key?, cert?, ca?, pfx?, pfxPassphrase?, " +
53-
"inline?, disableHostCheck?, public?, https?, contentBase?, watchContentBase?, open?, openPage?, features?, " +
53+
"inline?, disableHostCheck?, public?, https?, contentBase?, watchContentBase?, open?, useLocalIp?, openPage?, features?, " +
5454
"compress?, proxy?, historyApiFallback?, staticOptions?, setup?, stats?, reporter?, " +
5555
"noInfo?, quiet?, serverSideRender?, index?, log?, warn? }"
5656
]

0 commit comments

Comments
 (0)