diff --git a/src/utils/getFilenameFromUrl.js b/src/utils/getFilenameFromUrl.js index 5ea2e99..056bd79 100644 --- a/src/utils/getFilenameFromUrl.js +++ b/src/utils/getFilenameFromUrl.js @@ -99,9 +99,9 @@ function getFilenameFromUrl(context, url) { } for (const { + compilation, publicPath, outputPath, - assetsInfo, outputFileSystem, } of paths) { /** @type {string | undefined} */ @@ -144,6 +144,28 @@ function getFilenameFromUrl(context, url) { pathname.slice(publicPathPathname.length), ); + /** @type {boolean | undefined} */ + let immutable = undefined; + + /** + * @param {FSStats} stats stats + * @returns {Extra} extra + */ + const createExtra = (stats) => ({ + // Lazy evaluate immutable because it may be expensive to get asset info + get immutable() { + if (immutable === undefined) { + const assetName = pathname.slice(publicPathPathname.length); + immutable = Boolean( + compilation.getAsset(assetName)?.info?.immutable, + ); + } + return immutable; + }, + outputFileSystem, + stats, + }); + /** * @param {string} filename filename * @param {Set=} visited visited filenames @@ -182,17 +204,10 @@ function getFilenameFromUrl(context, url) { return resolveIndex(filename, visited); } - /** @type {Extra} */ - const extra = { - immutable: assetsInfo - ? assetsInfo.get(pathname.slice(publicPathPathname.length)) - ?.immutable - : false, - outputFileSystem, - stats: /** @type {FSStats} */ (stats), + return { + filename, + extra: createExtra(/** @type {FSStats} */ (stats)), }; - - return { filename, extra }; }; /** @@ -221,17 +236,10 @@ function getFilenameFromUrl(context, url) { return; } - /** @type {Extra} */ - const extra = { - immutable: assetsInfo - ? assetsInfo.get(pathname.slice(publicPathPathname.length)) - ?.immutable - : false, - outputFileSystem, - stats: /** @type {FSStats} */ (stats), + return { + filename, + extra: createExtra(/** @type {FSStats} */ (stats)), }; - - return { filename, extra }; }; if (index.length > 0 && pathname.endsWith("/")) { diff --git a/src/utils/getPaths.js b/src/utils/getPaths.js index 39e92ea..a0cb954 100644 --- a/src/utils/getPaths.js +++ b/src/utils/getPaths.js @@ -1,7 +1,7 @@ /** @typedef {import("@rspack/core").Compiler} Compiler */ +/** @typedef {import("@rspack/core").Compilation} Compilation */ /** @typedef {import("@rspack/core").Stats} Stats */ /** @typedef {import("@rspack/core").MultiStats} MultiStats */ -/** @typedef {import("@rspack/core").Asset} Asset */ /** @typedef {import("../index.js").DevServerOption} DevServerOption */ /** @typedef {import("../index.js").IncomingMessage} IncomingMessage */ /** @typedef {import("../index.js").OutputFileSystem} OutputFileSystem */ @@ -11,7 +11,7 @@ * @template {IncomingMessage} Request * @template {ServerResponse} Response * @param {import("../index.js").FilledContext} context context - * @returns {{ outputPath: string, outputFileSystem: OutputFileSystem, publicPath: string, assetsInfo: Map | undefined }[]} paths + * @returns {{ compilation: Compilation, outputPath: string, outputFileSystem: OutputFileSystem, publicPath: string }[]} paths */ function getPaths(context) { const { stats, options } = context; @@ -22,7 +22,7 @@ function getPaths(context) { (stats).stats ? /** @type {MultiStats} */ (stats).stats : [/** @type {Stats} */ (stats)]; - /** @type {{ outputPath: string, outputFileSystem: OutputFileSystem, publicPath: string, assetsInfo: Map | undefined }[]} */ + /** @type {{ compilation: Compilation, outputPath: string, outputFileSystem: OutputFileSystem, publicPath: string }[]} */ const publicPaths = []; for (const { compilation } of childStats) { @@ -44,19 +44,16 @@ function getPaths(context) { /** @type {any} */ (compilation.outputOptions.publicPath), ) : ""; - const assetsInfo = new Map( - compilation.getAssets().map((asset) => [asset.name, asset.info]), - ); const { outputFileSystem } = /** @type {Compiler & { outputFileSystem: OutputFileSystem }} */ ( compilation.compiler ); publicPaths.push({ + compilation, outputPath, outputFileSystem, publicPath, - assetsInfo, }); } diff --git a/types/utils/getPaths.d.ts b/types/utils/getPaths.d.ts index 72b4924..8fbe8fb 100644 --- a/types/utils/getPaths.d.ts +++ b/types/utils/getPaths.d.ts @@ -1,16 +1,16 @@ export default getPaths; export type Compiler = import("@rspack/core").Compiler; +export type Compilation = import("@rspack/core").Compilation; export type Stats = import("@rspack/core").Stats; export type MultiStats = import("@rspack/core").MultiStats; -export type Asset = import("@rspack/core").Asset; export type DevServerOption = import("../index.js").DevServerOption; export type IncomingMessage = import("../index.js").IncomingMessage; export type OutputFileSystem = import("../index.js").OutputFileSystem; export type ServerResponse = import("../index.js").ServerResponse; /** @typedef {import("@rspack/core").Compiler} Compiler */ +/** @typedef {import("@rspack/core").Compilation} Compilation */ /** @typedef {import("@rspack/core").Stats} Stats */ /** @typedef {import("@rspack/core").MultiStats} MultiStats */ -/** @typedef {import("@rspack/core").Asset} Asset */ /** @typedef {import("../index.js").DevServerOption} DevServerOption */ /** @typedef {import("../index.js").IncomingMessage} IncomingMessage */ /** @typedef {import("../index.js").OutputFileSystem} OutputFileSystem */ @@ -19,7 +19,7 @@ export type ServerResponse = import("../index.js").ServerResponse; * @template {IncomingMessage} Request * @template {ServerResponse} Response * @param {import("../index.js").FilledContext} context context - * @returns {{ outputPath: string, outputFileSystem: OutputFileSystem, publicPath: string, assetsInfo: Map | undefined }[]} paths + * @returns {{ compilation: Compilation, outputPath: string, outputFileSystem: OutputFileSystem, publicPath: string }[]} paths */ declare function getPaths< Request extends IncomingMessage, @@ -27,8 +27,8 @@ declare function getPaths< >( context: import("../index.js").FilledContext, ): { + compilation: Compilation; outputPath: string; outputFileSystem: OutputFileSystem; publicPath: string; - assetsInfo: Map | undefined; }[];