Skip to content

Commit 21687c3

Browse files
refactor: utils (#1682)
1 parent 523a6ec commit 21687c3

12 files changed

+88
-102
lines changed

bin/webpack-dev-server.js

+12-13
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,8 @@
44

55
/* eslint-disable
66
import/order,
7-
import/no-extraneous-dependencies,
8-
global-require,
97
no-shadow,
10-
no-console,
11-
multiline-ternary,
12-
arrow-parens,
13-
array-bracket-spacing,
14-
space-before-function-paren
8+
no-console
159
*/
1610
const debug = require('debug')('webpack-dev-server');
1711

@@ -26,14 +20,16 @@ const webpack = require('webpack');
2620

2721
const options = require('./options');
2822

29-
const { colors, status, version, bonjour } = require('./utils');
30-
3123
const Server = require('../lib/Server');
3224

3325
const addEntries = require('../lib/utils/addEntries');
26+
const colors = require('../lib/utils/colors');
27+
const createConfig = require('../lib/utils/createConfig');
3428
const createDomain = require('../lib/utils/createDomain');
3529
const createLogger = require('../lib/utils/createLogger');
36-
const createConfig = require('../lib/utils/createConfig');
30+
const getVersions = require('../lib/utils/getVersions');
31+
const runBonjour = require('../lib/utils/runBonjour');
32+
const status = require('../lib/utils/status');
3733

3834
let server;
3935

@@ -74,17 +70,20 @@ try {
7470
}
7571

7672
yargs.usage(
77-
`${version()}\nUsage: https://webpack.js.org/configuration/dev-server/`
73+
`${getVersions()}\nUsage: https://webpack.js.org/configuration/dev-server/`
7874
);
7975

76+
// eslint-disable-next-line import/no-extraneous-dependencies
8077
require('webpack-cli/bin/config-yargs')(yargs);
78+
8179
// It is important that this is done after the webpack yargs config,
8280
// so it overrides webpack's version info.
83-
yargs.version(version());
81+
yargs.version(getVersions());
8482
yargs.options(options);
8583

8684
const argv = yargs.argv;
8785

86+
// eslint-disable-next-line import/no-extraneous-dependencies
8887
const config = require('webpack-cli/bin/convert-argv')(yargs, argv, {
8988
outputFilename: '/bundle.js',
9089
});
@@ -217,7 +216,7 @@ function startDevServer(config, options) {
217216
}
218217

219218
if (options.bonjour) {
220-
bonjour(options);
219+
runBonjour(options);
221220
}
222221

223222
const uri = createDomain(options, server.listeningApp) + suffix;

lib/Server.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
import/order,
55
no-shadow,
66
no-undefined,
7-
func-names,
8-
multiline-ternary,
9-
array-bracket-spacing,
10-
space-before-function-paren
7+
func-names
118
*/
129
const fs = require('fs');
1310
const path = require('path');
@@ -702,7 +699,10 @@ class Server {
702699
return true;
703700
}
704701

705-
if (!headerToCheck) headerToCheck = 'host';
702+
if (!headerToCheck) {
703+
headerToCheck = 'host';
704+
}
705+
706706
// get the Host header and extract hostname
707707
// we don't care about port not matching
708708
const hostHeader = headers[headerToCheck];
@@ -736,7 +736,9 @@ class Server {
736736
for (let hostIdx = 0; hostIdx < this.allowedHosts.length; hostIdx++) {
737737
const allowedHost = this.allowedHosts[hostIdx];
738738

739-
if (allowedHost === hostname) return true;
739+
if (allowedHost === hostname) {
740+
return true;
741+
}
740742

741743
// support "." as a subdomain wildcard
742744
// e.g. ".example.com" will allow "example.com", "www.example.com", "subdomain.example.com", etc
@@ -778,7 +780,7 @@ class Server {
778780
listen(port, hostname, fn) {
779781
this.hostname = hostname;
780782

781-
const returnValue = this.listeningApp.listen(port, hostname, (err) => {
783+
return this.listeningApp.listen(port, hostname, (err) => {
782784
const socket = sockjs.createServer({
783785
// Use provided up-to-date sockjs-client
784786
sockjs_url: '/__webpack_dev_server__/sockjs.bundle.js',
@@ -849,8 +851,6 @@ class Server {
849851
fn.call(this.listeningApp, err);
850852
}
851853
});
852-
853-
return returnValue;
854854
}
855855

856856
close(cb) {

lib/utils/addEntries.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
'use strict';
22

3-
/* eslint-disable
4-
no-shadow,
5-
no-param-reassign,
6-
array-bracket-spacing,
7-
space-before-function-paren
8-
*/
93
const webpack = require('webpack');
104
const createDomain = require('./createDomain');
115

@@ -50,6 +44,7 @@ function addEntries(config, options, server) {
5044
return entries.concat(entry);
5145
};
5246

47+
// eslint-disable-next-line no-shadow
5348
[].concat(config).forEach((config) => {
5449
config.entry = prependEntry(config.entry || './src');
5550

lib/utils/colors.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const colors = {
4+
info(useColor, msg) {
5+
if (useColor) {
6+
// Make text blue and bold, so it *pops*
7+
return `\u001b[1m\u001b[34m${msg}\u001b[39m\u001b[22m`;
8+
}
9+
10+
return msg;
11+
},
12+
error(useColor, msg) {
13+
if (useColor) {
14+
// Make text red and bold, so it *pops*
15+
return `\u001b[1m\u001b[31m${msg}\u001b[39m\u001b[22m`;
16+
}
17+
18+
return msg;
19+
},
20+
};
21+
22+
module.exports = colors;

lib/utils/createCertificate.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
'use strict';
22

3-
/* eslint-disable
4-
space-before-function-paren
5-
*/
63
const selfsigned = require('selfsigned');
74

85
function createCertificate(attrs) {

lib/utils/createConfig.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const path = require('path');
4-
const { defaultTo } = require('../../bin/utils');
4+
const defaultTo = require('./defaultTo');
55

66
function createConfig(config, argv, { port }) {
77
const firstWpOpt = Array.isArray(config) ? config[0] : config;

lib/utils/createDomain.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
'use strict';
22

3-
/* eslint-disable
4-
no-nested-ternary,
5-
multiline-ternary,
6-
space-before-function-paren
7-
*/
83
const url = require('url');
94
const ip = require('internal-ip');
105

@@ -14,6 +9,7 @@ function createDomain(options, server) {
149
? ip.v4.sync() || 'localhost'
1510
: options.host;
1611

12+
// eslint-disable-next-line no-nested-ternary
1713
const port = options.socket ? 0 : server ? server.address().port : 0;
1814
// use explicitly defined public url
1915
// (prefix with protocol if not explicitly given)

lib/utils/createLogger.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
'use strict';
22

3-
/* eslint-disable
4-
space-before-function-paren
5-
*/
63
const log = require('webpack-log');
74

85
function createLogger(options) {

lib/utils/defaultTo.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
function defaultTo(value, def) {
4+
return value == null ? def : value;
5+
}
6+
7+
module.exports = defaultTo;

lib/utils/getVersions.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
/* eslint-disable global-require */
4+
5+
function getVersions() {
6+
return (
7+
`webpack-dev-server ${require('../../package.json').version}\n` +
8+
`webpack ${require('webpack/package.json').version}`
9+
);
10+
}
11+
12+
module.exports = getVersions;

lib/utils/runBonjour.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
function runBonjour(options) {
4+
// eslint-disable-next-line global-require
5+
const bonjour = require('bonjour')();
6+
7+
bonjour.publish({
8+
name: 'Webpack Dev Server',
9+
port: options.port,
10+
type: 'http',
11+
subtypes: ['webpack'],
12+
});
13+
14+
process.on('exit', () => {
15+
bonjour.unpublishAll(() => {
16+
bonjour.destroy();
17+
});
18+
});
19+
}
20+
21+
module.exports = runBonjour;
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,7 @@
11
'use strict';
22

3-
/* eslint-disable
4-
no-shadow,
5-
global-require,
6-
multiline-ternary,
7-
array-bracket-spacing,
8-
space-before-function-paren
9-
*/
103
const open = require('opn');
11-
12-
const colors = {
13-
info(useColor, msg) {
14-
if (useColor) {
15-
// Make text blue and bold, so it *pops*
16-
return `\u001b[1m\u001b[34m${msg}\u001b[39m\u001b[22m`;
17-
}
18-
19-
return msg;
20-
},
21-
error(useColor, msg) {
22-
if (useColor) {
23-
// Make text red and bold, so it *pops*
24-
return `\u001b[1m\u001b[31m${msg}\u001b[39m\u001b[22m`;
25-
}
26-
27-
return msg;
28-
},
29-
};
30-
31-
// eslint-disable-next-line
32-
const defaultTo = (value, def) => {
33-
return value == null ? def : value;
34-
};
35-
36-
function version() {
37-
return (
38-
`webpack-dev-server ${require('../package.json').version}\n` +
39-
`webpack ${require('webpack/package.json').version}`
40-
);
41-
}
4+
const colors = require('./colors');
425

436
function status(uri, options, log, useColor) {
447
const contentBase = Array.isArray(options.contentBase)
@@ -96,27 +59,4 @@ function status(uri, options, log, useColor) {
9659
}
9760
}
9861

99-
function bonjour(options) {
100-
const bonjour = require('bonjour')();
101-
102-
bonjour.publish({
103-
name: 'Webpack Dev Server',
104-
port: options.port,
105-
type: 'http',
106-
subtypes: ['webpack'],
107-
});
108-
109-
process.on('exit', () => {
110-
bonjour.unpublishAll(() => {
111-
bonjour.destroy();
112-
});
113-
});
114-
}
115-
116-
module.exports = {
117-
status,
118-
colors,
119-
version,
120-
bonjour,
121-
defaultTo,
122-
};
62+
module.exports = status;

0 commit comments

Comments
 (0)