@@ -6,8 +6,6 @@ const url = require("url");
66const util = require ( "util" ) ;
77const fs = require ( "graceful-fs" ) ;
88const ipaddr = require ( "ipaddr.js" ) ;
9- const defaultGateway = require ( "default-gateway" ) ;
10- const express = require ( "express" ) ;
119const { validate } = require ( "schema-utils" ) ;
1210const schema = require ( "./options.json" ) ;
1311
@@ -210,6 +208,34 @@ if (!process.env.WEBPACK_SERVE) {
210208 process . env . WEBPACK_SERVE = true ;
211209}
212210
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+
213239class Server {
214240 /**
215241 * @param {Configuration | Compiler | MultiCompiler } options
@@ -342,7 +368,7 @@ class Server {
342368 */
343369 static async internalIP ( family ) {
344370 try {
345- const { gateway } = await defaultGateway [ family ] ( ) ;
371+ const { gateway } = await require ( "default-gateway" ) [ family ] ( ) ;
346372 return Server . findIp ( gateway ) ;
347373 } catch {
348374 // ignore
@@ -355,7 +381,7 @@ class Server {
355381 */
356382 static internalIPSync ( family ) {
357383 try {
358- const { gateway } = defaultGateway [ family ] . sync ( ) ;
384+ const { gateway } = require ( "default-gateway" ) [ family ] . sync ( ) ;
359385 return Server . findIp ( gateway ) ;
360386 } catch {
361387 // ignore
@@ -1144,7 +1170,7 @@ class Server {
11441170 // Ignore error
11451171 }
11461172
1147- // It is file
1173+ // It is a file
11481174 return stats ? fs . readFileSync ( item ) : item ;
11491175 }
11501176 } ;
@@ -1898,8 +1924,7 @@ class Server {
18981924 */
18991925 setupApp ( ) {
19001926 /** @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 ( ) ) ( ) ;
19031928 }
19041929
19051930 /**
@@ -2318,7 +2343,7 @@ class Server {
23182343 middlewares . push ( {
23192344 name : "express-static" ,
23202345 path : publicPath ,
2321- middleware : express . static (
2346+ middleware : getExpress ( ) . static (
23222347 staticOption . directory ,
23232348 staticOption . staticOptions
23242349 ) ,
@@ -2373,7 +2398,7 @@ class Server {
23732398 middlewares . push ( {
23742399 name : "express-static" ,
23752400 path : publicPath ,
2376- middleware : express . static (
2401+ middleware : getExpress ( ) . static (
23772402 staticOption . directory ,
23782403 staticOption . staticOptions
23792404 ) ,
@@ -3257,7 +3282,7 @@ class Server {
32573282 */
32583283 ( error ) => {
32593284 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
32613286 fs . unlinkSync ( /** @type {string } */ ( this . options . ipc ) ) ;
32623287
32633288 resolve ( ) ;
0 commit comments