-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"); | ||
|
||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -355,7 +381,7 @@ class Server { | |
*/ | ||
static internalIPSync(family) { | ||
try { | ||
const { gateway } = defaultGateway[family].sync(); | ||
const { gateway } = require("default-gateway")[family].sync(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -1144,7 +1170,7 @@ class Server { | |
// Ignore error | ||
} | ||
|
||
// It is file | ||
// It is a file | ||
return stats ? fs.readFileSync(item) : item; | ||
} | ||
}; | ||
|
@@ -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())(); | ||
} | ||
|
||
/** | ||
|
@@ -2318,7 +2343,7 @@ class Server { | |
middlewares.push({ | ||
name: "express-static", | ||
path: publicPath, | ||
middleware: express.static( | ||
middleware: getExpress().static( | ||
staticOption.directory, | ||
staticOption.staticOptions | ||
), | ||
|
@@ -2373,7 +2398,7 @@ class Server { | |
middlewares.push({ | ||
name: "express-static", | ||
path: publicPath, | ||
middleware: express.static( | ||
middleware: getExpress().static( | ||
staticOption.directory, | ||
staticOption.staticOptions | ||
), | ||
|
@@ -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(); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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