Skip to content

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

Merged
merged 5 commits into from
May 15, 2021
Merged
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
54 changes: 35 additions & 19 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link
Member

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

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) {
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -162,6 +148,8 @@ class Server {
}

setupDevMiddleware() {
const webpackDevMiddleware = require('webpack-dev-middleware');

// middleware for serving webpack bundle
this.middleware = webpackDevMiddleware(
this.compiler,
Expand All @@ -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: {
Expand Down Expand Up @@ -319,6 +311,8 @@ class Server {
}

setupHistoryApiFallbackFeature() {
const historyApiFallback = require('connect-history-api-fallback');

const fallback =
typeof this.options.historyApiFallback === 'object'
? this.options.historyApiFallback
Expand All @@ -340,6 +334,8 @@ class Server {
}

setupStaticServeIndexFeature() {
const serveIndex = require('serve-index');

this.options.static.forEach((staticOption) => {
staticOption.publicPath.forEach((publicPath) => {
if (staticOption.serveIndex) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -546,6 +544,9 @@ class Server {
}

createServer() {
const https = require('https');
const http = require('http');
Comment on lines +547 to +548
Copy link
Contributor

Choose a reason for hiding this comment

The 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 require('spdy').

Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -784,6 +791,8 @@ class Server {
}

if (this.options.bonjour) {
const runBonjour = require('./utils/runBonjour');

runBonjour(this.options);
}

Expand Down Expand Up @@ -838,6 +847,8 @@ class Server {
}

getStats(statsObj) {
const getStatsOption = require('./utils/getStatsOption');

const stats = Server.DEFAULT_STATS;

const configArr = getCompilerConfigArray(this.compiler);
Expand Down Expand Up @@ -974,6 +985,9 @@ class Server {
}

serveMagicHtml(req, res, next) {
const getFilenameFromUrl =
require('webpack-dev-middleware/dist/utils/getFilenameFromUrl').default;
Copy link
Member

Choose a reason for hiding this comment

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

I think we should add this method to public webpack-dev-middleware API here https://github.com/webpack/webpack-dev-middleware/blob/master/src/index.js#L78, in future we should avoid import non public API

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense, will do 👍🏻


const _path = req.path;

try {
Expand Down Expand Up @@ -1050,6 +1064,8 @@ class Server {
interval,
};

const chokidar = require('chokidar');

const watcher = chokidar.watch(watchPath, finalWatchOptions);

// disabling refreshing on changing the content
Expand Down