Skip to content

Commit 20c2628

Browse files
refactor: improve perf
1 parent 67102c1 commit 20c2628

1 file changed

Lines changed: 25 additions & 50 deletions

File tree

src/middleware.js

Lines changed: 25 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -40,49 +40,6 @@ const {
4040

4141
const BYTES_RANGE_REGEXP = /^ *bytes/i;
4242

43-
/**
44-
* @template {IncomingMessage} Request
45-
* @template {ServerResponse} Response
46-
* @param {import("./index.js").FilledContext<Request, Response>} context context
47-
* @returns {{ outputPath: string, publicPath: string, assetsInfo: Asset["info"] }[]} paths
48-
*/
49-
function getPaths(context) {
50-
const { stats, options } = context;
51-
/* eslint-disable unicorn/prefer-logical-operator-over-ternary */
52-
/** @type {Stats[]} */
53-
const childStats =
54-
/** @type {MultiStats} */
55-
(stats).stats
56-
? /** @type {MultiStats} */ (stats).stats
57-
: [/** @type {Stats} */ (stats)];
58-
/** @type {{ outputPath: string, publicPath: string, assetsInfo: Asset["info"] }[]} */
59-
const publicPaths = [];
60-
61-
for (const { compilation } of childStats) {
62-
if (compilation.options.devServer === false) {
63-
continue;
64-
}
65-
66-
// The `output.path` is always present and always absolute
67-
const outputPath = compilation.getPath(
68-
compilation.outputOptions.path || "",
69-
);
70-
const publicPath = options.publicPath
71-
? compilation.getPath(options.publicPath)
72-
: compilation.outputOptions.publicPath
73-
? compilation.getPath(compilation.outputOptions.publicPath)
74-
: "";
75-
76-
publicPaths.push({
77-
outputPath,
78-
publicPath,
79-
assetsInfo: compilation.assetsInfo,
80-
});
81-
}
82-
83-
return publicPaths;
84-
}
85-
8643
/**
8744
* @param {string} input input
8845
* @returns {string} unescape input
@@ -148,21 +105,29 @@ function getFilenameFromUrl(context, url) {
148105
return;
149106
}
150107

151-
const { options } = context;
152-
const paths = getPaths(context);
153-
108+
const { options, stats } = context;
154109
/** @type {Extra} */
155110
const extra = {};
156111

157-
for (const { publicPath, outputPath, assetsInfo } of paths) {
158-
/** @type {string | undefined} */
159-
let filename;
112+
/** @type {Stats[]} */
113+
const allStats =
114+
/** @type {MultiStats} */
115+
(stats).stats || [/** @type {Stats} */ (stats)];
116+
117+
for (const { compilation } of allStats) {
118+
if (compilation.options.devServer === false) {
119+
continue;
120+
}
121+
160122
/** @type {URL} */
161123
let publicPathObject;
162124

125+
const publicPath =
126+
options.publicPath || compilation.options.output.publicPath || "";
127+
163128
try {
164129
publicPathObject = memoizedParse(
165-
publicPath !== "auto" && publicPath ? publicPath : "/",
130+
publicPath === "auto" ? "/" : compilation.getPath(publicPath),
166131
);
167132
} catch {
168133
continue;
@@ -171,6 +136,9 @@ function getFilenameFromUrl(context, url) {
171136
const { pathname } = urlObject;
172137
const { pathname: publicPathPathname } = publicPathObject;
173138

139+
/** @type {string | undefined} */
140+
let filename;
141+
174142
if (
175143
pathname &&
176144
publicPathPathname &&
@@ -186,6 +154,11 @@ function getFilenameFromUrl(context, url) {
186154
throw new FilenameError("Forbidden", 403);
187155
}
188156

157+
// The `output.path` is always present and always absolute
158+
const outputPath = compilation.getPath(
159+
compilation.outputOptions.path || "",
160+
);
161+
189162
// Strip the `pathname` property from the `publicPath` option from the start of requested url
190163
// `/complex/foo.js` => `foo.js`
191164
// and add outputPath
@@ -201,6 +174,8 @@ function getFilenameFromUrl(context, url) {
201174
continue;
202175
}
203176

177+
const { assetsInfo } = compilation;
178+
204179
if (extra.stats.isFile()) {
205180
foundFilename = filename;
206181

0 commit comments

Comments
 (0)