Skip to content

Commit 2ee13ab

Browse files
refactor(lib/utils): move createLog && rename files (#1465)
1 parent 0e1f0c1 commit 2ee13ab

File tree

9 files changed

+95
-66
lines changed

9 files changed

+95
-66
lines changed

bin/webpack-dev-server.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ const path = require('path');
1111
const importLocal = require('import-local');
1212
const open = require('opn');
1313
const portfinder = require('portfinder');
14-
const addDevServerEntrypoints = require('../lib/util/addDevServerEntrypoints');
15-
const createDomain = require('../lib/util/createDomain'); // eslint-disable-line
16-
const createLog = require('../lib/createLog');
14+
const addEntries = require('../lib/utils/addEntries');
15+
const createDomain = require('../lib/utils/createDomain');
16+
const createLogger = require('../lib/utils/createLogger');
1717

1818
let server;
1919

@@ -384,8 +384,9 @@ function processOptions(webpackOptions) {
384384
}
385385

386386
function startDevServer(webpackOptions, options) {
387-
const log = createLog(options);
388-
addDevServerEntrypoints(webpackOptions, options);
387+
const log = createLogger(options);
388+
389+
addEntries(webpackOptions, options);
389390

390391
let compiler;
391392
try {

lib/Server.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const sockjs = require('sockjs');
2020
const spdy = require('spdy');
2121
const webpack = require('webpack');
2222
const webpackDevMiddleware = require('webpack-dev-middleware');
23-
const createLog = require('./createLog');
23+
const createLogger = require('./utils/createLogger');
2424
const OptionsValidationError = require('./OptionsValidationError');
2525
const optionsSchema = require('./optionsSchema.json');
2626

@@ -29,7 +29,7 @@ const clientStats = { all: false, assets: true, warnings: true, errors: true, er
2929
function Server(compiler, options, _log) {
3030
// Default options
3131
if (!options) options = {};
32-
this.log = _log || createLog(options);
32+
this.log = _log || createLogger(options);
3333

3434
const validationErrors = webpack.validateSchema(optionsSchema, options);
3535
if (validationErrors.length) {
@@ -703,6 +703,6 @@ Server.prototype.invalidate = function () {
703703
};
704704

705705
// Export this logic, so that other implementations, like task-runners can use it
706-
Server.addDevServerEntrypoints = require('./util/addDevServerEntrypoints');
706+
Server.addDevServerEntrypoints = require('./utils/addEntries');
707707

708708
module.exports = Server;

lib/createLog.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

lib/util/createDomain.js

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
'use strict';
22

3-
/* eslint no-param-reassign: 'off' */
4-
3+
/* eslint-disable
4+
no-param-reassign,
5+
space-before-function-paren
6+
*/
57
const createDomain = require('./createDomain');
68

7-
module.exports = function addDevServerEntrypoints(webpackOptions, devServerOptions, listeningApp) {
9+
function addEntries (webpackOptions, devServerOptions, server) {
810
if (devServerOptions.inline !== false) {
911
// we're stubbing the app in this method as it's static and doesn't require
10-
// a listeningApp to be supplied. createDomain requires an app with the
12+
// a server to be supplied. createDomain requires an app with the
1113
// address() signature.
12-
const app = listeningApp || {
14+
const app = server || {
1315
address() {
1416
return { port: devServerOptions.port };
1517
}
1618
};
19+
1720
const domain = createDomain(devServerOptions, app);
1821
const devClient = [`${require.resolve('../../client/')}?${domain}`];
1922

@@ -27,18 +30,24 @@ module.exports = function addDevServerEntrypoints(webpackOptions, devServerOptio
2730
if (typeof entry === 'function') {
2831
return () => Promise.resolve(entry()).then(prependDevClient);
2932
}
33+
3034
if (typeof entry === 'object' && !Array.isArray(entry)) {
3135
const entryClone = {};
36+
3237
Object.keys(entry).forEach((key) => {
3338
entryClone[key] = devClient.concat(entry[key]);
3439
});
40+
3541
return entryClone;
3642
}
43+
3744
return devClient.concat(entry);
3845
};
3946

4047
[].concat(webpackOptions).forEach((wpOpt) => {
4148
wpOpt.entry = prependDevClient(wpOpt.entry || './src');
4249
});
4350
}
44-
};
51+
}
52+
53+
module.exports = addEntries;

lib/utils/createDomain.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
/* eslint-disable
4+
no-nested-ternary,
5+
multiline-ternary,
6+
space-before-function-paren
7+
*/
8+
const url = require('url');
9+
const ip = require('internal-ip');
10+
11+
function createDomain (options, server) {
12+
const protocol = options.https ? 'https' : 'http';
13+
const hostname = options.useLocalIp ? ip.v4() : options.host;
14+
15+
const port = options.socket
16+
? 0
17+
: server
18+
? server.address().port
19+
: 0;
20+
// use explicitly defined public url
21+
// (prefix with protocol if not explicitly given)
22+
if (options.public) {
23+
return /^[a-zA-Z]+:\/\//.test(options.public)
24+
? `${options.public}`
25+
: `${protocol}://${options.public}`;
26+
}
27+
// the formatted domain (url without path) of the webpack server
28+
return url.format({
29+
protocol,
30+
hostname,
31+
port
32+
});
33+
}
34+
35+
module.exports = createDomain;

lib/utils/createLogger.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
/* eslint-disable
4+
space-before-function-paren
5+
*/
6+
const log = require('webpack-log');
7+
8+
function createLogger (options) {
9+
let level = options.logLevel || 'info';
10+
11+
if (options.quiet === true) {
12+
level = 'silent';
13+
}
14+
15+
if (options.noInfo === true) {
16+
level = 'warn';
17+
}
18+
19+
return log({
20+
name: 'wds',
21+
level,
22+
timestamp: options.logTime
23+
});
24+
}
25+
26+
module.exports = createLogger;

test/Entry.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
'use strict';
22

33
const assert = require('assert');
4-
const addDevServerEntrypoints = require('../lib/util/addDevServerEntrypoints');
4+
const addEntries = require('../lib/utils/addEntries');
55
const config = require('./fixtures/simple-config/webpack.config');
66

77
describe('Entry', () => {
88
it('adds devServer entry points to a single entry point', () => {
99
const webpackOptions = Object.assign({}, config);
1010
const devServerOptions = {};
1111

12-
addDevServerEntrypoints(webpackOptions, devServerOptions);
12+
addEntries(webpackOptions, devServerOptions);
1313

1414
assert.equal(webpackOptions.entry.length, 2);
1515
assert(webpackOptions.entry[0].indexOf('client/index.js?') !== -1);
@@ -22,7 +22,7 @@ describe('Entry', () => {
2222
});
2323
const devServerOptions = {};
2424

25-
addDevServerEntrypoints(webpackOptions, devServerOptions);
25+
addEntries(webpackOptions, devServerOptions);
2626

2727
assert.equal(webpackOptions.entry.length, 3);
2828
assert(webpackOptions.entry[0].indexOf('client/index.js?') !== -1);
@@ -39,7 +39,7 @@ describe('Entry', () => {
3939
});
4040
const devServerOptions = {};
4141

42-
addDevServerEntrypoints(webpackOptions, devServerOptions);
42+
addEntries(webpackOptions, devServerOptions);
4343

4444
assert.equal(webpackOptions.entry.foo.length, 2);
4545
assert(webpackOptions.entry.foo[0].indexOf('client/index.js?') !== -1);
@@ -51,7 +51,7 @@ describe('Entry', () => {
5151
const webpackOptions = {};
5252
const devServerOptions = {};
5353

54-
addDevServerEntrypoints(webpackOptions, devServerOptions);
54+
addEntries(webpackOptions, devServerOptions);
5555

5656
assert.equal(webpackOptions.entry.length, 2);
5757
assert.equal(webpackOptions.entry[1], './src');
@@ -68,7 +68,7 @@ describe('Entry', () => {
6868
};
6969
const devServerOptions = {};
7070

71-
addDevServerEntrypoints(webpackOptions, devServerOptions);
71+
addEntries(webpackOptions, devServerOptions);
7272

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

@@ -95,7 +95,7 @@ describe('Entry', () => {
9595
};
9696
const devServerOptions = {};
9797

98-
addDevServerEntrypoints(webpackOptions, devServerOptions);
98+
addEntries(webpackOptions, devServerOptions);
9999

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

@@ -121,7 +121,7 @@ describe('Entry', () => {
121121
hot: true
122122
};
123123

124-
addDevServerEntrypoints(webpackOptions, devServerOptions);
124+
addEntries(webpackOptions, devServerOptions);
125125

126126
const hotClientScript = webpackOptions.entry.app[1];
127127
assert.equal(hotClientScript.includes('webpack/hot/dev-server'), true);
@@ -138,7 +138,7 @@ describe('Entry', () => {
138138
hotOnly: true
139139
};
140140

141-
addDevServerEntrypoints(webpackOptions, devServerOptions);
141+
addEntries(webpackOptions, devServerOptions);
142142

143143
const hotClientScript = webpackOptions.entry.app[1];
144144
assert.equal(hotClientScript.includes('webpack/hot/only-dev-server'), true);

test/Util.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const webpack = require('webpack');
44
const internalIp = require('internal-ip');
55
const Server = require('../lib/Server');
6-
const createDomain = require('../lib/util/createDomain');
6+
const createDomain = require('../lib/utils/createDomain');
77
const config = require('./fixtures/simple-config/webpack.config');
88

99
describe('check utility funcitons', () => {

0 commit comments

Comments
 (0)