Skip to content

refactor: utils #1682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions bin/webpack-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@

/* eslint-disable
import/order,
import/no-extraneous-dependencies,
global-require,
no-shadow,
no-console,
multiline-ternary,
arrow-parens,
array-bracket-spacing,
space-before-function-paren
no-console
*/
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove eslint comments around stylistics rules, we use prettier 👍

const debug = require('debug')('webpack-dev-server');

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

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

const { colors, status, version, bonjour } = require('./utils');

const Server = require('../lib/Server');

const addEntries = require('../lib/utils/addEntries');
const colors = require('../lib/utils/colors');
const createConfig = require('../lib/utils/createConfig');
const createDomain = require('../lib/utils/createDomain');
const createLogger = require('../lib/utils/createLogger');
const createConfig = require('../lib/utils/createConfig');
const getVersions = require('../lib/utils/getVersions');
const runBonjour = require('../lib/utils/runBonjour');
const status = require('../lib/utils/status');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to utils, we should testing their, let's do it in other PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for separating to utils 🙏


let server;

Expand Down Expand Up @@ -74,17 +70,20 @@ try {
}

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

// eslint-disable-next-line import/no-extraneous-dependencies
require('webpack-cli/bin/config-yargs')(yargs);

// It is important that this is done after the webpack yargs config,
// so it overrides webpack's version info.
yargs.version(version());
yargs.version(getVersions());
yargs.options(options);

const argv = yargs.argv;

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

if (options.bonjour) {
bonjour(options);
runBonjour(options);
}

const uri = createDomain(options, server.listeningApp) + suffix;
Expand Down
18 changes: 9 additions & 9 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import/order,
no-shadow,
no-undefined,
func-names,
multiline-ternary,
array-bracket-spacing,
space-before-function-paren
func-names
*/
const fs = require('fs');
const path = require('path');
Expand Down Expand Up @@ -702,7 +699,10 @@ class Server {
return true;
}

if (!headerToCheck) headerToCheck = 'host';
if (!headerToCheck) {
headerToCheck = 'host';
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add { and } to all construction (better reading), we should enforce this rule in eslint, let's do it in other PR


// get the Host header and extract hostname
// we don't care about port not matching
const hostHeader = headers[headerToCheck];
Expand Down Expand Up @@ -736,7 +736,9 @@ class Server {
for (let hostIdx = 0; hostIdx < this.allowedHosts.length; hostIdx++) {
const allowedHost = this.allowedHosts[hostIdx];

if (allowedHost === hostname) return true;
if (allowedHost === hostname) {
return true;
}

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

const returnValue = this.listeningApp.listen(port, hostname, (err) => {
return this.listeningApp.listen(port, hostname, (err) => {
const socket = sockjs.createServer({
// Use provided up-to-date sockjs-client
sockjs_url: '/__webpack_dev_server__/sockjs.bundle.js',
Expand Down Expand Up @@ -849,8 +851,6 @@ class Server {
fn.call(this.listeningApp, err);
}
});

return returnValue;
}

close(cb) {
Expand Down
7 changes: 1 addition & 6 deletions lib/utils/addEntries.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
'use strict';

/* eslint-disable
no-shadow,
no-param-reassign,
array-bracket-spacing,
space-before-function-paren
*/
const webpack = require('webpack');
const createDomain = require('./createDomain');

Expand Down Expand Up @@ -50,6 +44,7 @@ function addEntries(config, options, server) {
return entries.concat(entry);
};

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

Expand Down
22 changes: 22 additions & 0 deletions lib/utils/colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const colors = {
info(useColor, msg) {
if (useColor) {
// Make text blue and bold, so it *pops*
return `\u001b[1m\u001b[34m${msg}\u001b[39m\u001b[22m`;
}

return msg;
},
error(useColor, msg) {
if (useColor) {
// Make text red and bold, so it *pops*
return `\u001b[1m\u001b[31m${msg}\u001b[39m\u001b[22m`;
}

return msg;
},
};

module.exports = colors;
3 changes: 0 additions & 3 deletions lib/utils/createCertificate.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict';

/* eslint-disable
space-before-function-paren
*/
const selfsigned = require('selfsigned');

function createCertificate(attrs) {
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/createConfig.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const path = require('path');
const { defaultTo } = require('../../bin/utils');
const defaultTo = require('./defaultTo');

function createConfig(config, argv, { port }) {
const firstWpOpt = Array.isArray(config) ? config[0] : config;
Expand Down
6 changes: 1 addition & 5 deletions lib/utils/createDomain.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
'use strict';

/* eslint-disable
no-nested-ternary,
multiline-ternary,
space-before-function-paren
*/
const url = require('url');
const ip = require('internal-ip');

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

// eslint-disable-next-line no-nested-ternary
const port = options.socket ? 0 : server ? server.address().port : 0;
// use explicitly defined public url
// (prefix with protocol if not explicitly given)
Expand Down
3 changes: 0 additions & 3 deletions lib/utils/createLogger.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict';

/* eslint-disable
space-before-function-paren
*/
const log = require('webpack-log');

function createLogger(options) {
Expand Down
7 changes: 7 additions & 0 deletions lib/utils/defaultTo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

function defaultTo(value, def) {
return value == null ? def : value;
}

module.exports = defaultTo;
12 changes: 12 additions & 0 deletions lib/utils/getVersions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

/* eslint-disable global-require */

function getVersions() {
return (
`webpack-dev-server ${require('../../package.json').version}\n` +
`webpack ${require('webpack/package.json').version}`
);
}

module.exports = getVersions;
21 changes: 21 additions & 0 deletions lib/utils/runBonjour.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

function runBonjour(options) {
// eslint-disable-next-line global-require
const bonjour = require('bonjour')();

bonjour.publish({
name: 'Webpack Dev Server',
port: options.port,
type: 'http',
subtypes: ['webpack'],
});

process.on('exit', () => {
bonjour.unpublishAll(() => {
bonjour.destroy();
});
});
}

module.exports = runBonjour;
64 changes: 2 additions & 62 deletions bin/utils.js → lib/utils/status.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,7 @@
'use strict';

/* eslint-disable
no-shadow,
global-require,
multiline-ternary,
array-bracket-spacing,
space-before-function-paren
*/
const open = require('opn');

const colors = {
info(useColor, msg) {
if (useColor) {
// Make text blue and bold, so it *pops*
return `\u001b[1m\u001b[34m${msg}\u001b[39m\u001b[22m`;
}

return msg;
},
error(useColor, msg) {
if (useColor) {
// Make text red and bold, so it *pops*
return `\u001b[1m\u001b[31m${msg}\u001b[39m\u001b[22m`;
}

return msg;
},
};

// eslint-disable-next-line
const defaultTo = (value, def) => {
return value == null ? def : value;
};

function version() {
return (
`webpack-dev-server ${require('../package.json').version}\n` +
`webpack ${require('webpack/package.json').version}`
);
}
const colors = require('./colors');

function status(uri, options, log, useColor) {
const contentBase = Array.isArray(options.contentBase)
Expand Down Expand Up @@ -96,27 +59,4 @@ function status(uri, options, log, useColor) {
}
}

function bonjour(options) {
const bonjour = require('bonjour')();

bonjour.publish({
name: 'Webpack Dev Server',
port: options.port,
type: 'http',
subtypes: ['webpack'],
});

process.on('exit', () => {
bonjour.unpublishAll(() => {
bonjour.destroy();
});
});
}

module.exports = {
status,
colors,
version,
bonjour,
defaultTo,
};
module.exports = status;