Skip to content

Commit 14d77a5

Browse files
TroyHistedshellscape
authored andcommitted
Adding page argument to the Open option (#917)
* Adding page argument to the Open option * Changing open argv comparison to be more explicite * Adding open-page cli arg * Making open-page require a value * Adjusting openPage cli description
1 parent 2ca97dd commit 14d77a5

File tree

7 files changed

+42
-4
lines changed

7 files changed

+42
-4
lines changed

bin/webpack-dev-server.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ yargs.options({
9191
type: "boolean",
9292
describe: "Open default browser"
9393
},
94+
"open-page": {
95+
type: "string",
96+
describe: "Open default browser with the specified page",
97+
requiresArg: true,
98+
},
9499
"color": {
95100
type: "boolean",
96101
alias: "colors",
@@ -312,8 +317,10 @@ function processOptions(wpOpt) {
312317
if(argv["compress"])
313318
options.compress = true;
314319

315-
if(argv["open"])
320+
if(argv["open"] || argv["open-page"]) {
316321
options.open = true;
322+
options.openPage = argv["open-page"] || "";
323+
}
317324

318325
// Kind of weird, but ensures prior behavior isn't broken in cases
319326
// that wouldn't throw errors. E.g. both argv.port and options.port
@@ -423,7 +430,7 @@ function reportReadiness(uri, options) {
423430
if(options.historyApiFallback)
424431
console.log(`404s will fallback to ${colorInfo(useColor, options.historyApiFallback.index || "/index.html")}`);
425432
if(options.open) {
426-
open(uri).catch(function() {
433+
open(uri + options.openPage).catch(function() {
427434
console.log("Unable to open browser. If you are running in a headless environment, please do not use the open flag.");
428435
});
429436
}

examples/cli-open-page/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# CLI - OPEN-PAGE
2+
3+
```shell
4+
node ../../bin/webpack-dev-server.js --open-page example.html#page1
5+
```
6+
7+
Some applications may consist of multiple web pages. During development it may be useful to directly open a particular page. The page to open may be specified as the argument to the `open-page` option.
8+
9+
## What should happen
10+
11+
The script should open `http://localhost:8080/example.html#page1`. In the app you should see "It's working."

examples/cli-open-page/app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if(window.location.href.endsWith("example.html#page1")) {
2+
document.write("It's working.");
3+
}

examples/cli-open-page/example.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script src="bundle.js" type="text/javascript" charset="utf-8"></script>
5+
</head>
6+
<body>
7+
<h1>Example: CLI - open-page</h1>
8+
</body>
9+
</html>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
context: __dirname,
3+
entry: "./app.js",
4+
}

lib/optionsSchema.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,13 @@
192192
"type": "boolean"
193193
},
194194
"open": {
195-
"description": "Let the CLI open your browser with the URL.",
195+
"description": "Let the CLI open your browser.",
196196
"type": "boolean"
197197
},
198+
"openPage": {
199+
"description": "Let the CLI open your browser to a specific page on the site.",
200+
"type": "string"
201+
},
198202
"features": {
199203
"description": "The order of which the features will be triggered.",
200204
"items": {

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?, host?, allowedHosts?, filename?, publicPath?, port?, socket?, " +
5252
"watchOptions?, headers?, clientLogLevel?, overlay?, key?, cert?, ca?, pfx?, pfxPassphrase?, " +
53-
"inline?, disableHostCheck?, public?, https?, contentBase?, watchContentBase?, open?, features?, " +
53+
"inline?, disableHostCheck?, public?, https?, contentBase?, watchContentBase?, open?, openPage?, features?, " +
5454
"compress?, proxy?, historyApiFallback?, staticOptions?, setup?, stats?, reporter?, " +
5555
"noInfo?, quiet?, serverSideRender?, index?, log?, warn? }"
5656
]

0 commit comments

Comments
 (0)