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 os = require("os");
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 @@ function useFn(route, fn) {

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

/**
* @param {(host: string) => string} prettyPrintURL
*/
function setupKeypressLogger(prettyPrintURL) {
readline.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) {
process.stdin.setRawMode(true);
process.stdin.on("keypress", (str, key) => {
if (key.name === "u") {
console.log(`🔗 Dev Server: ${prettyPrintURL("localhost")}`);
} else if (key.name === "q" || (key.ctrl && key.name === "c")) {
process.exit();
}
});
console.log("🔑 Press [u] to show Dev Server URL, [q] to quit");
}
}

/**
* @typedef {Object} BasicApplication
* @property {typeof useFn} use
Expand Down Expand Up @@ -2917,6 +2936,11 @@ class Server {
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);
}

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