Skip to content

Commit

Permalink
fix(perf): reduced initial start time (#4818)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Apr 15, 2023
1 parent fc2c36b commit fcf01d1
Show file tree
Hide file tree
Showing 2 changed files with 573 additions and 403 deletions.
45 changes: 35 additions & 10 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ const url = require("url");
const util = require("util");
const fs = require("graceful-fs");
const ipaddr = require("ipaddr.js");
const defaultGateway = require("default-gateway");
const express = require("express");
const { validate } = require("schema-utils");
const schema = require("./options.json");

Expand Down Expand Up @@ -210,6 +208,34 @@ if (!process.env.WEBPACK_SERVE) {
process.env.WEBPACK_SERVE = true;
}

/**
* @template T
* @param fn {(function(): any) | undefined}
* @returns {function(): T}
*/
const memoize = (fn) => {
let cache = false;
/** @type {T} */
let result;

return () => {
if (cache) {
return result;
}

result = /** @type {function(): any} */ (fn)();
cache = true;
// Allow to clean up memory for fn
// and all dependent resources
// eslint-disable-next-line no-undefined
fn = undefined;

return result;
};
};

const getExpress = memoize(() => require("express"));

class Server {
/**
* @param {Configuration | Compiler | MultiCompiler} options
Expand Down Expand Up @@ -342,7 +368,7 @@ class Server {
*/
static async internalIP(family) {
try {
const { gateway } = await defaultGateway[family]();
const { gateway } = await require("default-gateway")[family]();
return Server.findIp(gateway);
} catch {
// ignore
Expand All @@ -355,7 +381,7 @@ class Server {
*/
static internalIPSync(family) {
try {
const { gateway } = defaultGateway[family].sync();
const { gateway } = require("default-gateway")[family].sync();
return Server.findIp(gateway);
} catch {
// ignore
Expand Down Expand Up @@ -1144,7 +1170,7 @@ class Server {
// Ignore error
}

// It is file
// It is a file
return stats ? fs.readFileSync(item) : item;
}
};
Expand Down Expand Up @@ -1898,8 +1924,7 @@ class Server {
*/
setupApp() {
/** @type {import("express").Application | undefined}*/
// eslint-disable-next-line new-cap
this.app = new /** @type {any} */ (express)();
this.app = new /** @type {any} */ (getExpress())();
}

/**
Expand Down Expand Up @@ -2318,7 +2343,7 @@ class Server {
middlewares.push({
name: "express-static",
path: publicPath,
middleware: express.static(
middleware: getExpress().static(
staticOption.directory,
staticOption.staticOptions
),
Expand Down Expand Up @@ -2373,7 +2398,7 @@ class Server {
middlewares.push({
name: "express-static",
path: publicPath,
middleware: express.static(
middleware: getExpress().static(
staticOption.directory,
staticOption.staticOptions
),
Expand Down Expand Up @@ -3257,7 +3282,7 @@ class Server {
*/
(error) => {
if (error.code === "ECONNREFUSED") {
// No other server listening on this socket so it can be safely removed
// No other server listening on this socket, so it can be safely removed
fs.unlinkSync(/** @type {string} */ (this.options.ipc));

resolve();
Expand Down
Loading

0 comments on commit fcf01d1

Please sign in to comment.