Skip to content

Commit 33f3194

Browse files
fix: match a rule's glob against relative values such as URL requests
1 parent 3ff741f commit 33f3194

7 files changed

Lines changed: 27 additions & 6 deletions

File tree

‎.changeset/039-url-hints-logical-conditions.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Match `urlHints` rules written with `and`, `or` and `not` conditions.
5+
Match `urlHints` rules written with `and`, `or`, `not` and `glob` conditions.

‎lib/rules/RuleSetCompiler.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
const { SyncHook } = require("tapable");
99
const { compileGlobList } = require("../util/globUtils");
10-
const { fileUrlToPath } = require("../util/identifier");
10+
const {
11+
RELATIVE_PATH_PREFIX_REGEXP,
12+
fileUrlToPath
13+
} = require("../util/identifier");
1114

1215
/** @import { ResolveRequest } from "enhanced-resolve" */
1316
/**
@@ -473,7 +476,11 @@ class RuleSetCompiler {
473476
});
474477
return {
475478
matchWhenEmpty: match(""),
476-
fn: (v) => typeof v === "string" && match(v)
479+
// a relative value starts where its `./` and `../` end, as a relative
480+
// pattern matches at any depth
481+
fn: (v) =>
482+
typeof v === "string" &&
483+
match(v.replace(RELATIVE_PATH_PREFIX_REGEXP, ""))
477484
};
478485
}
479486

‎lib/util/identifier.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const FILE_URL_REGEXP = /^file:\//i;
2222
// What `import()` resolves as a place rather than as a package name: relative,
2323
// rooted, or carrying a scheme.
2424
const RESOLVABLE_SPECIFIER_REGEXP = /^(?:\.{0,2}\/|[a-zA-Z][\w+.-]*:)/;
25+
// The `./` and `../` segments a relative path opens with, either separator.
26+
const RELATIVE_PATH_PREFIX_REGEXP = /^(?:\.\.?[\\/])+/;
2527

2628
/**
2729
* Converts a file URL to the path it names. Any other value is returned
@@ -697,6 +699,7 @@ const getRequestBaseName = (request) => {
697699

698700
module.exports.ABSOLUTE_PATH_REGEXP = ABSOLUTE_PATH_REGEXP;
699701
module.exports.NODE_MODULES_REGEXP = NODE_MODULES_REGEXP;
702+
module.exports.RELATIVE_PATH_PREFIX_REGEXP = RELATIVE_PATH_PREFIX_REGEXP;
700703
module.exports.RESOLVABLE_SPECIFIER_REGEXP = RESOLVABLE_SPECIFIER_REGEXP;
701704
module.exports.WINDOWS_ABS_PATH_REGEXP = WINDOWS_ABS_PATH_REGEXP;
702705
module.exports.WINDOWS_PATH_SEPARATOR_REGEXP = WINDOWS_PATH_SEPARATOR_REGEXP;

‎test/configCases/asset-modules/url-hints-logical-conditions/deep.woff2‎

Whitespace-only changes.

‎test/configCases/asset-modules/url-hints-logical-conditions/index.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"use strict";
22

3+
import deep from "./nested/deep.js";
4+
35
const a = new URL("./a.png", import.meta.url);
46
const b = new URL("./b.png", import.meta.url);
57
const font = new URL("./font.woff2", import.meta.url);
@@ -15,5 +17,12 @@ it("should match urlHints rules written as logical conditions", () => {
1517
expect(font.href).toContain("font.woff2");
1618
expect(findLink("a.png").rel).toBe("preload");
1719
expect(findLink("b.png").rel).toBe("prefetch");
18-
expect(findLink("font.woff2").rel).toBe("prefetch");
20+
expect(findLink("font.woff2").getAttribute("fetchpriority")).toBe("low");
21+
});
22+
23+
it("should match a glob against relative requests", () => {
24+
expect(deep.href).toContain("deep.woff2");
25+
expect(findLink("font.woff2").rel).toBe("preload");
26+
expect(findLink("font.woff2").as).toBe("font");
27+
expect(findLink("deep.woff2").rel).toBe("preload");
1928
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default new URL("../deep.woff2", import.meta.url);

‎test/configCases/asset-modules/url-hints-logical-conditions/webpack.config.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ module.exports = {
1414
preload: true,
1515
as: "image"
1616
},
17-
{ test: [{ not: /\.png$/ }], prefetch: true },
18-
{ include: { or: [/b\.png$/] }, prefetch: true }
17+
{ include: { or: [/b\.png$/] }, prefetch: true },
18+
{ test: [{ not: /\.png$/ }], prefetch: true, fetchPriority: "low" },
19+
{ test: { glob: "*.woff2" }, preload: true, as: "font" }
1920
]
2021
}
2122
},

0 commit comments

Comments
 (0)