-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
refactor: imports #3299
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: imports #3299
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 |
---|---|---|
|
@@ -2,36 +2,18 @@ | |
|
||
const path = require('path'); | ||
const url = require('url'); | ||
const http = require('http'); | ||
const https = require('https'); | ||
const fs = require('graceful-fs'); | ||
const ipaddr = require('ipaddr.js'); | ||
const internalIp = require('internal-ip'); | ||
const killable = require('killable'); | ||
const chokidar = require('chokidar'); | ||
const express = require('express'); | ||
const { createProxyMiddleware } = require('http-proxy-middleware'); | ||
const historyApiFallback = require('connect-history-api-fallback'); | ||
const compress = require('compression'); | ||
const serveIndex = require('serve-index'); | ||
const webpack = require('webpack'); | ||
const webpackDevMiddleware = require('webpack-dev-middleware'); | ||
const getFilenameFromUrl = | ||
require('webpack-dev-middleware/dist/utils/getFilenameFromUrl').default; | ||
const { validate } = require('schema-utils'); | ||
const DevServerPlugin = require('./utils/DevServerPlugin'); | ||
const normalizeOptions = require('./utils/normalizeOptions'); | ||
const getCertificate = require('./utils/getCertificate'); | ||
const colors = require('./utils/colors'); | ||
const runOpen = require('./utils/runOpen'); | ||
const runBonjour = require('./utils/runBonjour'); | ||
const routes = require('./utils/routes'); | ||
const getSocketServerImplementation = require('./utils/getSocketServerImplementation'); | ||
const getCompilerConfigArray = require('./utils/getCompilerConfigArray'); | ||
const getStatsOption = require('./utils/getStatsOption'); | ||
const getColorsOption = require('./utils/getColorsOption'); | ||
const setupExitSignals = require('./utils/setupExitSignals'); | ||
const findPort = require('./utils/findPort'); | ||
const schema = require('./options.json'); | ||
|
||
if (!process.env.WEBPACK_SERVE) { | ||
|
@@ -93,6 +75,8 @@ class Server { | |
} | ||
|
||
applyDevServerPlugin() { | ||
const DevServerPlugin = require('./utils/DevServerPlugin'); | ||
|
||
const compilers = this.compiler.compilers || [this.compiler]; | ||
|
||
// eslint-disable-next-line no-shadow | ||
|
@@ -102,7 +86,9 @@ class Server { | |
} | ||
|
||
setupProgressPlugin() { | ||
new webpack.ProgressPlugin((percent, msg, addInfo) => { | ||
const { ProgressPlugin } = require('webpack'); | ||
|
||
new ProgressPlugin((percent, msg, addInfo) => { | ||
percent = Math.floor(percent * 100); | ||
|
||
if (percent === 100) { | ||
|
@@ -162,6 +148,8 @@ class Server { | |
} | ||
|
||
setupDevMiddleware() { | ||
const webpackDevMiddleware = require('webpack-dev-middleware'); | ||
|
||
// middleware for serving webpack bundle | ||
this.middleware = webpackDevMiddleware( | ||
this.compiler, | ||
|
@@ -170,10 +158,14 @@ class Server { | |
} | ||
|
||
setupCompressFeature() { | ||
const compress = require('compression'); | ||
|
||
this.app.use(compress()); | ||
} | ||
|
||
setupProxyFeature() { | ||
const { createProxyMiddleware } = require('http-proxy-middleware'); | ||
|
||
/** | ||
* Assume a proxy configuration specified as: | ||
* proxy: { | ||
|
@@ -319,6 +311,8 @@ class Server { | |
} | ||
|
||
setupHistoryApiFallbackFeature() { | ||
const historyApiFallback = require('connect-history-api-fallback'); | ||
|
||
const fallback = | ||
typeof this.options.historyApiFallback === 'object' | ||
? this.options.historyApiFallback | ||
|
@@ -340,6 +334,8 @@ class Server { | |
} | ||
|
||
setupStaticServeIndexFeature() { | ||
const serveIndex = require('serve-index'); | ||
|
||
this.options.static.forEach((staticOption) => { | ||
staticOption.publicPath.forEach((publicPath) => { | ||
if (staticOption.serveIndex) { | ||
|
@@ -514,6 +510,8 @@ class Server { | |
} | ||
|
||
if (this.options.https) { | ||
const getCertificate = require('./utils/getCertificate'); | ||
|
||
for (const property of ['cacert', 'pfx', 'key', 'cert']) { | ||
const value = this.options.https[property]; | ||
const isBuffer = value instanceof Buffer; | ||
|
@@ -546,6 +544,9 @@ class Server { | |
} | ||
|
||
createServer() { | ||
const https = require('https'); | ||
const http = require('http'); | ||
Comment on lines
+547
to
+548
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. I'm not sure it'd make a difference, but I think we can move these below just like 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. Feel free to send PR |
||
|
||
if (this.options.https) { | ||
if (this.options.http2) { | ||
// TODO: we need to replace spdy with http2 which is an internal module | ||
|
@@ -628,6 +629,8 @@ class Server { | |
} | ||
|
||
showStatus() { | ||
const getColorsOption = require('./utils/getColorsOption'); | ||
|
||
const useColor = getColorsOption(getCompilerConfigArray(this.compiler)); | ||
const protocol = this.options.https ? 'https' : 'http'; | ||
const { address, port } = this.server.address(); | ||
|
@@ -749,13 +752,17 @@ class Server { | |
} | ||
|
||
if (this.options.open) { | ||
const runOpen = require('./utils/runOpen'); | ||
|
||
const openTarget = prettyPrintUrl(this.hostname || 'localhost'); | ||
|
||
runOpen(openTarget, this.options.open, this.logger); | ||
} | ||
} | ||
|
||
listen(port, hostname, fn) { | ||
const findPort = require('./utils/findPort'); | ||
|
||
if (hostname === 'local-ip') { | ||
this.hostname = internalIp.v4.sync() || internalIp.v6.sync() || '0.0.0.0'; | ||
} else if (hostname === 'local-ipv4') { | ||
|
@@ -784,6 +791,8 @@ class Server { | |
} | ||
|
||
if (this.options.bonjour) { | ||
const runBonjour = require('./utils/runBonjour'); | ||
|
||
runBonjour(this.options); | ||
} | ||
|
||
|
@@ -838,6 +847,8 @@ class Server { | |
} | ||
|
||
getStats(statsObj) { | ||
const getStatsOption = require('./utils/getStatsOption'); | ||
|
||
const stats = Server.DEFAULT_STATS; | ||
|
||
const configArr = getCompilerConfigArray(this.compiler); | ||
|
@@ -974,6 +985,9 @@ class Server { | |
} | ||
|
||
serveMagicHtml(req, res, next) { | ||
const getFilenameFromUrl = | ||
require('webpack-dev-middleware/dist/utils/getFilenameFromUrl').default; | ||
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. I think we should add this method to public 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. Makes sense, will do 👍🏻 |
||
|
||
const _path = req.path; | ||
|
||
try { | ||
|
@@ -1050,6 +1064,8 @@ class Server { | |
interval, | ||
}; | ||
|
||
const chokidar = require('chokidar'); | ||
|
||
const watcher = chokidar.watch(watchPath, finalWatchOptions); | ||
|
||
// disabling refreshing on changing the content | ||
|
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.
I think in future we reduce them more, good right now