Skip to content

Commit

Permalink
fix: show warning for non-web targets
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed May 24, 2021
1 parent 257e2eb commit 71a9eb9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down
11 changes: 10 additions & 1 deletion lib/utils/DevServerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 17 additions & 1 deletion test/e2e/DevServer.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
11 changes: 11 additions & 0 deletions test/e2e/__snapshots__/DevServer.test.js.snap.webpack4
Original file line number Diff line number Diff line change
@@ -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`] = `
"<i> [webpack-dev-server] A non-web target was selected in dev server config.
<w> [webpack-dev-server] Live reload will not work with a non-web target.
<i> [webpack-dev-server] Project is running at:
<i> [webpack-dev-server] Loopback: http://localhost:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<network-ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<network-ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
`;
11 changes: 11 additions & 0 deletions test/e2e/__snapshots__/DevServer.test.js.snap.webpack5
Original file line number Diff line number Diff line change
@@ -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`] = `
"<i> [webpack-dev-server] A non-web target was selected in dev server config.
<w> [webpack-dev-server] Live reload will not work with a non-web target.
<i> [webpack-dev-server] Project is running at:
<i> [webpack-dev-server] Loopback: http://localhost:<port>/
<i> [webpack-dev-server] On Your Network (IPv4): http://<network-ip-v4>:<port>/
<i> [webpack-dev-server] On Your Network (IPv6): http://[<network-ip-v6>]:<port>/
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/public' directory"
`;

0 comments on commit 71a9eb9

Please sign in to comment.