Skip to content

Commit

Permalink
fix: types (#5057)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Feb 16, 2024
1 parent 0c4c6b7 commit da2c24d
Show file tree
Hide file tree
Showing 2 changed files with 342 additions and 251 deletions.
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

0 comments on commit da2c24d

Please sign in to comment.