Skip to content

Commit feabc4d

Browse files
refactor: built-in routers
1 parent d6fce1b commit feabc4d

File tree

1 file changed

+192
-161
lines changed

1 file changed

+192
-161
lines changed

lib/Server.js

Lines changed: 192 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,164 +1887,6 @@ class Server {
18871887
);
18881888
}
18891889

1890-
/**
1891-
* @private
1892-
* @returns {void}
1893-
*/
1894-
setupBuiltInRoutes() {
1895-
const { app, middleware } = this;
1896-
1897-
/** @type {A} */
1898-
(app).use("/__webpack_dev_server__/sockjs.bundle.js", (req, res, next) => {
1899-
if (req.method !== "GET" && req.method !== "HEAD") {
1900-
next();
1901-
return;
1902-
}
1903-
1904-
const clientPath = path.join(
1905-
__dirname,
1906-
"..",
1907-
"client/modules/sockjs-client/index.js",
1908-
);
1909-
1910-
// Express send Etag and other headers by default, so let's keep them for compatibility reasons
1911-
// @ts-ignore
1912-
if (typeof res.sendFile === "function") {
1913-
// @ts-ignore
1914-
res.sendFile(clientPath);
1915-
return;
1916-
}
1917-
1918-
let stats;
1919-
1920-
try {
1921-
// TODO implement `inputFileSystem.createReadStream` in webpack
1922-
stats = fs.statSync(clientPath);
1923-
} catch (err) {
1924-
next();
1925-
return;
1926-
}
1927-
1928-
res.setHeader("Content-Type", "application/javascript; charset=UTF-8");
1929-
res.setHeader("Content-Length", stats.size);
1930-
1931-
if (req.method === "HEAD") {
1932-
res.end();
1933-
return;
1934-
}
1935-
1936-
fs.createReadStream(clientPath).pipe(res);
1937-
});
1938-
1939-
/** @type {A} */
1940-
(app).use("/webpack-dev-server/invalidate", (req, res, next) => {
1941-
if (req.method !== "GET" && req.method !== "HEAD") {
1942-
next();
1943-
return;
1944-
}
1945-
1946-
this.invalidate();
1947-
1948-
res.end();
1949-
});
1950-
1951-
/** @type {A} */
1952-
(app).use("/webpack-dev-server/open-editor", (req, res, next) => {
1953-
if (req.method !== "GET" && req.method !== "HEAD") {
1954-
next();
1955-
return;
1956-
}
1957-
1958-
if (!req.url) {
1959-
next();
1960-
return;
1961-
}
1962-
1963-
const resolveUrl = new URL(req.url, `http://${req.headers.host}`);
1964-
const params = new URLSearchParams(resolveUrl.search);
1965-
const fileName = params.get("fileName");
1966-
1967-
if (typeof fileName === "string") {
1968-
// @ts-ignore
1969-
const launchEditor = require("launch-editor");
1970-
1971-
launchEditor(fileName);
1972-
}
1973-
1974-
res.end();
1975-
});
1976-
1977-
/** @type {A} */
1978-
(app).use("/webpack-dev-server", (req, res, next) => {
1979-
if (req.method !== "GET" && req.method !== "HEAD") {
1980-
next();
1981-
return;
1982-
}
1983-
1984-
/** @type {import("webpack-dev-middleware").API<Request, Response>}*/
1985-
(middleware).waitUntilValid((stats) => {
1986-
res.setHeader("Content-Type", "text/html; charset=utf-8");
1987-
1988-
// HEAD requests should not return body content
1989-
if (req.method === "HEAD") {
1990-
res.end();
1991-
return;
1992-
}
1993-
1994-
res.write(
1995-
'<!DOCTYPE html><html><head><meta charset="utf-8"/></head><body>',
1996-
);
1997-
1998-
/**
1999-
* @type {StatsCompilation[]}
2000-
*/
2001-
const statsForPrint =
2002-
typeof (/** @type {MultiStats} */ (stats).stats) !== "undefined"
2003-
? /** @type {NonNullable<StatsCompilation["children"]>} */
2004-
(/** @type {MultiStats} */ (stats).toJson().children)
2005-
: [/** @type {Stats} */ (stats).toJson()];
2006-
2007-
res.write(`<h1>Assets Report:</h1>`);
2008-
2009-
for (const [index, item] of statsForPrint.entries()) {
2010-
res.write("<div>");
2011-
2012-
const name =
2013-
// eslint-disable-next-line no-nested-ternary
2014-
typeof item.name !== "undefined"
2015-
? item.name
2016-
: /** @type {MultiStats} */ (stats).stats
2017-
? `unnamed[${index}]`
2018-
: "unnamed";
2019-
2020-
res.write(`<h2>Compilation: ${name}</h2>`);
2021-
res.write("<ul>");
2022-
2023-
const publicPath = item.publicPath === "auto" ? "" : item.publicPath;
2024-
const assets =
2025-
/** @type {NonNullable<StatsCompilation["assets"]>} */
2026-
(item.assets);
2027-
2028-
for (const asset of assets) {
2029-
const assetName = asset.name;
2030-
const assetURL = `${publicPath}${assetName}`;
2031-
2032-
res.write(
2033-
`<li>
2034-
<strong><a href="${assetURL}" target="_blank">${assetName}</a></strong>
2035-
</li>`,
2036-
);
2037-
}
2038-
2039-
res.write("</ul>");
2040-
res.write("</div>");
2041-
}
2042-
2043-
res.end("</body></html>");
2044-
});
2045-
});
2046-
}
2047-
20481890
/**
20491891
* @private
20501892
* @returns {void}
@@ -2110,9 +1952,6 @@ class Server {
21101952
},
21111953
});
21121954

2113-
// Should be after `webpack-dev-middleware`, otherwise other middlewares might rewrite response
2114-
// this.setupBuiltInRoutes();
2115-
21161955
const isHTTP2 =
21171956
/** @type {ServerConfiguration} */ (this.options.server).type === "http2";
21181957

@@ -2163,6 +2002,198 @@ class Server {
21632002
middleware: devMiddleware,
21642003
});
21652004

2005+
// Should be after `webpack-dev-middleware`, otherwise other middlewares might rewrite response
2006+
middlewares.push({
2007+
name: "webpack-dev-server-sockjs-bundle",
2008+
path: "/__webpack_dev_server__/sockjs.bundle.js",
2009+
/**
2010+
* @param {Request} req
2011+
* @param {Response} res
2012+
* @param {NextFunction} next
2013+
* @returns {void}
2014+
*/
2015+
middleware: (req, res, next) => {
2016+
if (req.method !== "GET" && req.method !== "HEAD") {
2017+
next();
2018+
return;
2019+
}
2020+
2021+
const clientPath = path.join(
2022+
__dirname,
2023+
"..",
2024+
"client/modules/sockjs-client/index.js",
2025+
);
2026+
2027+
// Express send Etag and other headers by default, so let's keep them for compatibility reasons
2028+
// @ts-ignore
2029+
if (typeof res.sendFile === "function") {
2030+
// @ts-ignore
2031+
res.sendFile(clientPath);
2032+
return;
2033+
}
2034+
2035+
let stats;
2036+
2037+
try {
2038+
// TODO implement `inputFileSystem.createReadStream` in webpack
2039+
stats = fs.statSync(clientPath);
2040+
} catch (err) {
2041+
next();
2042+
return;
2043+
}
2044+
2045+
res.setHeader("Content-Type", "application/javascript; charset=UTF-8");
2046+
res.setHeader("Content-Length", stats.size);
2047+
2048+
if (req.method === "HEAD") {
2049+
res.end();
2050+
return;
2051+
}
2052+
2053+
fs.createReadStream(clientPath).pipe(res);
2054+
},
2055+
});
2056+
2057+
middlewares.push({
2058+
name: "webpack-dev-server-invalidate",
2059+
path: "/webpack-dev-server/invalidate",
2060+
/**
2061+
* @param {Request} req
2062+
* @param {Response} res
2063+
* @param {NextFunction} next
2064+
* @returns {void}
2065+
*/
2066+
middleware: (req, res, next) => {
2067+
if (req.method !== "GET" && req.method !== "HEAD") {
2068+
next();
2069+
return;
2070+
}
2071+
2072+
this.invalidate();
2073+
2074+
res.end();
2075+
},
2076+
});
2077+
2078+
middlewares.push({
2079+
name: "webpack-dev-server-open-editor",
2080+
path: "/webpack-dev-server/open-editor",
2081+
/**
2082+
* @param {Request} req
2083+
* @param {Response} res
2084+
* @param {NextFunction} next
2085+
* @returns {void}
2086+
*/
2087+
middleware: (req, res, next) => {
2088+
if (req.method !== "GET" && req.method !== "HEAD") {
2089+
next();
2090+
return;
2091+
}
2092+
2093+
if (!req.url) {
2094+
next();
2095+
return;
2096+
}
2097+
2098+
const resolveUrl = new URL(req.url, `http://${req.headers.host}`);
2099+
const params = new URLSearchParams(resolveUrl.search);
2100+
const fileName = params.get("fileName");
2101+
2102+
if (typeof fileName === "string") {
2103+
// @ts-ignore
2104+
const launchEditor = require("launch-editor");
2105+
2106+
launchEditor(fileName);
2107+
}
2108+
2109+
res.end();
2110+
},
2111+
});
2112+
2113+
middlewares.push({
2114+
name: "webpack-dev-server-assets",
2115+
path: "/webpack-dev-server",
2116+
/**
2117+
* @param {Request} req
2118+
* @param {Response} res
2119+
* @param {NextFunction} next
2120+
* @returns {void}
2121+
*/
2122+
middleware: (req, res, next) => {
2123+
if (req.method !== "GET" && req.method !== "HEAD") {
2124+
next();
2125+
return;
2126+
}
2127+
2128+
if (!this.middleware) {
2129+
next();
2130+
return;
2131+
}
2132+
2133+
this.middleware.waitUntilValid((stats) => {
2134+
res.setHeader("Content-Type", "text/html; charset=utf-8");
2135+
2136+
// HEAD requests should not return body content
2137+
if (req.method === "HEAD") {
2138+
res.end();
2139+
return;
2140+
}
2141+
2142+
res.write(
2143+
'<!DOCTYPE html><html><head><meta charset="utf-8"/></head><body>',
2144+
);
2145+
2146+
/**
2147+
* @type {StatsCompilation[]}
2148+
*/
2149+
const statsForPrint =
2150+
typeof (/** @type {MultiStats} */ (stats).stats) !== "undefined"
2151+
? /** @type {NonNullable<StatsCompilation["children"]>} */
2152+
(/** @type {MultiStats} */ (stats).toJson().children)
2153+
: [/** @type {Stats} */ (stats).toJson()];
2154+
2155+
res.write(`<h1>Assets Report:</h1>`);
2156+
2157+
for (const [index, item] of statsForPrint.entries()) {
2158+
res.write("<div>");
2159+
2160+
const name =
2161+
// eslint-disable-next-line no-nested-ternary
2162+
typeof item.name !== "undefined"
2163+
? item.name
2164+
: /** @type {MultiStats} */ (stats).stats
2165+
? `unnamed[${index}]`
2166+
: "unnamed";
2167+
2168+
res.write(`<h2>Compilation: ${name}</h2>`);
2169+
res.write("<ul>");
2170+
2171+
const publicPath =
2172+
item.publicPath === "auto" ? "" : item.publicPath;
2173+
const assets =
2174+
/** @type {NonNullable<StatsCompilation["assets"]>} */
2175+
(item.assets);
2176+
2177+
for (const asset of assets) {
2178+
const assetName = asset.name;
2179+
const assetURL = `${publicPath}${assetName}`;
2180+
2181+
res.write(
2182+
`<li>
2183+
<strong><a href="${assetURL}" target="_blank">${assetName}</a></strong>
2184+
</li>`,
2185+
);
2186+
}
2187+
2188+
res.write("</ul>");
2189+
res.write("</div>");
2190+
}
2191+
2192+
res.end("</body></html>");
2193+
});
2194+
},
2195+
});
2196+
21662197
if (this.options.proxy) {
21672198
const { createProxyMiddleware } = require("http-proxy-middleware");
21682199

0 commit comments

Comments
 (0)