From bda707b5acd26183f1be0090547229dd46bb2fdc Mon Sep 17 00:00:00 2001 From: KoloMl Date: Thu, 12 Mar 2026 00:53:45 +0400 Subject: [PATCH] Builder: Support replacing domains inside `exclude_matches` --- .vite/lib/manifest.js | 44 ++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/.vite/lib/manifest.js b/.vite/lib/manifest.js index 54aa712..3a3181a 100644 --- a/.vite/lib/manifest.js +++ b/.vite/lib/manifest.js @@ -96,21 +96,16 @@ class ManifestProcessor { singleOrMultipleHostnames = [singleOrMultipleHostnames]; } + const matchPatterReplacer = ManifestProcessor.#createHostnameReplacementReduce(singleOrMultipleHostnames); + this.#manifestObject.host_permissions = singleOrMultipleHostnames.map(hostname => `*://*.${hostname}/`); this.#manifestObject.content_scripts?.forEach(entry => { - entry.matches = entry.matches.reduce((resultMatches, originalMatchPattern) => { - for (const updatedHostname of singleOrMultipleHostnames) { - resultMatches.push( - originalMatchPattern.replace( - /\*:\/\/\*\.[a-z]+\.[a-z]+\//, - `*://*.${updatedHostname}/` - ), - ); - } + entry.matches = entry.matches.reduce(matchPatterReplacer, []); - return resultMatches; - }, []); + if (entry.exclude_matches) { + entry.exclude_matches = entry.exclude_matches.reduce(matchPatterReplacer, []); + } }) } @@ -148,6 +143,32 @@ class ManifestProcessor { } ); } + + /** + * @param {string|(string[])} singleOrMultipleHostnames + * @return {function(string[], string): string[]} + */ + static #createHostnameReplacementReduce(singleOrMultipleHostnames) { + return ( + /** + * @param {string[]} resultMatches + * @param {string} originalMatchPattern + * @return {string[]} + */ + (resultMatches, originalMatchPattern) => { + for (const updatedHostname of singleOrMultipleHostnames) { + resultMatches.push( + originalMatchPattern.replace( + /\*:\/\/\*\.[a-z]+\.[a-z]+\//, + `*://*.${updatedHostname}/` + ), + ); + } + + return resultMatches; + } + ); + } } /** @@ -186,6 +207,7 @@ export function loadManifest(filePath) { /** * @typedef {Object} ContentScriptsEntry * @property {string[]} matches + * @property {string[]} exclude_matches * @property {string[]|undefined} js * @property {string[]|undefined} css */