Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(perf): reduced initial start time #4818

Merged
merged 2 commits into from
Apr 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we always run express, but let's do it lazy


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();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use only one method or async or sync, so it is fine to use require

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we don't it when a developer provide IP, so avoid it for some cases

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