2
2
3
3
const path = require ( 'path' ) ;
4
4
const url = require ( 'url' ) ;
5
- const http = require ( 'http' ) ;
6
- const https = require ( 'https' ) ;
7
5
const fs = require ( 'graceful-fs' ) ;
8
6
const ipaddr = require ( 'ipaddr.js' ) ;
9
7
const internalIp = require ( 'internal-ip' ) ;
10
8
const killable = require ( 'killable' ) ;
11
- const chokidar = require ( 'chokidar' ) ;
12
9
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 ;
21
10
const { validate } = require ( 'schema-utils' ) ;
22
- const DevServerPlugin = require ( './utils/DevServerPlugin' ) ;
23
11
const normalizeOptions = require ( './utils/normalizeOptions' ) ;
24
- const getCertificate = require ( './utils/getCertificate' ) ;
25
12
const colors = require ( './utils/colors' ) ;
26
- const runOpen = require ( './utils/runOpen' ) ;
27
- const runBonjour = require ( './utils/runBonjour' ) ;
28
13
const routes = require ( './utils/routes' ) ;
29
14
const getSocketServerImplementation = require ( './utils/getSocketServerImplementation' ) ;
30
15
const getCompilerConfigArray = require ( './utils/getCompilerConfigArray' ) ;
31
- const getStatsOption = require ( './utils/getStatsOption' ) ;
32
- const getColorsOption = require ( './utils/getColorsOption' ) ;
33
16
const setupExitSignals = require ( './utils/setupExitSignals' ) ;
34
- const findPort = require ( './utils/findPort' ) ;
35
17
const schema = require ( './options.json' ) ;
36
18
37
19
if ( ! process . env . WEBPACK_SERVE ) {
@@ -103,6 +85,8 @@ class Server {
103
85
}
104
86
105
87
applyDevServerPlugin ( ) {
88
+ const DevServerPlugin = require ( './utils/DevServerPlugin' ) ;
89
+
106
90
const compilers = this . compiler . compilers || [ this . compiler ] ;
107
91
108
92
// eslint-disable-next-line no-shadow
@@ -112,7 +96,9 @@ class Server {
112
96
}
113
97
114
98
setupProgressPlugin ( ) {
115
- new webpack . ProgressPlugin ( ( percent , msg , addInfo ) => {
99
+ const { ProgressPlugin } = require ( 'webpack' ) ;
100
+
101
+ new ProgressPlugin ( ( percent , msg , addInfo ) => {
116
102
percent = Math . floor ( percent * 100 ) ;
117
103
118
104
if ( percent === 100 ) {
@@ -172,6 +158,8 @@ class Server {
172
158
}
173
159
174
160
setupDevMiddleware ( ) {
161
+ const webpackDevMiddleware = require ( 'webpack-dev-middleware' ) ;
162
+
175
163
// middleware for serving webpack bundle
176
164
this . middleware = webpackDevMiddleware (
177
165
this . compiler ,
@@ -180,10 +168,14 @@ class Server {
180
168
}
181
169
182
170
setupCompressFeature ( ) {
171
+ const compress = require ( 'compression' ) ;
172
+
183
173
this . app . use ( compress ( ) ) ;
184
174
}
185
175
186
176
setupProxyFeature ( ) {
177
+ const { createProxyMiddleware } = require ( 'http-proxy-middleware' ) ;
178
+
187
179
/**
188
180
* Assume a proxy configuration specified as:
189
181
* proxy: {
@@ -329,6 +321,8 @@ class Server {
329
321
}
330
322
331
323
setupHistoryApiFallbackFeature ( ) {
324
+ const historyApiFallback = require ( 'connect-history-api-fallback' ) ;
325
+
332
326
const fallback =
333
327
typeof this . options . historyApiFallback === 'object'
334
328
? this . options . historyApiFallback
@@ -350,6 +344,8 @@ class Server {
350
344
}
351
345
352
346
setupStaticServeIndexFeature ( ) {
347
+ const serveIndex = require ( 'serve-index' ) ;
348
+
353
349
this . options . static . forEach ( ( staticOption ) => {
354
350
staticOption . publicPath . forEach ( ( publicPath ) => {
355
351
if ( staticOption . serveIndex ) {
@@ -524,6 +520,8 @@ class Server {
524
520
}
525
521
526
522
if ( this . options . https ) {
523
+ const getCertificate = require ( './utils/getCertificate' ) ;
524
+
527
525
for ( const property of [ 'cacert' , 'pfx' , 'key' , 'cert' ] ) {
528
526
const value = this . options . https [ property ] ;
529
527
const isBuffer = value instanceof Buffer ;
@@ -556,6 +554,9 @@ class Server {
556
554
}
557
555
558
556
createServer ( ) {
557
+ const https = require ( 'https' ) ;
558
+ const http = require ( 'http' ) ;
559
+
559
560
if ( this . options . https ) {
560
561
if ( this . options . http2 ) {
561
562
// TODO: we need to replace spdy with http2 which is an internal module
@@ -590,7 +591,7 @@ class Server {
590
591
591
592
if ( ! headers ) {
592
593
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) ' +
594
595
'via f(connection, headers) in order for clients to pass a headers security check'
595
596
) ;
596
597
}
@@ -638,6 +639,8 @@ class Server {
638
639
}
639
640
640
641
showStatus ( ) {
642
+ const getColorsOption = require ( './utils/getColorsOption' ) ;
643
+
641
644
const useColor = getColorsOption ( getCompilerConfigArray ( this . compiler ) ) ;
642
645
const protocol = this . options . https ? 'https' : 'http' ;
643
646
const { address, port } = this . server . address ( ) ;
@@ -759,6 +762,8 @@ class Server {
759
762
}
760
763
761
764
if ( this . options . open ) {
765
+ const runOpen = require ( './utils/runOpen' ) ;
766
+
762
767
const openTarget = prettyPrintUrl ( this . hostname || 'localhost' ) ;
763
768
764
769
runOpen ( openTarget , this . options . open , this . logger ) ;
@@ -783,7 +788,7 @@ class Server {
783
788
}
784
789
785
790
return (
786
- findPort ( port || this . options . port )
791
+ Server . getFreePort ( port || this . options . port )
787
792
// eslint-disable-next-line no-shadow
788
793
. then ( ( port ) => {
789
794
this . options . port = port ;
@@ -794,6 +799,8 @@ class Server {
794
799
}
795
800
796
801
if ( this . options . bonjour ) {
802
+ const runBonjour = require ( './utils/runBonjour' ) ;
803
+
797
804
runBonjour ( this . options ) ;
798
805
}
799
806
@@ -846,7 +853,38 @@ class Server {
846
853
} ;
847
854
}
848
855
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
+
849
885
getStats ( statsObj ) {
886
+ const getStatsOption = require ( './utils/getStatsOption' ) ;
887
+
850
888
const stats = Server . DEFAULT_STATS ;
851
889
852
890
const configArr = getCompilerConfigArray ( this . compiler ) ;
@@ -983,6 +1021,9 @@ class Server {
983
1021
}
984
1022
985
1023
serveMagicHtml ( req , res , next ) {
1024
+ const getFilenameFromUrl =
1025
+ require ( 'webpack-dev-middleware/dist/utils/getFilenameFromUrl' ) . default ;
1026
+
986
1027
const _path = req . path ;
987
1028
988
1029
try {
@@ -1059,6 +1100,8 @@ class Server {
1059
1100
interval,
1060
1101
} ;
1061
1102
1103
+ const chokidar = require ( 'chokidar' ) ;
1104
+
1062
1105
const watcher = chokidar . watch ( watchPath , finalWatchOptions ) ;
1063
1106
1064
1107
// disabling refreshing on changing the content
0 commit comments