Skip to content

Commit

Permalink
feat!: optimize web accessible resources (#89)
Browse files Browse the repository at this point in the history
adds an optimizeWebAccessibleResources option that, in Manifest V3, will merge web accessible resource definitions that have matching non-`resource` properties and dedupe and sort `resources`. In Manifest V2, will sort web accessible resources.
  • Loading branch information
samrum authored Apr 9, 2023
1 parent cd9590e commit d673de2
Show file tree
Hide file tree
Showing 38 changed files with 1,331 additions and 281 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ useDynamicUrlWebAccessibleResources (optional)
- Default: `true`
- Adds the `use_dynamic_url` property to web accessible resources generated by the plugin

optimizeWebAccessibleResources (optional)

- Type: `boolean`
- Default: `true`
- On build, in Manifest V3, merge web accessible resource definitions that have matching non-`resource` properties and dedupe and sort `resources`. In Manifest V2, sort web accessible resources.

devHtmlTransform (optional)

- Type: `boolean`
Expand Down
8 changes: 8 additions & 0 deletions src/manifestParser/manifestParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export default abstract class ManifestParser<
result = await parseMethod(result, bundle);
}

if (this.pluginOptions.optimizeWebAccessibleResources !== false) {
result = this.optimizeWebAccessibleResources(result);
}

result.emitFiles.push({
type: "asset",
fileName: "manifest.json",
Expand Down Expand Up @@ -116,6 +120,10 @@ export default abstract class ManifestParser<
bundle: OutputBundle
): Promise<ParseResult<Manifest>>;

protected abstract optimizeWebAccessibleResources(
result: ParseResult<Manifest>
): ParseResult<Manifest>;

protected parseInputAdditionalInputs(
result: ParseResult<Manifest>
): ParseResult<Manifest> {
Expand Down
13 changes: 13 additions & 0 deletions src/manifestParser/manifestV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,17 @@ export default class ManifestV2 extends ManifestParser<Manifest> {

return result;
}

protected optimizeWebAccessibleResources(
result: ParseResult<chrome.runtime.ManifestV2>
): ParseResult<chrome.runtime.ManifestV2> {
if (!result.manifest.web_accessible_resources) {
return result;
}

result.manifest.web_accessible_resources =
result.manifest.web_accessible_resources.sort();

return result;
}
}
35 changes: 35 additions & 0 deletions src/manifestParser/manifestV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,39 @@ export default class ManifestV3 extends ManifestParser<Manifest> {

return result;
}

protected optimizeWebAccessibleResources(
result: ParseResult<chrome.runtime.ManifestV3>
): ParseResult<chrome.runtime.ManifestV3> {
if (!result.manifest.web_accessible_resources) {
return result;
}

const resourceMap = new Map();

result.manifest.web_accessible_resources.forEach((resource) => {
const resourceKey = JSON.stringify({
...resource,
resources: [],
});

if (resourceMap.has(resourceKey)) {
const existingEntry = resourceMap.get(resourceKey);

resourceMap.set(resourceKey, {
...existingEntry,
...resource,
resources: [
...new Set([...existingEntry.resources, ...resource.resources]),
].sort(),
});
} else {
resourceMap.set(resourceKey, resource);
}
});

result.manifest.web_accessible_resources = [...resourceMap.values()];

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ export {
\\"name\\": \\"Manifest Name\\",
\\"manifest_version\\": 2,
\\"web_accessible_resources\\": [
\\"assets/test/manifest/resources/additionalInputsScriptsChunkedImport/script1.js\\",
\\"assets/log.js\\",
\\"test/manifest/resources/additionalInputsScriptsChunkedImport/script1.js\\",
\\"assets/test/manifest/resources/additionalInputsScriptsChunkedImport/script1.js\\",
\\"assets/test/manifest/resources/additionalInputsScriptsChunkedImport/script2.js\\",
\\"test/manifest/resources/additionalInputsScriptsChunkedImport/script1.js\\",
\\"test/manifest/resources/additionalInputsScriptsChunkedImport/script2.js\\"
]
}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,10 @@ export {
\\"web_accessible_resources\\": [
{
\\"resources\\": [
\\"assets/test/manifest/resources/additionalInputsScriptsChunkedImport/script1.js\\",
\\"assets/log.js\\",
\\"test/manifest/resources/additionalInputsScriptsChunkedImport/script1.js\\"
],
\\"matches\\": [
\\"<all_urls>\\"
],
\\"use_dynamic_url\\": true
},
{
\\"resources\\": [
\\"assets/test/manifest/resources/additionalInputsScriptsChunkedImport/script1.js\\",
\\"assets/test/manifest/resources/additionalInputsScriptsChunkedImport/script2.js\\",
\\"assets/log.js\\",
\\"test/manifest/resources/additionalInputsScriptsChunkedImport/script1.js\\",
\\"test/manifest/resources/additionalInputsScriptsChunkedImport/script2.js\\"
],
\\"matches\\": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,7 @@ exports[`additionalInputsScriptsNoImport - Manifest V3 1`] = `
\\"web_accessible_resources\\": [
{
\\"resources\\": [
\\"test/manifest/resources/additionalInputsScriptsNoImport/script1.js\\"
],
\\"matches\\": [
\\"<all_urls>\\"
],
\\"use_dynamic_url\\": true
},
{
\\"resources\\": [
\\"test/manifest/resources/additionalInputsScriptsNoImport/script1.js\\",
\\"test/manifest/resources/additionalInputsScriptsNoImport/script2.js\\"
],
\\"matches\\": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,7 @@ exports[`additionalInputsScriptsTypes - Manifest V3 1`] = `
\\"web_accessible_resources\\": [
{
\\"resources\\": [
\\"test/manifest/resources/additionalInputsScriptsTypes/script1.js\\"
],
\\"matches\\": [
\\"<all_urls>\\"
],
\\"use_dynamic_url\\": true
},
{
\\"resources\\": [
\\"test/manifest/resources/additionalInputsScriptsTypes/script1.js\\",
\\"test/manifest/resources/additionalInputsScriptsTypes/script2.js\\"
],
\\"matches\\": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,7 @@ log(\\"script1\\");
\\"web_accessible_resources\\": [
{
\\"resources\\": [
\\"test/manifest/resources/additionalInputsScriptsUnchunkedImport/script1.js\\"
],
\\"matches\\": [
\\"<all_urls>\\"
],
\\"use_dynamic_url\\": true
},
{
\\"resources\\": [
\\"test/manifest/resources/additionalInputsScriptsUnchunkedImport/script1.js\\",
\\"test/manifest/resources/additionalInputsScriptsUnchunkedImport/script2.js\\"
],
\\"matches\\": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,7 @@ exports[`additionalInputsStylesTypes - Manifest V3 1`] = `
\\"web_accessible_resources\\": [
{
\\"resources\\": [
\\"test/manifest/resources/additionalInputsStylesTypes/style1.css\\"
],
\\"matches\\": [
\\"<all_urls>\\"
],
\\"use_dynamic_url\\": true
},
{
\\"resources\\": [
\\"test/manifest/resources/additionalInputsStylesTypes/style1.css\\",
\\"test/manifest/resources/additionalInputsStylesTypes/style2.css\\"
],
\\"matches\\": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,17 @@ export {
\\"name\\": \\"Manifest Name\\",
\\"manifest_version\\": 2,
\\"web_accessible_resources\\": [
\\"test/manifest/resources/additionalInputsWebAccessible/script1.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/script3.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/script4.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/script5.js\\",
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript1.js\\",
\\"assets/log.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript1.js\\",
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript1.js\\",
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript2.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript2.js\\",
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript3.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript3.js\\"
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript1.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript2.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript3.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/script1.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/script3.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/script4.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/script5.js\\"
]
}",
"type": "asset",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,7 @@ export {
\\"web_accessible_resources\\": [
{
\\"resources\\": [
\\"test/manifest/resources/additionalInputsWebAccessible/script1.js\\"
],
\\"matches\\": [
\\"<all_urls>\\"
],
\\"use_dynamic_url\\": true
},
{
\\"resources\\": [
\\"test/manifest/resources/additionalInputsWebAccessible/script1.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/script3.js\\"
],
\\"matches\\": [
Expand All @@ -239,38 +231,12 @@ export {
},
{
\\"resources\\": [
\\"test/manifest/resources/additionalInputsWebAccessible/script4.js\\"
],
\\"matches\\": [
\\"https://example.com/\\"
],
\\"use_dynamic_url\\": true
},
{
\\"resources\\": [
\\"test/manifest/resources/additionalInputsWebAccessible/script5.js\\"
],
\\"extension_ids\\": [
\\"oilkjaldkfjlasdf\\"
],
\\"use_dynamic_url\\": true
},
{
\\"resources\\": [
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript1.js\\",
\\"assets/log.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript1.js\\"
],
\\"matches\\": [
\\"https://example.com/\\"
],
\\"use_dynamic_url\\": true
},
{
\\"resources\\": [
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript1.js\\",
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript2.js\\",
\\"assets/log.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript2.js\\"
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript1.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript2.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/script4.js\\"
],
\\"matches\\": [
\\"https://example.com/\\"
Expand All @@ -279,9 +245,10 @@ export {
},
{
\\"resources\\": [
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript3.js\\",
\\"assets/log.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript3.js\\"
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript3.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript3.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/script5.js\\"
],
\\"extension_ids\\": [
\\"oilkjaldkfjlasdf\\"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,17 @@ export {
\\"name\\": \\"Manifest Name\\",
\\"manifest_version\\": 2,
\\"web_accessible_resources\\": [
\\"assets/log.js\\",
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript1.js\\",
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript2.js\\",
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript3.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript1.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript2.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/script1.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/script2.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/script3.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/script5.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/script6.js\\",
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript1.js\\",
\\"assets/log.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript1.js\\",
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript2.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript2.js\\",
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript3.js\\"
\\"test/manifest/resources/additionalInputsWebAccessible/script6.js\\"
]
}",
"type": "asset",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,13 @@ export {
},
{
\\"resources\\": [
\\"test/manifest/resources/additionalInputsWebAccessible/script2.js\\"
],
\\"matches\\": [
\\"https://example.com/\\"
],
\\"use_dynamic_url\\": true
},
{
\\"resources\\": [
\\"assets/log.js\\",
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript1.js\\",
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript2.js\\",
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript3.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript1.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript2.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/script2.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/script3.js\\"
],
\\"matches\\": [
Expand All @@ -262,53 +260,13 @@ export {
},
{
\\"resources\\": [
\\"test/manifest/resources/additionalInputsWebAccessible/script5.js\\"
],
\\"extension_ids\\": [
\\"oilkjaldkfjlasdf\\"
],
\\"use_dynamic_url\\": true
},
{
\\"resources\\": [
\\"test/manifest/resources/additionalInputsWebAccessible/script5.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/script6.js\\"
],
\\"extension_ids\\": [
\\"oilkjaldkfjlasdf\\"
],
\\"use_dynamic_url\\": true
},
{
\\"resources\\": [
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript1.js\\",
\\"assets/log.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript1.js\\"
],
\\"matches\\": [
\\"https://example.com/\\"
],
\\"use_dynamic_url\\": true
},
{
\\"resources\\": [
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript2.js\\",
\\"assets/log.js\\",
\\"test/manifest/resources/additionalInputsWebAccessible/chunkedScript2.js\\"
],
\\"matches\\": [
\\"https://example.com/\\"
],
\\"use_dynamic_url\\": true
},
{
\\"resources\\": [
\\"assets/test/manifest/resources/additionalInputsWebAccessible/chunkedScript3.js\\",
\\"assets/log.js\\"
],
\\"matches\\": [
\\"https://example.com/\\"
],
\\"use_dynamic_url\\": true
}
]
}",
Expand Down
Loading

0 comments on commit d673de2

Please sign in to comment.