From a8d39c0cdfe243ee8111d8114895eaddfdc197d4 Mon Sep 17 00:00:00 2001 From: Ryuya Date: Mon, 15 Sep 2025 07:14:12 -0700 Subject: [PATCH] refactor: simplify `webpackPreloadAs` validation and update warnings --- cspell.json | 1 - lib/url/URLParserPlugin.js | 31 ++----------------- .../generate-warnings.js | 6 ++-- .../warnings.js | 4 +-- 4 files changed, 6 insertions(+), 36 deletions(-) diff --git a/cspell.json b/cspell.json index 2d3369659..9ce10a6cb 100644 --- a/cspell.json +++ b/cspell.json @@ -231,7 +231,6 @@ "serializables", "serializer", "serializers", - "serviceworker", "shama", "skypack", "snapshotting", diff --git a/lib/url/URLParserPlugin.js b/lib/url/URLParserPlugin.js index 6fd7d8c01..9eb83c7d6 100644 --- a/lib/url/URLParserPlugin.js +++ b/lib/url/URLParserPlugin.js @@ -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) ) ); diff --git a/test/configCases/asset-modules/url-prefetch-preload-fetchpriority/generate-warnings.js b/test/configCases/asset-modules/url-prefetch-preload-fetchpriority/generate-warnings.js index 33a862198..e19df05d1 100644 --- a/test/configCases/asset-modules/url-prefetch-preload-fetchpriority/generate-warnings.js +++ b/test/configCases/asset-modules/url-prefetch-preload-fetchpriority/generate-warnings.js @@ -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 */ diff --git a/test/configCases/asset-modules/url-prefetch-preload-fetchpriority/warnings.js b/test/configCases/asset-modules/url-prefetch-preload-fetchpriority/warnings.js index 48ec8ca27..5923056d0 100644 --- a/test/configCases/asset-modules/url-prefetch-preload-fetchpriority/warnings.js +++ b/test/configCases/asset-modules/url-prefetch-preload-fetchpriority/warnings.js @@ -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)