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", "serializables",
"serializer", "serializer",
"serializers", "serializers",
"serviceworker",
"shama", "shama",
"skypack", "skypack",
"snapshotting", "snapshotting",

View File

@ -226,40 +226,13 @@ class URLParserPlugin {
} }
// webpackPreloadAs: allow override of the "as" attribute for preload // 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) { if (importOptions.webpackPreloadAs !== undefined) {
const allowedAs = [ if (typeof importOptions.webpackPreloadAs === "string") {
// 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)
) {
dep.preloadAs = importOptions.webpackPreloadAs; dep.preloadAs = importOptions.webpackPreloadAs;
} else { } else {
parser.state.module.addWarning( parser.state.module.addWarning(
new UnsupportedFeatureWarning( 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) /** @type {DependencyLocation} */ (expr.loc)
) )
); );

View File

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

View File

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