diff --git a/lib/Server.js b/lib/Server.js index 6e208128f6..c4efa87fa9 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -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); @@ -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" + ? "/" + : publicPath, + }); + }; let host; let localhost; @@ -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`, + ); + } } /** diff --git a/test/cli/__snapshots__/basic.test.js.snap.webpack5 b/test/cli/__snapshots__/basic.test.js.snap.webpack5 index a5e1d16264..e20c1a98dc 100644 --- a/test/cli/__snapshots__/basic.test.js.snap.webpack5 +++ b/test/cli/__snapshots__/basic.test.js.snap.webpack5 @@ -8,6 +8,21 @@ exports[`basic basic should accept the promise function of webpack.config.js: st [webpack-dev-server] Content not from webpack is served from '/public' directory" `; +exports[`basic basic should respect output.publicPath config option in URL's output: stderr 1`] = ` +" [webpack-dev-server] Project is running at: + Loopback: http://localhost:/, http://:/, http://[]:/ + [webpack-dev-server] On Your Network (IPv4): http://:/foo/ + [webpack-dev-server] Content not from webpack is served from '/public' directory" +`; + +exports[`basic basic should show info message when output.publicPath is function: stderr 1`] = ` +" [webpack-dev-server] Project is running at: + Loopback: http://localhost:/, http://:/, http://[]:/ + [webpack-dev-server] On Your Network (IPv4): http://:/ + [webpack-dev-server] Content not from webpack is served from '/public' directory + [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 ": stderr 1`] = ` " [webpack-dev-server] Project is running at: Loopback: http://localhost:/, http://:/, http://[]:/ diff --git a/test/cli/__snapshots__/server-option.test.js.snap.webpack5 b/test/cli/__snapshots__/server-option.test.js.snap.webpack5 index f3efb017ab..0a5aeb7f6e 100644 --- a/test/cli/__snapshots__/server-option.test.js.snap.webpack5 +++ b/test/cli/__snapshots__/server-option.test.js.snap.webpack5 @@ -1,8 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`"server" CLI options should work using "--no-server-options-request-cert" 1`] = ` -" [webpack-dev-server] Generating SSL certificate... - [webpack-dev-server] SSL certificate: /node_modules/.cache/webpack-dev-server/server.pem +" [webpack-dev-server] SSL certificate: /node_modules/.cache/webpack-dev-server/server.pem [webpack-dev-server] Project is running at: Loopback: https://localhost:/, https://:/, https://[]:/ [webpack-dev-server] On Your Network (IPv4): https://:/ diff --git a/test/cli/basic.test.js b/test/cli/basic.test.js index 98a5e33c79..15d51a1ad5 100644 --- a/test/cli/basic.test.js +++ b/test/cli/basic.test.js @@ -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", diff --git a/test/fixtures/cli-output-public-path-config/foo.js b/test/fixtures/cli-output-public-path-config/foo.js new file mode 100644 index 0000000000..5940e9197c --- /dev/null +++ b/test/fixtures/cli-output-public-path-config/foo.js @@ -0,0 +1,3 @@ +"use strict"; + +console.log("i am foo!"); diff --git a/test/fixtures/cli-output-public-path-config/webpack.config.js b/test/fixtures/cli-output-public-path-config/webpack.config.js new file mode 100644 index 0000000000..ab340d5ea1 --- /dev/null +++ b/test/fixtures/cli-output-public-path-config/webpack.config.js @@ -0,0 +1,11 @@ +"use strict"; + +const { join } = require("path"); + +module.exports = { + mode: "development", + entry: join(__dirname, "foo.js"), + output: { + publicPath: "/foo/", + }, +}; diff --git a/test/fixtures/cli-output-public-path-function-config/foo.js b/test/fixtures/cli-output-public-path-function-config/foo.js new file mode 100644 index 0000000000..5940e9197c --- /dev/null +++ b/test/fixtures/cli-output-public-path-function-config/foo.js @@ -0,0 +1,3 @@ +"use strict"; + +console.log("i am foo!"); diff --git a/test/fixtures/cli-output-public-path-function-config/webpack.config.js b/test/fixtures/cli-output-public-path-function-config/webpack.config.js new file mode 100644 index 0000000000..4567b9ae39 --- /dev/null +++ b/test/fixtures/cli-output-public-path-function-config/webpack.config.js @@ -0,0 +1,11 @@ +"use strict"; + +const { join } = require("path"); + +module.exports = { + mode: "development", + entry: join(__dirname, "foo.js"), + output: { + publicPath: () => "/any-public-path", + }, +};