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 5 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
26 changes: 23 additions & 3 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2899,7 +2899,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 @@ -2914,8 +2917,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 @@ -3062,6 +3076,12 @@ class Server {
`Broadcasting "${bonjourProtocol}" with subtype of "webpack" via ZeroConf DNS (Bonjour)`,
);
}

if (typeof compilerOptions.output.publicPath === "function") {
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
15 changes: 15 additions & 0 deletions test/cli/__snapshots__/basic.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ exports[`basic basic should accept the promise function of webpack.config.js: st
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
`;

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 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>/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`"server" CLI options should work using "--no-server-options-request-cert" 1`] = `
"<i> [webpack-dev-server] Generating SSL certificate...
<i> [webpack-dev-server] SSL certificate: <cwd>/node_modules/.cache/webpack-dev-server/server.pem
"<i> [webpack-dev-server] SSL certificate: <cwd>/node_modules/.cache/webpack-dev-server/server.pem
<i> [webpack-dev-server] Project is running at:
<i> Loopback: https://localhost:<port>/, https://<ip-v4>:<port>/, https://[<ip-v6>]:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): https://<ip-v4>:<port>/
Expand Down
30 changes: 30 additions & 0 deletions test/cli/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,36 @@ describe("basic", () => {
expect(normalizeStderr(stderr, { ipv6: true })).toMatchSnapshot("stderr");
});

it("should respect output.publicPath config option in URL's output", async () => {
const { exitCode, stderr } = await testBin([
"--config",
path.resolve(
__dirname,
"../fixtures/cli-output-public-path-config/webpack.config.js",
),
"--port",
port,
]);

expect(exitCode).toEqual(0);
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
});

it("should show info message when output.publicPath is function", async () => {
const { exitCode, stderr } = await testBin([
"--config",
path.resolve(
__dirname,
"../fixtures/cli-output-public-path-function-config/webpack.config.js",
),
"--port",
port,
]);

expect(exitCode).toEqual(0);
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
});

it("should work using multi compiler mode", async () => {
const { exitCode, stderr } = await testBin([
"--config",
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/cli-output-public-path-config/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

console.log("i am foo!");
11 changes: 11 additions & 0 deletions test/fixtures/cli-output-public-path-config/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

const { join } = require("path");

module.exports = {
mode: "development",
entry: join(__dirname, "foo.js"),
output: {
publicPath: "/foo/",
},
};
3 changes: 3 additions & 0 deletions test/fixtures/cli-output-public-path-function-config/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

console.log("i am foo!");
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

const { join } = require("path");

module.exports = {
mode: "development",
entry: join(__dirname, "foo.js"),
output: {
publicPath: () => "/any-public-path",
},
};