Skip to content

feat: publicPath output #5434

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 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
31 changes: 28 additions & 3 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2868,7 +2868,10 @@ class Server {
return msg;
},
};
const useColor = getColorsOption(this.getCompilerOptions());

const compilerOptions = this.getCompilerOptions();

const useColor = getColorsOption(compilerOptions);

const server = /** @type {S} */ (this.server);

Expand All @@ -2883,8 +2886,19 @@ class Server {
* @param {string} newHostname
* @returns {string}
*/
const prettyPrintURL = (newHostname) =>
url.format({ protocol, hostname: newHostname, port, pathname: "/" });
const prettyPrintURL = (newHostname) => {
const publicPath = compilerOptions.output.publicPath;

return url.format({
protocol,
hostname: newHostname,
port,
pathname:
typeof publicPath === "function" || publicPath === "auto"
Copy link
Member

Choose a reason for hiding this comment

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

We need respect function too... But yeah, we known about it only after compilation, so maybe we can improve our message and write - something like you can have custom public path, please navigate in your browsers to the point that you required (just an example, feel free to improve it)

Copy link
Author

Choose a reason for hiding this comment

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

@alexander-akait I think adding an info message if public path is auto or function would be a good idea, can you check this commit?

? "/"
: publicPath,
});
};

let host;
let localhost;
Expand Down Expand Up @@ -3031,6 +3045,17 @@ class Server {
`Broadcasting "${bonjourProtocol}" with subtype of "webpack" via ZeroConf DNS (Bonjour)`,
);
}

const publicPath = compilerOptions.output.publicPath;

const isOnTheFlyDefinedPublicPath =
typeof publicPath === "function" || publicPath === "auto";

if (isOnTheFlyDefinedPublicPath) {
this.logger.info(
`You probably have a custom public path, please navigate in your browser to the point that you required`,
);
}
Copy link
Member

Choose a reason for hiding this comment

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

We should output this only for function, auto is like /, so it is unnecessary

Copy link
Author

Choose a reason for hiding this comment

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

@alexander-akait

Hmm, webpack documentation says that the "auto" value allows you to automatically infer the public path value based on the values ​​in the scripts. Are you sure that we should keep default behavior for "auto" value?

https://webpack.js.org/guides/public-path/#automatic-publicpath

Copy link
Member

Choose a reason for hiding this comment

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

Yes, you are right, but mostly it used only for /, so I think it can be a little bit spammy

Copy link
Author

@adubrouski adubrouski Apr 2, 2025

Choose a reason for hiding this comment

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

Ok, I fixed it in this commit. Can you check?

}

/**
Expand Down
33 changes: 30 additions & 3 deletions test/cli/__snapshots__/basic.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,56 @@ exports[`basic basic should accept the promise function of webpack.config.js: st
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`basic basic should respect output.publicPath config option in URL's output: stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/foo/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
`;

exports[`basic basic should show info message when output.publicPath equals 'auto': stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`basic basic should show info message when output.publicPath is function: stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`basic basic should work using "--host localhost --port <port>": stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`basic basic should work using multi compiler mode: stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`basic basic should work: stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`basic should output help should generate correct cli flags 1`] = `
Expand Down
9 changes: 6 additions & 3 deletions test/cli/__snapshots__/bonjour-option.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ exports[`"bonjour" CLI option should work using "--bonjour and --server-type=htt
<i> [webpack-dev-server] On Your Network (IPv4): https://<ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): https://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] Broadcasting "https" with subtype of "webpack" via ZeroConf DNS (Bonjour)"
<i> [webpack-dev-server] Broadcasting "https" with subtype of "webpack" via ZeroConf DNS (Bonjour)
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`"bonjour" CLI option should work using "--bonjour" 1`] = `
Expand All @@ -17,13 +18,15 @@ exports[`"bonjour" CLI option should work using "--bonjour" 1`] = `
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] Broadcasting "http" with subtype of "webpack" via ZeroConf DNS (Bonjour)"
<i> [webpack-dev-server] Broadcasting "http" with subtype of "webpack" via ZeroConf DNS (Bonjour)
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`"bonjour" CLI option should work using "--no-bonjour" 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;
15 changes: 10 additions & 5 deletions test/cli/__snapshots__/colors.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,42 @@ exports[`colors should work and do not use colors using configuration with disab
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`colors should work do not use colors using "--no-color": stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`colors should work use colors by default: stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`colors should work use colors using "--color": stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`colors should work use colors using configuration with enabled colors: stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ exports[`"historyApiFallback" CLI option should work using "--history-api-fallba
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] 404s will fallback to '/index.html'"
<i> [webpack-dev-server] 404s will fallback to '/index.html'
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`"historyApiFallback" CLI option should work using "--no-history-api-fallback" 1`] = `
Expand Down
27 changes: 18 additions & 9 deletions test/cli/__snapshots__/host-option.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,64 @@ exports[`"host" CLI option should work using "--host ::" (IPv6): stderr 1`] = `
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`"host" CLI option should work using "--host ::1" (IPv6): stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`"host" CLI option should work using "--host ::1" (IPv6): stderr 2`] = `
"<i> [webpack-dev-server] Project is running at:
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`"host" CLI option should work using "--host <IPv4>": stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`"host" CLI option should work using "--host 0.0.0.0" (IPv4): stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`"host" CLI option should work using "--host 127.0.0.1" (IPv4): stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`"host" CLI option should work using "--host local-ip": stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`"host" CLI option should work using "--host local-ipv4": stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`"host" CLI option should work using "--host localhost": stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;
6 changes: 4 additions & 2 deletions test/cli/__snapshots__/ipc-option.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

exports[`"ipc" CLI option should work using "--ipc": stderr 1`] = `
"<i> [webpack-dev-server] Project is running at: "<tmp>/webpack-dev-server.sock"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`"ipc" CLI option should work using "--ipc=<string>": stderr 1`] = `
"<i> [webpack-dev-server] Project is running at: "<tmp>/webpack-dev-server.cli.sock"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;
6 changes: 4 additions & 2 deletions test/cli/__snapshots__/port-option.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ exports[`"port" CLI option should work using "--port <string>": stderr 1`] = `
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;

exports[`"port" CLI option should work using "--port auto": stderr 1`] = `
"<i> [webpack-dev-server] Project is running at:
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
`;
Loading