diff --git a/lib/Server.js b/lib/Server.js index 3c3c6c8c2c..0a97900515 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -84,7 +84,7 @@ class Server { // eslint-disable-next-line no-shadow compilers.forEach((compiler) => { - new DevServerPlugin(this.options).apply(compiler); + new DevServerPlugin(this.options, this.logger).apply(compiler); }); } diff --git a/lib/utils/DevServerPlugin.js b/lib/utils/DevServerPlugin.js index e92c1ffda1..bf3a94d857 100644 --- a/lib/utils/DevServerPlugin.js +++ b/lib/utils/DevServerPlugin.js @@ -10,9 +10,11 @@ const EntryPlugin = webpack.EntryPlugin; class DevServerPlugin { /** * @param {?Object} options - Dev-Server options + * @param {?Object} logger - Dev-Server logger */ - constructor(options) { + constructor(options, logger) { this.options = options; + this.logger = logger; } /** @@ -190,6 +192,13 @@ class DevServerPlugin { additionalEntries.push(hotEntry); } + if (!isWebTarget) { + this.logger.info(`A non-web target was selected in dev server config.`); + if (this.options.liveReload) { + this.logger.warn(`Live reload will not work with a non-web target.`); + } + } + // use a hook to add entries if available if (EntryPlugin) { for (const additionalEntry of additionalEntries) { diff --git a/test/e2e/DevServer.test.js b/test/e2e/DevServer.test.js index 87e14c269d..eaaab5a80f 100644 --- a/test/e2e/DevServer.test.js +++ b/test/e2e/DevServer.test.js @@ -1,6 +1,6 @@ 'use strict'; -const { testBin } = require('../helpers/test-bin'); +const { testBin, normalizeStderr } = require('../helpers/test-bin'); const isWebpack5 = require('../helpers/isWebpack5'); describe('DevServer', () => { @@ -124,4 +124,20 @@ describe('DevServer', () => { .catch(done); } ); + + it('should show a warning for live reloading with non-web target', (done) => { + testBin( + '--target node --live-reload', + './test/fixtures/dev-server/default-config.js' + ) + .then((output) => { + expect(output.exitCode).toEqual(0); + expect( + normalizeStderr(output.stderr, { ipv6: true }) + ).toMatchSnapshot(); + + done(); + }) + .catch(done); + }); }); diff --git a/test/e2e/__snapshots__/DevServer.test.js.snap.webpack4 b/test/e2e/__snapshots__/DevServer.test.js.snap.webpack4 new file mode 100644 index 0000000000..7071fd653e --- /dev/null +++ b/test/e2e/__snapshots__/DevServer.test.js.snap.webpack4 @@ -0,0 +1,11 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`DevServer should show a warning for live reloading with non-web target 1`] = ` +" [webpack-dev-server] A non-web target was selected in dev server config. + [webpack-dev-server] Live reload will not work with a non-web target. + [webpack-dev-server] Project is running at: + [webpack-dev-server] Loopback: http://localhost:/ + [webpack-dev-server] On Your Network (IPv4): http://:/ + [webpack-dev-server] On Your Network (IPv6): http://[]:/ + [webpack-dev-server] Content not from webpack is served from '/public' directory" +`; diff --git a/test/e2e/__snapshots__/DevServer.test.js.snap.webpack5 b/test/e2e/__snapshots__/DevServer.test.js.snap.webpack5 new file mode 100644 index 0000000000..7071fd653e --- /dev/null +++ b/test/e2e/__snapshots__/DevServer.test.js.snap.webpack5 @@ -0,0 +1,11 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`DevServer should show a warning for live reloading with non-web target 1`] = ` +" [webpack-dev-server] A non-web target was selected in dev server config. + [webpack-dev-server] Live reload will not work with a non-web target. + [webpack-dev-server] Project is running at: + [webpack-dev-server] Loopback: http://localhost:/ + [webpack-dev-server] On Your Network (IPv4): http://:/ + [webpack-dev-server] On Your Network (IPv6): http://[]:/ + [webpack-dev-server] Content not from webpack is served from '/public' directory" +`;