diff --git a/lib/Server.js b/lib/Server.js index 5cdeccffaf..bbdf4ef543 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -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( diff --git a/lib/utils/addEntries.js b/lib/utils/addEntries.js index fb5e542e75..6cef4c8427 100644 --- a/lib/utils/addEntries.js +++ b/lib/utils/addEntries.js @@ -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 @@ -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); diff --git a/lib/utils/updateCompiler.js b/lib/utils/updateCompiler.js index 9f701e81b4..9387bb42ba 100644 --- a/lib/utils/updateCompiler.js +++ b/lib/utils/updateCompiler.js @@ -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) { @@ -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); diff --git a/test/server/utils/addEntries.test.js b/test/server/utils/addEntries.test.js index 608bd61a30..2787a84804 100644 --- a/test/server/utils/addEntries.test.js +++ b/test/server/utils/addEntries.test.js @@ -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( @@ -29,7 +35,7 @@ describe('addEntries util', () => { const devServerOptions = {}; - addEntries(webpackOptions, devServerOptions); + addEntries(webpackOptions, devServerOptions, null, log); expect(webpackOptions.entry.length).toEqual(3); expect( @@ -49,7 +55,7 @@ describe('addEntries util', () => { const devServerOptions = {}; - addEntries(webpackOptions, devServerOptions); + addEntries(webpackOptions, devServerOptions, null, log); expect(webpackOptions.entry.foo.length).toEqual(2); @@ -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'); @@ -81,7 +87,7 @@ describe('addEntries util', () => { }; const devServerOptions = {}; - addEntries(webpackOptions, devServerOptions); + addEntries(webpackOptions, devServerOptions, null, log); expect(typeof webpackOptions.entry).toEqual('function'); @@ -113,7 +119,7 @@ describe('addEntries util', () => { const devServerOptions = {}; - addEntries(webpackOptions, devServerOptions); + addEntries(webpackOptions, devServerOptions, null, log); expect(typeof webpackOptions.entry).toEqual('function'); @@ -143,7 +149,7 @@ describe('addEntries util', () => { hot: true, }; - addEntries(webpackOptions, devServerOptions); + addEntries(webpackOptions, devServerOptions, null, log); const hotClientScript = webpackOptions.entry.app[1]; @@ -164,7 +170,7 @@ describe('addEntries util', () => { hotOnly: true, }; - addEntries(webpackOptions, devServerOptions); + addEntries(webpackOptions, devServerOptions, null, log); const hotClientScript = webpackOptions.entry.app[1]; @@ -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(); }); @@ -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([]); }); @@ -200,7 +206,7 @@ describe('addEntries util', () => { }); const devServerOptions = {}; - addEntries(webpackOptions, devServerOptions); + addEntries(webpackOptions, devServerOptions, null, log); expect(webpackOptions.plugins).toEqual([existingPlugin1, existingPlugin2]); }); @@ -212,7 +218,7 @@ describe('addEntries util', () => { }); const devServerOptions = { hot: true }; - addEntries(webpackOptions, devServerOptions); + addEntries(webpackOptions, devServerOptions, null, log); expect(webpackOptions.plugins).toEqual([ existingPlugin, @@ -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(), @@ -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(), @@ -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); @@ -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); }); @@ -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) => { @@ -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) => { @@ -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) => { @@ -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]; @@ -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); + }); });