Skip to content

Commit 9e1279d

Browse files
committed
feat: publicPath output
1 parent 3d27615 commit 9e1279d

File tree

8 files changed

+97
-5
lines changed

8 files changed

+97
-5
lines changed

lib/Server.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2918,7 +2918,10 @@ class Server {
29182918
return msg;
29192919
},
29202920
};
2921-
const useColor = getColorsOption(this.getCompilerOptions());
2921+
2922+
const compilerOptions = this.getCompilerOptions();
2923+
2924+
const useColor = getColorsOption(compilerOptions);
29222925

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

@@ -2933,8 +2936,19 @@ class Server {
29332936
* @param {string} newHostname new hostname
29342937
* @returns {string} prettified URL
29352938
*/
2936-
const prettyPrintURL = (newHostname) =>
2937-
url.format({ protocol, hostname: newHostname, port, pathname: "/" });
2939+
const prettyPrintURL = (newHostname) => {
2940+
const publicPath = compilerOptions.output.publicPath;
2941+
2942+
return url.format({
2943+
protocol,
2944+
hostname: newHostname,
2945+
port,
2946+
pathname:
2947+
typeof publicPath === "function" || publicPath === "auto"
2948+
? "/"
2949+
: publicPath,
2950+
});
2951+
};
29382952

29392953
let host;
29402954
let localhost;
@@ -3081,6 +3095,12 @@ class Server {
30813095
`Broadcasting "${bonjourProtocol}" with subtype of "webpack" via ZeroConf DNS (Bonjour)`,
30823096
);
30833097
}
3098+
3099+
if (typeof compilerOptions.output.publicPath === "function") {
3100+
this.logger.info(
3101+
`You probably have a custom public path, please navigate in your browser to the point that you required`,
3102+
);
3103+
}
30843104
}
30853105

30863106
/**

test/cli/__snapshots__/basic.test.js.snap.webpack5

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ exports[`basic basic should accept the promise function of webpack.config.js: st
88
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
99
`;
1010

11+
exports[`basic basic should respect output.publicPath config option in URL's output: stderr 1`] = `
12+
"<i> [webpack-dev-server] Project is running at:
13+
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
14+
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/foo/
15+
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
16+
`;
17+
18+
exports[`basic basic should show info message when output.publicPath is function: stderr 1`] = `
19+
"<i> [webpack-dev-server] Project is running at:
20+
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/
21+
<i> [webpack-dev-server] On Your Network (IPv4): http://<ip-v4>:<port>/
22+
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory
23+
<i> [webpack-dev-server] You probably have a custom public path, please navigate in your browser to the point that you required"
24+
`;
25+
1126
exports[`basic basic should work using "--host localhost --port <port>": stderr 1`] = `
1227
"<i> [webpack-dev-server] Project is running at:
1328
<i> Loopback: http://localhost:<port>/, http://<ip-v4>:<port>/, http://[<ip-v6>]:<port>/

test/cli/__snapshots__/server-option.test.js.snap.webpack5

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`"server" CLI options should work using "--no-server-options-request-cert" 1`] = `
4-
"<i> [webpack-dev-server] Generating SSL certificate...
5-
<i> [webpack-dev-server] SSL certificate: <cwd>/node_modules/.cache/webpack-dev-server/server.pem
4+
"<i> [webpack-dev-server] SSL certificate: <cwd>/node_modules/.cache/webpack-dev-server/server.pem
65
<i> [webpack-dev-server] Project is running at:
76
<i> Loopback: https://localhost:<port>/, https://<ip-v4>:<port>/, https://[<ip-v6>]:<port>/
87
<i> [webpack-dev-server] On Your Network (IPv4): https://<ip-v4>:<port>/

test/cli/basic.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,36 @@ describe("basic", () => {
5959
expect(normalizeStderr(stderr, { ipv6: true })).toMatchSnapshot("stderr");
6060
});
6161

62+
it("should respect output.publicPath config option in URL's output", async () => {
63+
const { exitCode, stderr } = await testBin([
64+
"--config",
65+
path.resolve(
66+
__dirname,
67+
"../fixtures/cli-output-public-path-config/webpack.config.js",
68+
),
69+
"--port",
70+
port,
71+
]);
72+
73+
expect(exitCode).toEqual(0);
74+
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
75+
});
76+
77+
it("should show info message when output.publicPath is function", async () => {
78+
const { exitCode, stderr } = await testBin([
79+
"--config",
80+
path.resolve(
81+
__dirname,
82+
"../fixtures/cli-output-public-path-function-config/webpack.config.js",
83+
),
84+
"--port",
85+
port,
86+
]);
87+
88+
expect(exitCode).toEqual(0);
89+
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
90+
});
91+
6292
it("should work using multi compiler mode", async () => {
6393
const { exitCode, stderr } = await testBin([
6494
"--config",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
console.log("i am foo!");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
3+
const { join } = require("path");
4+
5+
module.exports = {
6+
mode: "development",
7+
entry: join(__dirname, "foo.js"),
8+
output: {
9+
publicPath: "/foo/",
10+
},
11+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
console.log("i am foo!");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
3+
const { join } = require("path");
4+
5+
module.exports = {
6+
mode: "development",
7+
entry: join(__dirname, "foo.js"),
8+
output: {
9+
publicPath: () => "/any-public-path",
10+
},
11+
};

0 commit comments

Comments
 (0)