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 Mar 19, 2021
1 parent 728effc commit 43fca8f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
8 changes: 8 additions & 0 deletions lib/Server.js
Expand Up @@ -33,6 +33,7 @@ const getColorsOption = require('./utils/getColorsOption');
const setupExitSignals = require('./utils/setupExitSignals');
const findPort = require('./utils/findPort');
const schema = require('./options.json');
const isWebTarget = require('./utils/isWebTarget');

if (!process.env.WEBPACK_SERVE) {
process.env.WEBPACK_SERVE = true;
Expand Down Expand Up @@ -662,6 +663,13 @@ class Server {
);
}

if (!isWebTarget(this.compiler.options)) {
this.logger.info(`A non-web target was selected in dev server config.`);
if (this.options.liveReload) {
this.logger.info(`Live reload will not work with a non-web target.`);
}
}

if (this.options.open || this.options.openPage) {
runOpen(localUrlForTerminal, this.options, this.logger);
}
Expand Down
16 changes: 2 additions & 14 deletions lib/utils/DevServerPlugin.js
Expand Up @@ -3,6 +3,7 @@
const webpack = require('webpack');
const createDomain = require('./createDomain');
const getSocketClientPath = require('./getSocketClientPath');
const isWebTarget = require('./isWebTarget');

// @ts-ignore
const EntryPlugin = webpack.EntryPlugin;
Expand Down Expand Up @@ -137,24 +138,11 @@ class DevServerPlugin {

compilerOptions.plugins = compilerOptions.plugins || [];

/** @type {boolean} */
const isWebTarget = compilerOptions.externalsPresets
? compilerOptions.externalsPresets.web
: [
'web',
'webworker',
'electron-renderer',
'node-webkit',
// eslint-disable-next-line no-undefined
undefined,
null,
].includes(compilerOptions.target);

/** @type {Entry} */
const additionalEntries = checkInject(
options.injectClient,
compilerOptions,
isWebTarget
isWebTarget(compilerOptions)
)
? [clientEntry]
: [];
Expand Down
16 changes: 16 additions & 0 deletions lib/utils/isWebTarget.js
@@ -0,0 +1,16 @@
'use strict';

const isWebTarget = (compilerOptions) =>
compilerOptions.externalsPresets
? compilerOptions.externalsPresets.web
: [
'web',
'webworker',
'electron-renderer',
'node-webkit',
// eslint-disable-next-line no-undefined
undefined,
null,
].includes(compilerOptions.target);

module.exports = isWebTarget;
14 changes: 14 additions & 0 deletions test/cli/cli.test.js
Expand Up @@ -165,6 +165,20 @@ describe('CLI', () => {
});
});

it('should show a warning for live reloading with non-web target', (done) => {
testBin('--target node --live-reload')
.then((output) => {
expect(output.stderr).toContain(
'A non-web target was selected in dev server config'
);
expect(output.stderr).toContain(
'Live reload will not work with a non-web target'
);
done();
})
.catch(done);
});

it('should log static', (done) => {
testBin(
'--no-color',
Expand Down

0 comments on commit 43fca8f

Please sign in to comment.