-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
refactor: utils #1682
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
*/ | ||
const debug = require('debug')('webpack-dev-server'); | ||
|
||
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for separating to utils 🙏 |
||
|
||
let server; | ||
|
||
|
@@ -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', | ||
}); | ||
|
@@ -217,7 +216,7 @@ function startDevServer(config, options) { | |
} | ||
|
||
if (options.bonjour) { | ||
bonjour(options); | ||
runBonjour(options); | ||
} | ||
|
||
const uri = createDomain(options, server.listeningApp) + suffix; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
|
@@ -702,7 +699,10 @@ class Server { | |
return true; | ||
} | ||
|
||
if (!headerToCheck) headerToCheck = 'host'; | ||
if (!headerToCheck) { | ||
headerToCheck = 'host'; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
|
||
// get the Host header and extract hostname | ||
// we don't care about port not matching | ||
const hostHeader = headers[headerToCheck]; | ||
|
@@ -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 | ||
|
@@ -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', | ||
|
@@ -849,8 +851,6 @@ class Server { | |
fn.call(this.listeningApp, err); | ||
} | ||
}); | ||
|
||
return returnValue; | ||
} | ||
|
||
close(cb) { | ||
|
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; |
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; |
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; |
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; |
There was a problem hiding this comment.
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 useprettier
👍