Skip to content

Commit

Permalink
feat(server): warning for non web target (webpack#2026)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristonitas committed Jul 5, 2019
1 parent 99192c8 commit d129a15
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Server {
);
}

updateCompiler(this.compiler, this.options);
updateCompiler(this.compiler, this.options, this.log);

// this.SocketServerImplementation is a class, so it must be instantiated before use
this.socketServerImplementation = getSocketServerImplementation(
Expand Down
8 changes: 7 additions & 1 deletion lib/utils/addEntries.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const webpack = require('webpack');
const createDomain = require('./createDomain');

function addEntries(config, options, server) {
function addEntries(config, options, server, log) {
if (options.inline !== false) {
// we're stubbing the app in this method as it's static and doesn't require
// a server to be supplied. createDomain requires an app with the
Expand Down Expand Up @@ -82,6 +82,12 @@ function addEntries(config, options, server) {
)
? [clientEntry]
: [];
if (!webTarget) {
log.warn(`Non web target selected in dev server config`);
if (config.liveReload !== false) {
log.error(`Live reload won't work with non web target`);
}
}

if (hotEntry && checkInject(options.injectHot, config, true)) {
additionalEntries.push(hotEntry);
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/updateCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const webpack = require('webpack');
const addEntries = require('./addEntries');
const getSocketClientPath = require('./getSocketClientPath');

function updateCompiler(compiler, options) {
function updateCompiler(compiler, options, log) {
if (options.inline !== false) {
const findHMRPlugin = (config) => {
if (!config.plugins) {
Expand Down Expand Up @@ -45,7 +45,7 @@ function updateCompiler(compiler, options) {
// the changes we are making to the compiler
// important: this relies on the fact that addEntries now
// prevents duplicate new entries.
addEntries(webpackConfig, options);
addEntries(webpackConfig, options, null, log);
compilers.forEach((compiler) => {
const config = compiler.options;
compiler.hooks.entryOption.call(config.context, config.entry);
Expand Down
74 changes: 53 additions & 21 deletions test/server/utils/addEntries.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ const configEntryAsFunction = require('./../../fixtures/entry-as-function/webpac

const normalize = (entry) => entry.split(path.sep).join('/');

const log = {
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};

describe('addEntries util', () => {
it('should adds devServer entry points to a single entry point', () => {
const webpackOptions = Object.assign({}, config);
const devServerOptions = {};

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

expect(webpackOptions.entry.length).toEqual(2);
expect(
Expand All @@ -29,7 +35,7 @@ describe('addEntries util', () => {

const devServerOptions = {};

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

expect(webpackOptions.entry.length).toEqual(3);
expect(
Expand All @@ -49,7 +55,7 @@ describe('addEntries util', () => {

const devServerOptions = {};

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

expect(webpackOptions.entry.foo.length).toEqual(2);

Expand All @@ -64,7 +70,7 @@ describe('addEntries util', () => {
const webpackOptions = {};
const devServerOptions = {};

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

expect(webpackOptions.entry.length).toEqual(2);
expect(webpackOptions.entry[1]).toEqual('./src');
Expand All @@ -81,7 +87,7 @@ describe('addEntries util', () => {
};
const devServerOptions = {};

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

expect(typeof webpackOptions.entry).toEqual('function');

Expand Down Expand Up @@ -113,7 +119,7 @@ describe('addEntries util', () => {

const devServerOptions = {};

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

expect(typeof webpackOptions.entry).toEqual('function');

Expand Down Expand Up @@ -143,7 +149,7 @@ describe('addEntries util', () => {
hot: true,
};

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

const hotClientScript = webpackOptions.entry.app[1];

Expand All @@ -164,7 +170,7 @@ describe('addEntries util', () => {
hotOnly: true,
};

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

const hotClientScript = webpackOptions.entry.app[1];

Expand All @@ -178,7 +184,7 @@ describe('addEntries util', () => {
const webpackOptions = Object.assign({}, config);
const devServerOptions = {};

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

expect('plugins' in webpackOptions).toBeFalsy();
});
Expand All @@ -187,7 +193,7 @@ describe('addEntries util', () => {
const webpackOptions = Object.assign({}, config, { plugins: [] });
const devServerOptions = {};

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

expect(webpackOptions.plugins).toEqual([]);
});
Expand All @@ -200,7 +206,7 @@ describe('addEntries util', () => {
});
const devServerOptions = {};

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

expect(webpackOptions.plugins).toEqual([existingPlugin1, existingPlugin2]);
});
Expand All @@ -212,7 +218,7 @@ describe('addEntries util', () => {
});
const devServerOptions = { hot: true };

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

expect(webpackOptions.plugins).toEqual([
existingPlugin,
Expand All @@ -224,7 +230,7 @@ describe('addEntries util', () => {
const webpackOptions = Object.assign({}, config);
const devServerOptions = { hotOnly: true };

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

expect(webpackOptions.plugins).toEqual([
new webpack.HotModuleReplacementPlugin(),
Expand All @@ -238,7 +244,7 @@ describe('addEntries util', () => {
});
const devServerOptions = { hot: true };

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

expect(webpackOptions.plugins).toEqual([
new webpack.HotModuleReplacementPlugin(),
Expand All @@ -250,8 +256,8 @@ describe('addEntries util', () => {
const webpackOptions = Object.assign({}, config);
const devServerOptions = { hot: true };

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);
addEntries(webpackOptions, devServerOptions, null, log);

expect(webpackOptions.entry.length).toEqual(3);

Expand All @@ -265,7 +271,7 @@ describe('addEntries util', () => {
const webpackOptions = Object.assign({}, configEntryAsFunction);
const devServerOptions = {};

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

expect(typeof webpackOptions.entry === 'function').toBe(true);
});
Expand All @@ -282,7 +288,12 @@ describe('addEntries util', () => {

const devServerOptions = {};

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

expect(log.warn).toHaveBeenCalledTimes(1);
expect(log.error).toHaveBeenCalledTimes(1);
log.warn.mockReset();
log.error.mockReset();

// eslint-disable-next-line no-shadow
webpackOptions.forEach((webpackOptions, index) => {
Expand Down Expand Up @@ -314,7 +325,12 @@ describe('addEntries util', () => {
injectClient: (compilerConfig) => compilerConfig.name === 'only-include',
};

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

expect(log.warn).toHaveBeenCalledTimes(1);
expect(log.error).toHaveBeenCalledTimes(1);
log.warn.mockReset();
log.error.mockReset();

// eslint-disable-next-line no-shadow
webpackOptions.forEach((webpackOptions, index) => {
Expand Down Expand Up @@ -347,7 +363,12 @@ describe('addEntries util', () => {
hot: true,
};

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

expect(log.warn).toHaveBeenCalledTimes(1);
expect(log.error).toHaveBeenCalledTimes(1);
log.warn.mockReset();
log.error.mockReset();

// eslint-disable-next-line no-shadow
webpackOptions.forEach((webpackOptions) => {
Expand All @@ -372,7 +393,12 @@ describe('addEntries util', () => {
hot: true,
};

addEntries(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions, null, log);

expect(log.warn).toHaveBeenCalledTimes(1);
expect(log.error).toHaveBeenCalledTimes(1);
log.warn.mockReset();
log.error.mockReset();

// node target should have the client runtime but not the hot runtime
const webWebpackOptions = webpackOptions[0];
Expand All @@ -396,4 +422,10 @@ describe('addEntries util', () => {

expect(normalize(nodeWebpackOptions.entry[1])).toEqual('./foo.js');
});

afterEach(() => {
expect(log.info).toHaveBeenCalledTimes(0);
expect(log.warn).toHaveBeenCalledTimes(0);
expect(log.error).toHaveBeenCalledTimes(0);
});
});

0 comments on commit d129a15

Please sign in to comment.