@@ -6,8 +6,6 @@ const url = require("url");
6
6
const util = require ( "util" ) ;
7
7
const fs = require ( "graceful-fs" ) ;
8
8
const ipaddr = require ( "ipaddr.js" ) ;
9
- const defaultGateway = require ( "default-gateway" ) ;
10
- const express = require ( "express" ) ;
11
9
const { validate } = require ( "schema-utils" ) ;
12
10
const schema = require ( "./options.json" ) ;
13
11
@@ -210,6 +208,34 @@ if (!process.env.WEBPACK_SERVE) {
210
208
process . env . WEBPACK_SERVE = true ;
211
209
}
212
210
211
+ /**
212
+ * @template T
213
+ * @param fn {(function(): any) | undefined}
214
+ * @returns {function(): T }
215
+ */
216
+ const memoize = ( fn ) => {
217
+ let cache = false ;
218
+ /** @type {T } */
219
+ let result ;
220
+
221
+ return ( ) => {
222
+ if ( cache ) {
223
+ return result ;
224
+ }
225
+
226
+ result = /** @type {function(): any } */ ( fn ) ( ) ;
227
+ cache = true ;
228
+ // Allow to clean up memory for fn
229
+ // and all dependent resources
230
+ // eslint-disable-next-line no-undefined
231
+ fn = undefined ;
232
+
233
+ return result ;
234
+ } ;
235
+ } ;
236
+
237
+ const getExpress = memoize ( ( ) => require ( "express" ) ) ;
238
+
213
239
class Server {
214
240
/**
215
241
* @param {Configuration | Compiler | MultiCompiler } options
@@ -342,7 +368,7 @@ class Server {
342
368
*/
343
369
static async internalIP ( family ) {
344
370
try {
345
- const { gateway } = await defaultGateway [ family ] ( ) ;
371
+ const { gateway } = await require ( "default-gateway" ) [ family ] ( ) ;
346
372
return Server . findIp ( gateway ) ;
347
373
} catch {
348
374
// ignore
@@ -355,7 +381,7 @@ class Server {
355
381
*/
356
382
static internalIPSync ( family ) {
357
383
try {
358
- const { gateway } = defaultGateway [ family ] . sync ( ) ;
384
+ const { gateway } = require ( "default-gateway" ) [ family ] . sync ( ) ;
359
385
return Server . findIp ( gateway ) ;
360
386
} catch {
361
387
// ignore
@@ -1144,7 +1170,7 @@ class Server {
1144
1170
// Ignore error
1145
1171
}
1146
1172
1147
- // It is file
1173
+ // It is a file
1148
1174
return stats ? fs . readFileSync ( item ) : item ;
1149
1175
}
1150
1176
} ;
@@ -1898,8 +1924,7 @@ class Server {
1898
1924
*/
1899
1925
setupApp ( ) {
1900
1926
/** @type {import("express").Application | undefined }*/
1901
- // eslint-disable-next-line new-cap
1902
- this . app = new /** @type {any } */ ( express ) ( ) ;
1927
+ this . app = new /** @type {any } */ ( getExpress ( ) ) ( ) ;
1903
1928
}
1904
1929
1905
1930
/**
@@ -2318,7 +2343,7 @@ class Server {
2318
2343
middlewares . push ( {
2319
2344
name : "express-static" ,
2320
2345
path : publicPath ,
2321
- middleware : express . static (
2346
+ middleware : getExpress ( ) . static (
2322
2347
staticOption . directory ,
2323
2348
staticOption . staticOptions
2324
2349
) ,
@@ -2373,7 +2398,7 @@ class Server {
2373
2398
middlewares . push ( {
2374
2399
name : "express-static" ,
2375
2400
path : publicPath ,
2376
- middleware : express . static (
2401
+ middleware : getExpress ( ) . static (
2377
2402
staticOption . directory ,
2378
2403
staticOption . staticOptions
2379
2404
) ,
@@ -3257,7 +3282,7 @@ class Server {
3257
3282
*/
3258
3283
( error ) => {
3259
3284
if ( error . code === "ECONNREFUSED" ) {
3260
- // No other server listening on this socket so it can be safely removed
3285
+ // No other server listening on this socket, so it can be safely removed
3261
3286
fs . unlinkSync ( /** @type {string } */ ( this . options . ipc ) ) ;
3262
3287
3263
3288
resolve ( ) ;
0 commit comments