Skip to content

Commit

Permalink
refactor(lib/utils): move createLog && rename files (#1465)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-ciniawsky authored Aug 27, 2018
1 parent 0e1f0c1 commit 2ee13ab
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 66 deletions.
11 changes: 6 additions & 5 deletions bin/webpack-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
19 changes: 0 additions & 19 deletions lib/createLog.js

This file was deleted.

23 changes: 0 additions & 23 deletions lib/util/createDomain.js

This file was deleted.

21 changes: 15 additions & 6 deletions lib/util/addDevServerEntrypoints.js → lib/utils/addEntries.js
Original file line number Diff line number Diff line change
@@ -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}`];

Expand All @@ -27,18 +30,24 @@ 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);
};

[].concat(webpackOptions).forEach((wpOpt) => {
wpOpt.entry = prependDevClient(wpOpt.entry || './src');
});
}
};
}

module.exports = addEntries;
35 changes: 35 additions & 0 deletions lib/utils/createDomain.js
Original file line number Diff line number Diff line change
@@ -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;
26 changes: 26 additions & 0 deletions lib/utils/createLogger.js
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 9 additions & 9 deletions test/Entry.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'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', () => {
it('adds devServer entry points to a single entry point', () => {
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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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');
Expand All @@ -68,7 +68,7 @@ describe('Entry', () => {
};
const devServerOptions = {};

addDevServerEntrypoints(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions);

assert(typeof webpackOptions.entry, 'function');

Expand All @@ -95,7 +95,7 @@ describe('Entry', () => {
};
const devServerOptions = {};

addDevServerEntrypoints(webpackOptions, devServerOptions);
addEntries(webpackOptions, devServerOptions);

assert(typeof webpackOptions.entry, 'function');

Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/Util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 2ee13ab

Please sign in to comment.