refactor: simplify `webpackPreloadAs` validation and update warnings

This commit is contained in:
Ryuya 2025-09-15 07:14:12 -07:00
parent 57bb97cbb5
commit a8d39c0cdf
4 changed files with 6 additions and 36 deletions

View File

@ -231,7 +231,6 @@
"serializables",
"serializer",
"serializers",
"serviceworker",
"shama",
"skypack",
"snapshotting",

View File

@ -226,40 +226,13 @@ class URLParserPlugin {
}
// webpackPreloadAs: allow override of the "as" attribute for preload
// Fetch Standard: Request destinations (enumerates destinations used by `as`) https://fetch.spec.whatwg.org/#concept-request-destination
if (importOptions.webpackPreloadAs !== undefined) {
const allowedAs = [
// Per HTML LS "match-preload-type" + Fetch Standard request destinations
// See references above
"audio",
"audioworklet",
"document",
"embed",
"fetch",
"font",
"image",
"manifest",
"object",
"paintworklet",
"report",
"script",
"sharedworker",
"serviceworker",
"style",
"track",
"video",
"worker",
"xslt"
];
if (
typeof importOptions.webpackPreloadAs === "string" &&
allowedAs.includes(importOptions.webpackPreloadAs)
) {
if (typeof importOptions.webpackPreloadAs === "string") {
dep.preloadAs = importOptions.webpackPreloadAs;
} else {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackPreloadAs\` expected one of ${JSON.stringify(allowedAs)}, but received: ${importOptions.webpackPreloadAs}.`,
`\`webpackPreloadAs\` expected a string, but received: ${importOptions.webpackPreloadAs}.`,
/** @type {DependencyLocation} */ (expr.loc)
)
);

View File

@ -4,15 +4,13 @@
// Invalid fetchPriority value - should generate warning
const invalidPriorityUrl = new URL(/* webpackPrefetch: true */ /* webpackFetchPriority: "invalid" */ "./assets/images/priority-invalid.png", import.meta.url);
// Invalid preloadAs - should generate warning
// Invalid preloadAs (non-string) - should generate warning
const invalidPreloadAs = new URL(
/* webpackPreload: true */
/* webpackPreloadAs: "invalid-as" */
/* webpackPreloadAs: 123 */
"./assets/images/priority-invalid.png",
import.meta.url
);
// Invalid preloadType (non-string) - should generate warning
const invalidPreloadType = new URL(
/* webpackPreload: true */

View File

@ -5,8 +5,8 @@ module.exports = [
[
/`webpackFetchPriority` expected "low", "high" or "auto", but received: invalid\./
],
// Invalid preloadAs value
[/`webpackPreloadAs` expected one of \[.*\], but received: invalid-as\./],
// Invalid preloadAs (non-string)
[/`webpackPreloadAs` expected a string, but received: 123\./],
// Invalid preloadType (non-string)
[/`webpackPreloadType` expected a string, but received: 123\./],
// Invalid preloadMedia (non-string)