Skip to content

Commit ba8209a

Browse files
committed
chore: rebase
2 parents a2bc74f + 9801377 commit ba8209a

File tree

57 files changed

+2299
-1732
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2299
-1732
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ Options:
108108
--no-http2 Do not use HTTP/2.
109109
--bonjour Broadcasts the server via ZeroConf networking on start.
110110
--no-bonjour Do not broadcast the server via ZeroConf networking on start.
111+
--client-hot-entry Tell devServer to inject a Hot Module Replacement entry.
112+
--no-client-hot-entry Do not tell devServer to inject a Hot Module Replacement entry.
111113
--client-progress Print compilation progress in percentage in the browser.
112114
--no-client-progress Do not print compilation progress in percentage in the browser.
113115
--client-overlay Show a full-screen overlay in the browser when there are compiler errors or warnings.

bin/cli-flags.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,24 @@ module.exports = {
244244
'Do not broadcast the server via ZeroConf networking on start.',
245245
negative: true,
246246
},
247+
{
248+
name: 'client-hot-entry',
249+
type: Boolean,
250+
configs: [
251+
{
252+
type: 'boolean',
253+
},
254+
],
255+
description: 'Tell devServer to inject a Hot Module Replacement entry.',
256+
negatedDescription:
257+
'Do not tell devServer to inject a Hot Module Replacement entry.',
258+
negative: true,
259+
processor(opts) {
260+
opts.client = opts.client || {};
261+
opts.client.hotEntry = opts.clientHotEntry;
262+
delete opts.clientHotEntry;
263+
},
264+
},
247265
{
248266
name: 'client-progress',
249267
type: Boolean,

lib/Server.js

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,18 @@
22

33
const path = require('path');
44
const url = require('url');
5-
const http = require('http');
6-
const https = require('https');
75
const fs = require('graceful-fs');
86
const ipaddr = require('ipaddr.js');
97
const internalIp = require('internal-ip');
108
const killable = require('killable');
11-
const chokidar = require('chokidar');
129
const express = require('express');
13-
const { createProxyMiddleware } = require('http-proxy-middleware');
14-
const historyApiFallback = require('connect-history-api-fallback');
15-
const compress = require('compression');
16-
const serveIndex = require('serve-index');
17-
const webpack = require('webpack');
18-
const webpackDevMiddleware = require('webpack-dev-middleware');
19-
const getFilenameFromUrl =
20-
require('webpack-dev-middleware/dist/utils/getFilenameFromUrl').default;
2110
const { validate } = require('schema-utils');
22-
const DevServerPlugin = require('./utils/DevServerPlugin');
2311
const normalizeOptions = require('./utils/normalizeOptions');
24-
const getCertificate = require('./utils/getCertificate');
2512
const colors = require('./utils/colors');
26-
const runOpen = require('./utils/runOpen');
27-
const runBonjour = require('./utils/runBonjour');
2813
const routes = require('./utils/routes');
2914
const getSocketServerImplementation = require('./utils/getSocketServerImplementation');
3015
const getCompilerConfigArray = require('./utils/getCompilerConfigArray');
31-
const getStatsOption = require('./utils/getStatsOption');
32-
const getColorsOption = require('./utils/getColorsOption');
3316
const setupExitSignals = require('./utils/setupExitSignals');
34-
const findPort = require('./utils/findPort');
3517
const schema = require('./options.json');
3618

3719
if (!process.env.WEBPACK_SERVE) {
@@ -103,6 +85,8 @@ class Server {
10385
}
10486

10587
applyDevServerPlugin() {
88+
const DevServerPlugin = require('./utils/DevServerPlugin');
89+
10690
const compilers = this.compiler.compilers || [this.compiler];
10791

10892
// eslint-disable-next-line no-shadow
@@ -112,7 +96,9 @@ class Server {
11296
}
11397

11498
setupProgressPlugin() {
115-
new webpack.ProgressPlugin((percent, msg, addInfo) => {
99+
const { ProgressPlugin } = require('webpack');
100+
101+
new ProgressPlugin((percent, msg, addInfo) => {
116102
percent = Math.floor(percent * 100);
117103

118104
if (percent === 100) {
@@ -172,6 +158,8 @@ class Server {
172158
}
173159

174160
setupDevMiddleware() {
161+
const webpackDevMiddleware = require('webpack-dev-middleware');
162+
175163
// middleware for serving webpack bundle
176164
this.middleware = webpackDevMiddleware(
177165
this.compiler,
@@ -180,10 +168,14 @@ class Server {
180168
}
181169

182170
setupCompressFeature() {
171+
const compress = require('compression');
172+
183173
this.app.use(compress());
184174
}
185175

186176
setupProxyFeature() {
177+
const { createProxyMiddleware } = require('http-proxy-middleware');
178+
187179
/**
188180
* Assume a proxy configuration specified as:
189181
* proxy: {
@@ -329,6 +321,8 @@ class Server {
329321
}
330322

331323
setupHistoryApiFallbackFeature() {
324+
const historyApiFallback = require('connect-history-api-fallback');
325+
332326
const fallback =
333327
typeof this.options.historyApiFallback === 'object'
334328
? this.options.historyApiFallback
@@ -350,6 +344,8 @@ class Server {
350344
}
351345

352346
setupStaticServeIndexFeature() {
347+
const serveIndex = require('serve-index');
348+
353349
this.options.static.forEach((staticOption) => {
354350
staticOption.publicPath.forEach((publicPath) => {
355351
if (staticOption.serveIndex) {
@@ -524,6 +520,8 @@ class Server {
524520
}
525521

526522
if (this.options.https) {
523+
const getCertificate = require('./utils/getCertificate');
524+
527525
for (const property of ['cacert', 'pfx', 'key', 'cert']) {
528526
const value = this.options.https[property];
529527
const isBuffer = value instanceof Buffer;
@@ -556,6 +554,9 @@ class Server {
556554
}
557555

558556
createServer() {
557+
const https = require('https');
558+
const http = require('http');
559+
559560
if (this.options.https) {
560561
if (this.options.http2) {
561562
// TODO: we need to replace spdy with http2 which is an internal module
@@ -590,7 +591,7 @@ class Server {
590591

591592
if (!headers) {
592593
this.logger.warn(
593-
'transportMode.server implementation must pass headers to the callback of onConnection(f) ' +
594+
'webSocketServer implementation must pass headers to the callback of onConnection(f) ' +
594595
'via f(connection, headers) in order for clients to pass a headers security check'
595596
);
596597
}
@@ -638,6 +639,8 @@ class Server {
638639
}
639640

640641
showStatus() {
642+
const getColorsOption = require('./utils/getColorsOption');
643+
641644
const useColor = getColorsOption(getCompilerConfigArray(this.compiler));
642645
const protocol = this.options.https ? 'https' : 'http';
643646
const { address, port } = this.server.address();
@@ -759,6 +762,8 @@ class Server {
759762
}
760763

761764
if (this.options.open) {
765+
const runOpen = require('./utils/runOpen');
766+
762767
const openTarget = prettyPrintUrl(this.hostname || 'localhost');
763768

764769
runOpen(openTarget, this.options.open, this.logger);
@@ -783,7 +788,7 @@ class Server {
783788
}
784789

785790
return (
786-
findPort(port || this.options.port)
791+
Server.getFreePort(port || this.options.port)
787792
// eslint-disable-next-line no-shadow
788793
.then((port) => {
789794
this.options.port = port;
@@ -794,6 +799,8 @@ class Server {
794799
}
795800

796801
if (this.options.bonjour) {
802+
const runBonjour = require('./utils/runBonjour');
803+
797804
runBonjour(this.options);
798805
}
799806

@@ -846,7 +853,38 @@ class Server {
846853
};
847854
}
848855

856+
static getFreePort(port) {
857+
const pRetry = require('p-retry');
858+
const portfinder = require('portfinder');
859+
860+
if (port && port !== 'auto') {
861+
return Promise.resolve(port);
862+
}
863+
864+
function runPortFinder() {
865+
return new Promise((resolve, reject) => {
866+
// default port
867+
portfinder.basePort = 8080;
868+
portfinder.getPort((error, foundPort) => {
869+
if (error) {
870+
return reject(error);
871+
}
872+
873+
return resolve(foundPort);
874+
});
875+
});
876+
}
877+
878+
// Try to find unused port and listen on it for 3 times,
879+
// if port is not specified in options.
880+
const defaultPortRetry = parseInt(process.env.DEFAULT_PORT_RETRY, 10) || 3;
881+
882+
return pRetry(runPortFinder, { retries: defaultPortRetry });
883+
}
884+
849885
getStats(statsObj) {
886+
const getStatsOption = require('./utils/getStatsOption');
887+
850888
const stats = Server.DEFAULT_STATS;
851889

852890
const configArr = getCompilerConfigArray(this.compiler);
@@ -983,6 +1021,9 @@ class Server {
9831021
}
9841022

9851023
serveMagicHtml(req, res, next) {
1024+
const getFilenameFromUrl =
1025+
require('webpack-dev-middleware/dist/utils/getFilenameFromUrl').default;
1026+
9861027
const _path = req.path;
9871028

9881029
try {
@@ -1059,6 +1100,8 @@ class Server {
10591100
interval,
10601101
};
10611102

1103+
const chokidar = require('chokidar');
1104+
10621105
const watcher = chokidar.watch(watchPath, finalWatchOptions);
10631106

10641107
// disabling refreshing on changing the content

0 commit comments

Comments
 (0)