Skip to content

Commit

Permalink
Update web_accessible_resources test
Browse files Browse the repository at this point in the history
  • Loading branch information
cezaraugusto committed Dec 28, 2024
1 parent e86dea5 commit 926ab2c
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,153 +45,147 @@ describe('generateManifestPatches', () => {
return JSON.parse(callArgs[1].source().toString()) as Manifest
}

it('should work for manifest v2', () => {
it('should add non-css/js resources for manifest v3 content scripts', () => {
expect(
runWith(
{
'content_scripts/content-0': [
'content_scripts/content-0.css',
'content_scripts/content-0.js.map'
],
'content_scripts/content-1': [
'content_scripts/content-1.css',
'content_scripts/content-1.js.map'
]
'content_scripts/content-0': ['content_scripts/content-0.svg'],
'content_scripts/content-1': ['content_scripts/content-1.json']
},
{
manifest_version: 2,
manifest_version: 3,
content_scripts: [
{
matches: ['<all_urls>'],
run_at: 'document_start',
js: ['content_scripts/content-0.js'],
css: []
matches: ['*://example.com/*'],
js: ['content_scripts/content-0.js']
},
{
matches: ['<all_urls>'],
run_at: 'document_start',
js: ['content_scripts/content-1.js'],
css: []
matches: ['*://example.com/logout?e=4'],
js: ['content_scripts/content-1.js']
}
]
}
)
).toMatchObject({
web_accessible_resources: [
'content_scripts/content-0.css',
'content_scripts/content-0.js.map',
'content_scripts/content-1.css',
'content_scripts/content-1.js.map'
{
matches: ['*://example.com/*'],
resources: ['content_scripts/content-0.svg']
},
{
matches: ['*://example.com/logout?e=4'],
resources: ['content_scripts/content-1.json']
}
]
})
})

it('should work for manifest v3', () => {
it('should not generate web_accessible_resources if content scripts have no imports', () => {
expect(
runWith(
{
'content_scripts/content-0': [
'content_scripts/content-0.css',
'content_scripts/content-0.js.map'
],
'content_scripts/content-1': [
'content_scripts/content-1.css',
'content_scripts/content-1.js.map'
]
'content_scripts/content-0': [],
'content_scripts/content-1': []
},
{
manifest_version: 3,
content_scripts: [
{
matches: ['<all_urls>'],
run_at: 'document_start',
js: ['content_scripts/content-0.js'],
css: []
matches: ['*://example.com/*'],
js: ['content_scripts/content-0.js']
},
{
matches: ['<all_urls>'],
run_at: 'document_start',
js: ['content_scripts/content-1.js'],
css: []
matches: ['*://example.com/logout?e=4'],
js: ['content_scripts/content-1.js']
}
]
}
)
).toMatchObject({
web_accessible_resources: [
{
matches: ['<all_urls>'],
resources: [
'content_scripts/content-0.css',
'content_scripts/content-1.css'
]
}
]
})
).web_accessible_resources
).toBeUndefined()
})

it('should work if there is existing web_accessible_resources', () => {
it('should correctly merge existing web_accessible_resources', () => {
expect(
runWith(
{
'content_scripts/content-0': [
'content_scripts/content-0.css',
'content_scripts/content-0.js.map'
],
'content_scripts/content-1': [
'content_scripts/content-1.css',
'content_scripts/content-1.js.map'
]
'content_scripts/content-0': ['content_scripts/content-0.svg']
},
{
manifest_version: 3,
content_scripts: [
{
matches: ['<all_urls>'],
run_at: 'document_start',
js: ['content_scripts/content-0.js'],
css: []
},
{
matches: ['https://example.com/some/path'],
run_at: 'document_start',
js: ['content_scripts/content-1.js'],
css: []
matches: ['*://example.com/*'],
js: ['content_scripts/content-0.js']
}
],
web_accessible_resources: [
{
matches: ['<all_urls>'],
resources: ['my-file.css']
matches: ['*://example.com/*'],
resources: ['existing_resource.svg']
}
]
}
)
).toMatchObject({
web_accessible_resources: [
{
matches: ['<all_urls>'],
resources: ['my-file.css', 'content_scripts/content-0.css']
matches: ['*://example.com/*'],
resources: ['existing_resource.svg', 'content_scripts/content-0.svg']
}
]
})
})

it('should correctly handle manifest v2 content scripts', () => {
expect(
runWith(
{
'content_scripts/content-0': ['content_scripts/content-0.json']
},
{
matches: ['https://example.com/*'],
resources: ['content_scripts/content-1.css']
manifest_version: 2,
content_scripts: [
{
matches: ['<all_urls>'],
js: ['content_scripts/content-0.js']
}
]
}
]
)
).toMatchObject({
web_accessible_resources: ['content_scripts/content-0.json']
})
})

it('should not add web_accessible_resources if there is no entries', () => {
it('should exclude .map, .css, and .js files from web_accessible_resources', () => {
expect(
runWith(
{},
{
'content_scripts/content-0': [
'content_scripts/content-0.js',
'content_scripts/content-0.css',
'content_scripts/content-0.js.map',
'content_scripts/content-0.svg'
]
},
{
manifest_version: 3,
background: {
service_worker: 'background.js'
}
content_scripts: [
{
matches: ['*://example.com/*'],
js: ['content_scripts/content-0.js'],
css: ['content_scripts/content-0.css']
}
]
}
).web_accessible_resources
).toBeUndefined()
)
).toMatchObject({
web_accessible_resources: [
{
matches: ['*://example.com/*'],
resources: ['content_scripts/content-0.svg']
}
]
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,19 @@ export class WebResourcesPlugin {
if (webAccessibleResourcesV3.length > 0) {
manifest.web_accessible_resources =
webAccessibleResourcesV3 as Manifest['web_accessible_resources']
} else {
delete manifest.web_accessible_resources // Remove if unnecessary
}
}
// else {
// // Do nothing
// }
} else {
if (webAccessibleResourcesV2.length > 0) {
manifest.web_accessible_resources = Array.from(
new Set(webAccessibleResourcesV2)
) as Manifest['web_accessible_resources']
} else {
delete manifest.web_accessible_resources // Remove if unnecessary
}
// else {
// // Do nothing
// }
}

const source = JSON.stringify(manifest, null, 2)
Expand Down

0 comments on commit 926ab2c

Please sign in to comment.