Skip to content

feat: print Dev Server URL on 'u' keypress to reduce terminal clutter #5489

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const path = require("path");
const url = require("url");
const util = require("util");
const readline = require("readline");
const fs = require("graceful-fs");
const ipaddr = require("ipaddr.js");
const { validate } = require("schema-utils");
Expand Down Expand Up @@ -309,6 +310,24 @@

const DEFAULT_ALLOWED_PROTOCOLS = /^(file|.+-extension):/i;

/**
* @param {(host: string) => string} prettyPrintURL
*/
function setupKeypressLogger(prettyPrintURL) {
readline.emitKeypressEvents(process.stdin);

Check warning on line 317 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L316-L317

Added lines #L316 - L317 were not covered by tests
if (process.stdin.isTTY) {
process.stdin.setRawMode(true);
process.stdin.on("keypress", (str, key) => {

Check warning on line 320 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L319-L320

Added lines #L319 - L320 were not covered by tests
if (key.name === "u") {
console.log(`🔗 Dev Server: ${prettyPrintURL("localhost")}`);

Check warning on line 322 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L322

Added line #L322 was not covered by tests
} else if (key.name === "q" || (key.ctrl && key.name === "c")) {
process.exit();

Check warning on line 324 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L324

Added line #L324 was not covered by tests
}
});
console.log("🔑 Press [u] to show Dev Server URL, [q] to quit");

Check warning on line 327 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L327

Added line #L327 was not covered by tests
}
}

/**
* @typedef {Object} BasicApplication
* @property {typeof useFn} use
Expand Down Expand Up @@ -2917,6 +2936,11 @@
const prettyPrintURL = (newHostname) =>
url.format({ protocol, hostname: newHostname, port, pathname: "/" });

// @ts-expect-error: we just added this option, TS types need updating
if (this.options.printUrlOnKeypress) {
setupKeypressLogger(prettyPrintURL);

Check warning on line 2941 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L2941

Added line #L2941 was not covered by tests
}

let host;
let localhost;
let loopbackIPv4;
Expand Down
8 changes: 8 additions & 0 deletions lib/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,11 @@
"description": "Allows to specify a port to use.",
"link": "https://webpack.js.org/configuration/dev-server/#devserverport"
},
"PrintUrlOnKeypress": {
"type": "boolean",
"default": false,
"description": "Only print the Dev Server URL when the user presses 'u' in the terminal."
},
"Proxy": {
"type": "array",
"items": {
Expand Down Expand Up @@ -976,6 +981,9 @@
"compress": {
"$ref": "#/definitions/Compress"
},
"printUrlOnKeypress": {
"$ref": "#/definitions/PrintUrlOnKeypress"
},
"devMiddleware": {
"$ref": "#/definitions/DevMiddleware"
},
Expand Down
8 changes: 8 additions & 0 deletions types/lib/Server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,11 @@ declare class Server<
description: string;
link: string;
};
PrintUrlOnKeypress: {
type: string;
default: boolean;
description: string;
};
Proxy: {
type: string;
items: {
Expand Down Expand Up @@ -1066,6 +1071,9 @@ declare class Server<
compress: {
$ref: string;
};
printUrlOnKeypress: {
$ref: string;
};
devMiddleware: {
$ref: string;
};
Expand Down
Loading