From 2ee13abb96f10c536e02d09abb52377056db372a Mon Sep 17 00:00:00 2001 From: Michael Ciniawsky Date: Mon, 27 Aug 2018 19:56:19 +0200 Subject: [PATCH] refactor(lib/utils): move `createLog` && rename files (#1465) --- bin/webpack-dev-server.js | 11 +++--- lib/Server.js | 6 ++-- lib/createLog.js | 19 ---------- lib/util/createDomain.js | 23 ------------ .../addEntries.js} | 21 +++++++---- lib/utils/createDomain.js | 35 +++++++++++++++++++ lib/utils/createLogger.js | 26 ++++++++++++++ test/Entry.test.js | 18 +++++----- test/Util.test.js | 2 +- 9 files changed, 95 insertions(+), 66 deletions(-) delete mode 100644 lib/createLog.js delete mode 100644 lib/util/createDomain.js rename lib/{util/addDevServerEntrypoints.js => utils/addEntries.js} (81%) create mode 100644 lib/utils/createDomain.js create mode 100644 lib/utils/createLogger.js diff --git a/bin/webpack-dev-server.js b/bin/webpack-dev-server.js index 7736a03dd8..d11f573bc6 100755 --- a/bin/webpack-dev-server.js +++ b/bin/webpack-dev-server.js @@ -11,9 +11,9 @@ const path = require('path'); const importLocal = require('import-local'); const open = require('opn'); const portfinder = require('portfinder'); -const addDevServerEntrypoints = require('../lib/util/addDevServerEntrypoints'); -const createDomain = require('../lib/util/createDomain'); // eslint-disable-line -const createLog = require('../lib/createLog'); +const addEntries = require('../lib/utils/addEntries'); +const createDomain = require('../lib/utils/createDomain'); +const createLogger = require('../lib/utils/createLogger'); let server; @@ -384,8 +384,9 @@ function processOptions(webpackOptions) { } function startDevServer(webpackOptions, options) { - const log = createLog(options); - addDevServerEntrypoints(webpackOptions, options); + const log = createLogger(options); + + addEntries(webpackOptions, options); let compiler; try { diff --git a/lib/Server.js b/lib/Server.js index 5c84b093fa..49de8b015f 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -20,7 +20,7 @@ const sockjs = require('sockjs'); const spdy = require('spdy'); const webpack = require('webpack'); const webpackDevMiddleware = require('webpack-dev-middleware'); -const createLog = require('./createLog'); +const createLogger = require('./utils/createLogger'); const OptionsValidationError = require('./OptionsValidationError'); const optionsSchema = require('./optionsSchema.json'); @@ -29,7 +29,7 @@ const clientStats = { all: false, assets: true, warnings: true, errors: true, er function Server(compiler, options, _log) { // Default options if (!options) options = {}; - this.log = _log || createLog(options); + this.log = _log || createLogger(options); const validationErrors = webpack.validateSchema(optionsSchema, options); if (validationErrors.length) { @@ -703,6 +703,6 @@ Server.prototype.invalidate = function () { }; // Export this logic, so that other implementations, like task-runners can use it -Server.addDevServerEntrypoints = require('./util/addDevServerEntrypoints'); +Server.addDevServerEntrypoints = require('./utils/addEntries'); module.exports = Server; diff --git a/lib/createLog.js b/lib/createLog.js deleted file mode 100644 index 2de7662e52..0000000000 --- a/lib/createLog.js +++ /dev/null @@ -1,19 +0,0 @@ -'use strict'; - -const weblog = require('webpack-log'); - -module.exports = function createLog(options) { - let logLevel = options.logLevel || 'info'; - if (options.quiet === true) { - logLevel = 'silent'; - } - if (options.noInfo === true) { - logLevel = 'warn'; - } - - return weblog({ - level: logLevel, - name: 'wds', - timestamp: options.logTime - }); -}; diff --git a/lib/util/createDomain.js b/lib/util/createDomain.js deleted file mode 100644 index 00bc0ef5b7..0000000000 --- a/lib/util/createDomain.js +++ /dev/null @@ -1,23 +0,0 @@ -'use strict'; - -const url = require('url'); -const internalIp = require('internal-ip'); - - -module.exports = function createDomain(options, listeningApp) { - const protocol = options.https ? 'https' : 'http'; - const appPort = listeningApp ? listeningApp.address().port : 0; - const port = options.socket ? 0 : appPort; - const hostname = options.useLocalIp ? internalIp.v4() : options.host; - - // use explicitly defined public url (prefix with protocol if not explicitly given) - if (options.public) { - return /^[a-zA-Z]+:\/\//.test(options.public) ? `${options.public}` : `${protocol}://${options.public}`; - } - // the formatted domain (url without path) of the webpack server - return url.format({ - protocol, - hostname, - port - }); -}; diff --git a/lib/util/addDevServerEntrypoints.js b/lib/utils/addEntries.js similarity index 81% rename from lib/util/addDevServerEntrypoints.js rename to lib/utils/addEntries.js index e88bf801ea..699081224f 100644 --- a/lib/util/addDevServerEntrypoints.js +++ b/lib/utils/addEntries.js @@ -1,19 +1,22 @@ 'use strict'; -/* eslint no-param-reassign: 'off' */ - +/* eslint-disable + no-param-reassign, + space-before-function-paren +*/ const createDomain = require('./createDomain'); -module.exports = function addDevServerEntrypoints(webpackOptions, devServerOptions, listeningApp) { +function addEntries (webpackOptions, devServerOptions, server) { if (devServerOptions.inline !== false) { // we're stubbing the app in this method as it's static and doesn't require - // a listeningApp to be supplied. createDomain requires an app with the + // a server to be supplied. createDomain requires an app with the // address() signature. - const app = listeningApp || { + const app = server || { address() { return { port: devServerOptions.port }; } }; + const domain = createDomain(devServerOptions, app); const devClient = [`${require.resolve('../../client/')}?${domain}`]; @@ -27,13 +30,17 @@ module.exports = function addDevServerEntrypoints(webpackOptions, devServerOptio if (typeof entry === 'function') { return () => Promise.resolve(entry()).then(prependDevClient); } + if (typeof entry === 'object' && !Array.isArray(entry)) { const entryClone = {}; + Object.keys(entry).forEach((key) => { entryClone[key] = devClient.concat(entry[key]); }); + return entryClone; } + return devClient.concat(entry); }; @@ -41,4 +48,6 @@ module.exports = function addDevServerEntrypoints(webpackOptions, devServerOptio wpOpt.entry = prependDevClient(wpOpt.entry || './src'); }); } -}; +} + +module.exports = addEntries; diff --git a/lib/utils/createDomain.js b/lib/utils/createDomain.js new file mode 100644 index 0000000000..ba0adc993a --- /dev/null +++ b/lib/utils/createDomain.js @@ -0,0 +1,35 @@ +'use strict'; + +/* eslint-disable + no-nested-ternary, + multiline-ternary, + space-before-function-paren +*/ +const url = require('url'); +const ip = require('internal-ip'); + +function createDomain (options, server) { + const protocol = options.https ? 'https' : 'http'; + const hostname = options.useLocalIp ? ip.v4() : options.host; + + const port = options.socket + ? 0 + : server + ? server.address().port + : 0; + // use explicitly defined public url + // (prefix with protocol if not explicitly given) + if (options.public) { + return /^[a-zA-Z]+:\/\//.test(options.public) + ? `${options.public}` + : `${protocol}://${options.public}`; + } + // the formatted domain (url without path) of the webpack server + return url.format({ + protocol, + hostname, + port + }); +} + +module.exports = createDomain; diff --git a/lib/utils/createLogger.js b/lib/utils/createLogger.js new file mode 100644 index 0000000000..9eeb01e9b9 --- /dev/null +++ b/lib/utils/createLogger.js @@ -0,0 +1,26 @@ +'use strict'; + +/* eslint-disable + space-before-function-paren +*/ +const log = require('webpack-log'); + +function createLogger (options) { + let level = options.logLevel || 'info'; + + if (options.quiet === true) { + level = 'silent'; + } + + if (options.noInfo === true) { + level = 'warn'; + } + + return log({ + name: 'wds', + level, + timestamp: options.logTime + }); +} + +module.exports = createLogger; diff --git a/test/Entry.test.js b/test/Entry.test.js index 2d424f225c..816e23b7b2 100644 --- a/test/Entry.test.js +++ b/test/Entry.test.js @@ -1,7 +1,7 @@ 'use strict'; const assert = require('assert'); -const addDevServerEntrypoints = require('../lib/util/addDevServerEntrypoints'); +const addEntries = require('../lib/utils/addEntries'); const config = require('./fixtures/simple-config/webpack.config'); describe('Entry', () => { @@ -9,7 +9,7 @@ describe('Entry', () => { const webpackOptions = Object.assign({}, config); const devServerOptions = {}; - addDevServerEntrypoints(webpackOptions, devServerOptions); + addEntries(webpackOptions, devServerOptions); assert.equal(webpackOptions.entry.length, 2); assert(webpackOptions.entry[0].indexOf('client/index.js?') !== -1); @@ -22,7 +22,7 @@ describe('Entry', () => { }); const devServerOptions = {}; - addDevServerEntrypoints(webpackOptions, devServerOptions); + addEntries(webpackOptions, devServerOptions); assert.equal(webpackOptions.entry.length, 3); assert(webpackOptions.entry[0].indexOf('client/index.js?') !== -1); @@ -39,7 +39,7 @@ describe('Entry', () => { }); const devServerOptions = {}; - addDevServerEntrypoints(webpackOptions, devServerOptions); + addEntries(webpackOptions, devServerOptions); assert.equal(webpackOptions.entry.foo.length, 2); assert(webpackOptions.entry.foo[0].indexOf('client/index.js?') !== -1); @@ -51,7 +51,7 @@ describe('Entry', () => { const webpackOptions = {}; const devServerOptions = {}; - addDevServerEntrypoints(webpackOptions, devServerOptions); + addEntries(webpackOptions, devServerOptions); assert.equal(webpackOptions.entry.length, 2); assert.equal(webpackOptions.entry[1], './src'); @@ -68,7 +68,7 @@ describe('Entry', () => { }; const devServerOptions = {}; - addDevServerEntrypoints(webpackOptions, devServerOptions); + addEntries(webpackOptions, devServerOptions); assert(typeof webpackOptions.entry, 'function'); @@ -95,7 +95,7 @@ describe('Entry', () => { }; const devServerOptions = {}; - addDevServerEntrypoints(webpackOptions, devServerOptions); + addEntries(webpackOptions, devServerOptions); assert(typeof webpackOptions.entry, 'function'); @@ -121,7 +121,7 @@ describe('Entry', () => { hot: true }; - addDevServerEntrypoints(webpackOptions, devServerOptions); + addEntries(webpackOptions, devServerOptions); const hotClientScript = webpackOptions.entry.app[1]; assert.equal(hotClientScript.includes('webpack/hot/dev-server'), true); @@ -138,7 +138,7 @@ describe('Entry', () => { hotOnly: true }; - addDevServerEntrypoints(webpackOptions, devServerOptions); + addEntries(webpackOptions, devServerOptions); const hotClientScript = webpackOptions.entry.app[1]; assert.equal(hotClientScript.includes('webpack/hot/only-dev-server'), true); diff --git a/test/Util.test.js b/test/Util.test.js index 0c37056281..603a198c43 100644 --- a/test/Util.test.js +++ b/test/Util.test.js @@ -3,7 +3,7 @@ const webpack = require('webpack'); const internalIp = require('internal-ip'); const Server = require('../lib/Server'); -const createDomain = require('../lib/util/createDomain'); +const createDomain = require('../lib/utils/createDomain'); const config = require('./fixtures/simple-config/webpack.config'); describe('check utility funcitons', () => {