Skip to content

Commit b4c7dfc

Browse files
authored
perf: lazily resolve immutable asset info (#38)
1 parent 5667228 commit b4c7dfc

3 files changed

Lines changed: 37 additions & 32 deletions

File tree

src/utils/getFilenameFromUrl.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ function getFilenameFromUrl(context, url) {
9999
}
100100

101101
for (const {
102+
compilation,
102103
publicPath,
103104
outputPath,
104-
assetsInfo,
105105
outputFileSystem,
106106
} of paths) {
107107
/** @type {string | undefined} */
@@ -144,6 +144,28 @@ function getFilenameFromUrl(context, url) {
144144
pathname.slice(publicPathPathname.length),
145145
);
146146

147+
/** @type {boolean | undefined} */
148+
let immutable = undefined;
149+
150+
/**
151+
* @param {FSStats} stats stats
152+
* @returns {Extra} extra
153+
*/
154+
const createExtra = (stats) => ({
155+
// Lazy evaluate immutable because it may be expensive to get asset info
156+
get immutable() {
157+
if (immutable === undefined) {
158+
const assetName = pathname.slice(publicPathPathname.length);
159+
immutable = Boolean(
160+
compilation.getAsset(assetName)?.info?.immutable,
161+
);
162+
}
163+
return immutable;
164+
},
165+
outputFileSystem,
166+
stats,
167+
});
168+
147169
/**
148170
* @param {string} filename filename
149171
* @param {Set<string>=} visited visited filenames
@@ -182,17 +204,10 @@ function getFilenameFromUrl(context, url) {
182204
return resolveIndex(filename, visited);
183205
}
184206

185-
/** @type {Extra} */
186-
const extra = {
187-
immutable: assetsInfo
188-
? assetsInfo.get(pathname.slice(publicPathPathname.length))
189-
?.immutable
190-
: false,
191-
outputFileSystem,
192-
stats: /** @type {FSStats} */ (stats),
207+
return {
208+
filename,
209+
extra: createExtra(/** @type {FSStats} */ (stats)),
193210
};
194-
195-
return { filename, extra };
196211
};
197212

198213
/**
@@ -221,17 +236,10 @@ function getFilenameFromUrl(context, url) {
221236
return;
222237
}
223238

224-
/** @type {Extra} */
225-
const extra = {
226-
immutable: assetsInfo
227-
? assetsInfo.get(pathname.slice(publicPathPathname.length))
228-
?.immutable
229-
: false,
230-
outputFileSystem,
231-
stats: /** @type {FSStats} */ (stats),
239+
return {
240+
filename,
241+
extra: createExtra(/** @type {FSStats} */ (stats)),
232242
};
233-
234-
return { filename, extra };
235243
};
236244

237245
if (index.length > 0 && pathname.endsWith("/")) {

src/utils/getPaths.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @typedef {import("@rspack/core").Compiler} Compiler */
2+
/** @typedef {import("@rspack/core").Compilation} Compilation */
23
/** @typedef {import("@rspack/core").Stats} Stats */
34
/** @typedef {import("@rspack/core").MultiStats} MultiStats */
4-
/** @typedef {import("@rspack/core").Asset} Asset */
55
/** @typedef {import("../index.js").DevServerOption} DevServerOption */
66
/** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
77
/** @typedef {import("../index.js").OutputFileSystem} OutputFileSystem */
@@ -11,7 +11,7 @@
1111
* @template {IncomingMessage} Request
1212
* @template {ServerResponse} Response
1313
* @param {import("../index.js").FilledContext<Request, Response>} context context
14-
* @returns {{ outputPath: string, outputFileSystem: OutputFileSystem, publicPath: string, assetsInfo: Map<string, Asset["info"]> | undefined }[]} paths
14+
* @returns {{ compilation: Compilation, outputPath: string, outputFileSystem: OutputFileSystem, publicPath: string }[]} paths
1515
*/
1616
function getPaths(context) {
1717
const { stats, options } = context;
@@ -22,7 +22,7 @@ function getPaths(context) {
2222
(stats).stats
2323
? /** @type {MultiStats} */ (stats).stats
2424
: [/** @type {Stats} */ (stats)];
25-
/** @type {{ outputPath: string, outputFileSystem: OutputFileSystem, publicPath: string, assetsInfo: Map<string, Asset["info"]> | undefined }[]} */
25+
/** @type {{ compilation: Compilation, outputPath: string, outputFileSystem: OutputFileSystem, publicPath: string }[]} */
2626
const publicPaths = [];
2727

2828
for (const { compilation } of childStats) {
@@ -44,19 +44,16 @@ function getPaths(context) {
4444
/** @type {any} */ (compilation.outputOptions.publicPath),
4545
)
4646
: "";
47-
const assetsInfo = new Map(
48-
compilation.getAssets().map((asset) => [asset.name, asset.info]),
49-
);
5047
const { outputFileSystem } =
5148
/** @type {Compiler & { outputFileSystem: OutputFileSystem }} */ (
5249
compilation.compiler
5350
);
5451

5552
publicPaths.push({
53+
compilation,
5654
outputPath,
5755
outputFileSystem,
5856
publicPath,
59-
assetsInfo,
6057
});
6158
}
6259

types/utils/getPaths.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
export default getPaths;
22
export type Compiler = import("@rspack/core").Compiler;
3+
export type Compilation = import("@rspack/core").Compilation;
34
export type Stats = import("@rspack/core").Stats;
45
export type MultiStats = import("@rspack/core").MultiStats;
5-
export type Asset = import("@rspack/core").Asset;
66
export type DevServerOption = import("../index.js").DevServerOption;
77
export type IncomingMessage = import("../index.js").IncomingMessage;
88
export type OutputFileSystem = import("../index.js").OutputFileSystem;
99
export type ServerResponse = import("../index.js").ServerResponse;
1010
/** @typedef {import("@rspack/core").Compiler} Compiler */
11+
/** @typedef {import("@rspack/core").Compilation} Compilation */
1112
/** @typedef {import("@rspack/core").Stats} Stats */
1213
/** @typedef {import("@rspack/core").MultiStats} MultiStats */
13-
/** @typedef {import("@rspack/core").Asset} Asset */
1414
/** @typedef {import("../index.js").DevServerOption} DevServerOption */
1515
/** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
1616
/** @typedef {import("../index.js").OutputFileSystem} OutputFileSystem */
@@ -19,16 +19,16 @@ export type ServerResponse = import("../index.js").ServerResponse;
1919
* @template {IncomingMessage} Request
2020
* @template {ServerResponse} Response
2121
* @param {import("../index.js").FilledContext<Request, Response>} context context
22-
* @returns {{ outputPath: string, outputFileSystem: OutputFileSystem, publicPath: string, assetsInfo: Map<string, Asset["info"]> | undefined }[]} paths
22+
* @returns {{ compilation: Compilation, outputPath: string, outputFileSystem: OutputFileSystem, publicPath: string }[]} paths
2323
*/
2424
declare function getPaths<
2525
Request extends IncomingMessage,
2626
Response extends ServerResponse,
2727
>(
2828
context: import("../index.js").FilledContext<Request, Response>,
2929
): {
30+
compilation: Compilation;
3031
outputPath: string;
3132
outputFileSystem: OutputFileSystem;
3233
publicPath: string;
33-
assetsInfo: Map<string, Asset["info"]> | undefined;
3434
}[];

0 commit comments

Comments
 (0)