Skip to content

Commit 7627f0e

Browse files
fix: crash when using filesystem cache (#460)
1 parent 8eb07f8 commit 7627f0e

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/loader.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ const path = require("path");
33
const worker = require("./worker");
44
const schema = require("./loader-options.json");
55
const {
6-
isAbsoluteURL,
76
IMAGE_MINIMIZER_PLUGIN_INFO_MAPPINGS,
7+
ABSOLUTE_URL_REGEX,
8+
WINDOWS_PATH_REGEX,
89
} = require("./utils.js");
910

1011
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
@@ -33,14 +34,14 @@ const {
3334
/**
3435
* @template T
3536
* @param {import("webpack").LoaderContext<LoaderOptions<T>>} loaderContext
36-
* @param {boolean} isAbsolute
3737
* @param {WorkerResult} output
3838
* @param {string} query
3939
*/
40-
function changeResource(loaderContext, isAbsolute, output, query) {
41-
loaderContext.resourcePath = isAbsolute
42-
? output.filename
43-
: path.join(loaderContext.rootContext, output.filename);
40+
function changeResource(loaderContext, output, query) {
41+
loaderContext.resourcePath = path.join(
42+
loaderContext.rootContext,
43+
output.filename,
44+
);
4445
loaderContext.resourceQuery = query;
4546
}
4647

@@ -203,11 +204,11 @@ async function loader(content) {
203204
}
204205
}
205206

206-
let isAbsolute = isAbsoluteURL(this.resourcePath);
207-
208-
const filename = isAbsolute
209-
? this.resourcePath
210-
: path.relative(this.rootContext, this.resourcePath);
207+
const filename =
208+
ABSOLUTE_URL_REGEX.test(this.resourcePath) &&
209+
!WINDOWS_PATH_REGEX.test(this.resourcePath)
210+
? this.resourcePath
211+
: this.utils.contextify(this.rootContext, this.resourcePath);
211212

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

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

271271
// Change name of assets modules after generator
272272
if (this._module && !this._module.matchResource) {

src/utils.js

+2
Original file line numberDiff line numberDiff line change
@@ -1308,4 +1308,6 @@ module.exports = {
13081308
sharpGenerate,
13091309
svgoMinify,
13101310
IMAGE_MINIMIZER_PLUGIN_INFO_MAPPINGS,
1311+
ABSOLUTE_URL_REGEX,
1312+
WINDOWS_PATH_REGEX,
13111313
};

types/utils.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ export function svgoMinify<T>(
173173
): Promise<WorkerResult | null>;
174174
/** @type {WeakMap<Module, AssetInfo>} */
175175
export const IMAGE_MINIMIZER_PLUGIN_INFO_MAPPINGS: WeakMap<Module, AssetInfo>;
176+
export const ABSOLUTE_URL_REGEX: RegExp;
177+
export const WINDOWS_PATH_REGEX: RegExp;
176178
declare function squooshImagePoolSetup(): void;
177179
declare function squooshImagePoolTeardown(): Promise<void>;
178180
export {};

0 commit comments

Comments
 (0)