Skip to content

fix: types #5057

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

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
162 changes: 70 additions & 92 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ const schema = require("./options.json");
/** @typedef {import("webpack").Stats} Stats */
/** @typedef {import("webpack").MultiStats} MultiStats */
/** @typedef {import("os").NetworkInterfaceInfo} NetworkInterfaceInfo */
/** @typedef {import("express").Request} Request */
/** @typedef {import("express").Response} Response */
/** @typedef {import("express").NextFunction} NextFunction */
/** @typedef {import("express").RequestHandler} ExpressRequestHandler */
/** @typedef {import("express").ErrorRequestHandler} ExpressErrorRequestHandler */
Expand All @@ -42,14 +40,19 @@ const schema = require("./options.json");

/** @typedef {import("https").ServerOptions & { spdy?: { plain?: boolean | undefined, ssl?: boolean | undefined, 'x-forwarded-for'?: string | undefined, protocol?: string | undefined, protocols?: string[] | undefined }}} ServerOptions */

/** @typedef {import("express").Request} Request */
/** @typedef {import("express").Response} Response */

/**
* @template Request, Response
* @typedef {import("webpack-dev-middleware").Options<IncomingMessage, ServerResponse>} DevMiddlewareOptions
* @template {Request} T
* @template {Response} U
* @typedef {import("webpack-dev-middleware").Options<T, U>} DevMiddlewareOptions
*/

/**
* @template Request, Response
* @typedef {import("webpack-dev-middleware").Context<IncomingMessage, ServerResponse>} DevMiddlewareContext
* @template {Request} T
* @template {Response} U
* @typedef {import("webpack-dev-middleware").Context<T, U>} DevMiddlewareContext
*/

/**
Expand Down Expand Up @@ -1841,36 +1844,20 @@ class Server {
const { app, middleware } = this;

/** @type {import("express").Application} */
(app).get(
"/__webpack_dev_server__/sockjs.bundle.js",
/**
* @param {Request} req
* @param {Response} res
* @returns {void}
*/
(req, res) => {
res.setHeader("Content-Type", "application/javascript");
(app).get("/__webpack_dev_server__/sockjs.bundle.js", (req, res) => {
res.setHeader("Content-Type", "application/javascript");

const clientPath = path.join(__dirname, "..", "client");
const clientPath = path.join(__dirname, "..", "client");

res.sendFile(path.join(clientPath, "modules/sockjs-client/index.js"));
},
);
res.sendFile(path.join(clientPath, "modules/sockjs-client/index.js"));
});

/** @type {import("express").Application} */
(app).get(
"/webpack-dev-server/invalidate",
/**
* @param {Request} _req
* @param {Response} res
* @returns {void}
*/
(_req, res) => {
this.invalidate();
(app).get("/webpack-dev-server/invalidate", (_req, res) => {
this.invalidate();

res.end();
},
);
res.end();
});

/** @type {import("express").Application} */
(app).get("/webpack-dev-server/open-editor", (req, res) => {
Expand All @@ -1886,74 +1873,65 @@ class Server {
});

/** @type {import("express").Application} */
(app).get(
"/webpack-dev-server",
/**
* @param {Request} req
* @param {Response} res
* @returns {void}
*/
(req, res) => {
/** @type {import("webpack-dev-middleware").API<Request, Response>}*/
(middleware).waitUntilValid((stats) => {
res.setHeader("Content-Type", "text/html");
// HEAD requests should not return body content
if (req.method === "HEAD") {
res.end();
return;
}
res.write(
'<!DOCTYPE html><html><head><meta charset="utf-8"/></head><body>',
);
(app).get("/webpack-dev-server", (req, res) => {
/** @type {import("webpack-dev-middleware").API<Request, Response>}*/
(middleware).waitUntilValid((stats) => {
res.setHeader("Content-Type", "text/html");
// HEAD requests should not return body content
if (req.method === "HEAD") {
res.end();
return;
}
res.write(
'<!DOCTYPE html><html><head><meta charset="utf-8"/></head><body>',
);

const statsForPrint =
typeof (/** @type {MultiStats} */ (stats).stats) !== "undefined"
? /** @type {MultiStats} */ (stats).toJson().children
: [/** @type {Stats} */ (stats).toJson()];
const statsForPrint =
typeof (/** @type {MultiStats} */ (stats).stats) !== "undefined"
? /** @type {MultiStats} */ (stats).toJson().children
: [/** @type {Stats} */ (stats).toJson()];

res.write(`<h1>Assets Report:</h1>`);
res.write(`<h1>Assets Report:</h1>`);

/**
* @type {StatsCompilation[]}
*/
(statsForPrint).forEach((item, index) => {
res.write("<div>");

const name =
// eslint-disable-next-line no-nested-ternary
typeof item.name !== "undefined"
? item.name
: /** @type {MultiStats} */ (stats).stats
? `unnamed[${index}]`
: "unnamed";

res.write(`<h2>Compilation: ${name}</h2>`);
res.write("<ul>");

const publicPath =
item.publicPath === "auto" ? "" : item.publicPath;

for (const asset of /** @type {NonNullable<StatsCompilation["assets"]>} */ (
item.assets
)) {
const assetName = asset.name;
const assetURL = `${publicPath}${assetName}`;

res.write(
`<li>
/**
* @type {StatsCompilation[]}
*/
(statsForPrint).forEach((item, index) => {
res.write("<div>");

const name =
// eslint-disable-next-line no-nested-ternary
typeof item.name !== "undefined"
? item.name
: /** @type {MultiStats} */ (stats).stats
? `unnamed[${index}]`
: "unnamed";

res.write(`<h2>Compilation: ${name}</h2>`);
res.write("<ul>");

const publicPath = item.publicPath === "auto" ? "" : item.publicPath;

for (const asset of /** @type {NonNullable<StatsCompilation["assets"]>} */ (
item.assets
)) {
const assetName = asset.name;
const assetURL = `${publicPath}${assetName}`;

res.write(
`<li>
<strong><a href="${assetURL}" target="_blank">${assetName}</a></strong>
</li>`,
);
}

res.write("</ul>");
res.write("</div>");
});
);
}

res.end("</body></html>");
res.write("</ul>");
res.write("</div>");
});
},
);

res.end("</body></html>");
});
});
}

/**
Expand Down
Loading