Skip to content

Commit

Permalink
fix: crash when using filesystem cache (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Dec 18, 2024
1 parent 8eb07f8 commit 7627f0e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ const path = require("path");
const worker = require("./worker");
const schema = require("./loader-options.json");
const {
isAbsoluteURL,
IMAGE_MINIMIZER_PLUGIN_INFO_MAPPINGS,
ABSOLUTE_URL_REGEX,
WINDOWS_PATH_REGEX,
} = require("./utils.js");

/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
Expand Down Expand Up @@ -33,14 +34,14 @@ const {
/**
* @template T
* @param {import("webpack").LoaderContext<LoaderOptions<T>>} loaderContext
* @param {boolean} isAbsolute
* @param {WorkerResult} output
* @param {string} query
*/
function changeResource(loaderContext, isAbsolute, output, query) {
loaderContext.resourcePath = isAbsolute
? output.filename
: path.join(loaderContext.rootContext, output.filename);
function changeResource(loaderContext, output, query) {
loaderContext.resourcePath = path.join(
loaderContext.rootContext,
output.filename,
);
loaderContext.resourceQuery = query;
}

Expand Down Expand Up @@ -203,11 +204,11 @@ async function loader(content) {
}
}

let isAbsolute = isAbsoluteURL(this.resourcePath);

const filename = isAbsolute
? this.resourcePath
: path.relative(this.rootContext, this.resourcePath);
const filename =
ABSOLUTE_URL_REGEX.test(this.resourcePath) &&
!WINDOWS_PATH_REGEX.test(this.resourcePath)
? this.resourcePath
: this.utils.contextify(this.rootContext, this.resourcePath);

const minifyOptions =
/** @type {import("./index").InternalWorkerOptions<T>} */ ({
Expand Down Expand Up @@ -264,9 +265,8 @@ async function loader(content) {
query = query.length > 0 ? `?${query}` : "";
}

isAbsolute = isAbsoluteURL(output.filename);
// Old approach for `file-loader` and other old loaders
changeResource(this, isAbsolute, output, query);
changeResource(this, output, query);

// Change name of assets modules after generator
if (this._module && !this._module.matchResource) {
Expand Down
2 changes: 2 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1308,4 +1308,6 @@ module.exports = {
sharpGenerate,
svgoMinify,
IMAGE_MINIMIZER_PLUGIN_INFO_MAPPINGS,
ABSOLUTE_URL_REGEX,
WINDOWS_PATH_REGEX,
};
2 changes: 2 additions & 0 deletions types/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ export function svgoMinify<T>(
): Promise<WorkerResult | null>;
/** @type {WeakMap<Module, AssetInfo>} */
export const IMAGE_MINIMIZER_PLUGIN_INFO_MAPPINGS: WeakMap<Module, AssetInfo>;
export const ABSOLUTE_URL_REGEX: RegExp;
export const WINDOWS_PATH_REGEX: RegExp;
declare function squooshImagePoolSetup(): void;
declare function squooshImagePoolTeardown(): Promise<void>;
export {};

0 comments on commit 7627f0e

Please sign in to comment.