Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions src/utils/getFilenameFromUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ function getFilenameFromUrl(context, url) {
}

for (const {
compilation,
publicPath,
outputPath,
assetsInfo,
outputFileSystem,
} of paths) {
/** @type {string | undefined} */
Expand Down Expand Up @@ -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,
});

/**
Comment thread
chenjiahan marked this conversation as resolved.
* @param {string} filename filename
* @param {Set<string>=} visited visited filenames
Expand Down Expand Up @@ -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 };
};

/**
Expand Down Expand Up @@ -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("/")) {
Expand Down
11 changes: 4 additions & 7 deletions src/utils/getPaths.js
Original file line number Diff line number Diff line change
@@ -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 */
Expand All @@ -11,7 +11,7 @@
* @template {IncomingMessage} Request
* @template {ServerResponse} Response
* @param {import("../index.js").FilledContext<Request, Response>} context context
* @returns {{ outputPath: string, outputFileSystem: OutputFileSystem, publicPath: string, assetsInfo: Map<string, Asset["info"]> | undefined }[]} paths
* @returns {{ compilation: Compilation, outputPath: string, outputFileSystem: OutputFileSystem, publicPath: string }[]} paths
*/
function getPaths(context) {
const { stats, options } = context;
Expand All @@ -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<string, Asset["info"]> | undefined }[]} */
/** @type {{ compilation: Compilation, outputPath: string, outputFileSystem: OutputFileSystem, publicPath: string }[]} */
const publicPaths = [];

for (const { compilation } of childStats) {
Expand All @@ -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,
});
}

Expand Down
8 changes: 4 additions & 4 deletions types/utils/getPaths.d.ts
Original file line number Diff line number Diff line change
@@ -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 */
Expand All @@ -19,16 +19,16 @@ export type ServerResponse = import("../index.js").ServerResponse;
* @template {IncomingMessage} Request
* @template {ServerResponse} Response
* @param {import("../index.js").FilledContext<Request, Response>} context context
* @returns {{ outputPath: string, outputFileSystem: OutputFileSystem, publicPath: string, assetsInfo: Map<string, Asset["info"]> | undefined }[]} paths
* @returns {{ compilation: Compilation, outputPath: string, outputFileSystem: OutputFileSystem, publicPath: string }[]} paths
*/
declare function getPaths<
Request extends IncomingMessage,
Response extends ServerResponse,
>(
context: import("../index.js").FilledContext<Request, Response>,
): {
compilation: Compilation;
outputPath: string;
outputFileSystem: OutputFileSystem;
publicPath: string;
assetsInfo: Map<string, Asset["info"]> | undefined;
}[];