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 1 commit
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
20 changes: 17 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
7 changes: 7 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,13 @@ 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 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
15 changes: 15 additions & 0 deletions test/cli/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ 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 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/",
},
};