Skip to content

Commit a90dcef

Browse files
authored
Do not add web_accessible_resources if it would be empty (#183)
1 parent e57da96 commit a90dcef

File tree

2 files changed

+210
-7
lines changed

2 files changed

+210
-7
lines changed
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
import { Asset, Compilation } from "webpack";
2+
import { WebResourcesPlugin } from ".."
3+
4+
type Manifest = Partial<chrome.runtime.ManifestV2> | Partial<chrome.runtime.ManifestV3>;
5+
6+
describe('generateManifestPatches', () => {
7+
const plugin = new WebResourcesPlugin({
8+
manifestPath: 'manifest.json',
9+
});
10+
11+
function runWith(entryImports: Record<string, string[]>, partialManifest: Manifest = {}) {
12+
const manifest = {
13+
...partialManifest,
14+
};
15+
16+
const manifestSource = {
17+
source: () => JSON.stringify(manifest)
18+
} as Asset['source'];
19+
const manifestAsset = {
20+
name: 'manifest.json',
21+
source: manifestSource,
22+
} as unknown as Readonly<Asset>;
23+
24+
const updateAssetMock = jest.fn<void, [string, Asset['source']]>();
25+
26+
plugin['generateManifestPatches'](
27+
{
28+
getAsset: () => manifestAsset,
29+
assets: {
30+
'manifest.json': manifestSource,
31+
},
32+
updateAsset: updateAssetMock,
33+
} as unknown as Compilation,
34+
entryImports
35+
);
36+
37+
expect(updateAssetMock).toHaveBeenCalledTimes(1);
38+
const callArgs = updateAssetMock.mock.calls[0];
39+
expect(callArgs[0]).toEqual('manifest.json');
40+
return JSON.parse(callArgs[1].source().toString()) as Manifest;
41+
}
42+
43+
it('should work for manifest v2', () => {
44+
expect(
45+
runWith({
46+
'content_scripts/content-0': [
47+
'content_scripts/content-0.css',
48+
'content_scripts/content-0.js.map',
49+
],
50+
'content_scripts/content-1': [
51+
'content_scripts/content-1.css',
52+
'content_scripts/content-1.js.map',
53+
],
54+
}, {
55+
manifest_version: 2,
56+
content_scripts: [
57+
{
58+
matches: ['<all_urls>'],
59+
run_at: 'document_start',
60+
js: [
61+
'content_scripts/content-0.js'
62+
],
63+
css: []
64+
},
65+
{
66+
matches: ['<all_urls>'],
67+
run_at: 'document_start',
68+
js: [
69+
'content_scripts/content-1.js'
70+
],
71+
css: []
72+
}
73+
]
74+
})
75+
).toMatchObject({
76+
web_accessible_resources: [
77+
'content_scripts/content-0.css',
78+
'content_scripts/content-0.js.map',
79+
'content_scripts/content-1.css',
80+
'content_scripts/content-1.js.map',
81+
]
82+
});
83+
});
84+
85+
it('should work for manifest v3', () => {
86+
expect(
87+
runWith({
88+
'content_scripts/content-0': [
89+
'content_scripts/content-0.css',
90+
'content_scripts/content-0.js.map',
91+
],
92+
'content_scripts/content-1': [
93+
'content_scripts/content-1.css',
94+
'content_scripts/content-1.js.map',
95+
],
96+
}, {
97+
manifest_version: 3,
98+
content_scripts: [
99+
{
100+
matches: ['<all_urls>'],
101+
run_at: 'document_start',
102+
js: [
103+
'content_scripts/content-0.js'
104+
],
105+
css: []
106+
},
107+
{
108+
matches: ['<all_urls>'],
109+
run_at: 'document_start',
110+
js: [
111+
'content_scripts/content-1.js'
112+
],
113+
css: []
114+
}
115+
]
116+
})
117+
).toMatchObject({
118+
web_accessible_resources: [
119+
{
120+
matches: ['<all_urls>'],
121+
resources: [
122+
'content_scripts/content-0.css',
123+
'content_scripts/content-1.css',
124+
],
125+
}
126+
]
127+
});
128+
});
129+
130+
it('should work if there is existing web_accessible_resources', () => {
131+
expect(
132+
runWith({
133+
'content_scripts/content-0': [
134+
'content_scripts/content-0.css',
135+
'content_scripts/content-0.js.map',
136+
],
137+
'content_scripts/content-1': [
138+
'content_scripts/content-1.css',
139+
'content_scripts/content-1.js.map',
140+
],
141+
}, {
142+
manifest_version: 3,
143+
content_scripts: [
144+
{
145+
matches: ['<all_urls>'],
146+
run_at: 'document_start',
147+
js: [
148+
'content_scripts/content-0.js'
149+
],
150+
css: []
151+
},
152+
{
153+
matches: ['https://example.com/some/path'],
154+
run_at: 'document_start',
155+
js: [
156+
'content_scripts/content-1.js'
157+
],
158+
css: []
159+
}
160+
],
161+
web_accessible_resources: [
162+
{
163+
matches: ['<all_urls>'],
164+
resources: [
165+
'my-file.css',
166+
],
167+
}
168+
]
169+
})
170+
).toMatchObject({
171+
web_accessible_resources: [
172+
{
173+
matches: ['<all_urls>'],
174+
resources: [
175+
'my-file.css',
176+
'content_scripts/content-0.css',
177+
],
178+
},
179+
{
180+
matches: ['https://example.com/*'],
181+
resources: [
182+
'content_scripts/content-1.css',
183+
],
184+
}
185+
]
186+
});
187+
});
188+
189+
it('should not add web_accessible_resources if there is no entries', () => {
190+
expect(
191+
runWith({}, {
192+
manifest_version: 3,
193+
background: {
194+
service_worker: 'background.js',
195+
}
196+
}).web_accessible_resources
197+
).toBeUndefined();
198+
});
199+
});

programs/develop/webpack/plugin-extension/feature-web-resources/index.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class WebResourcesPlugin {
5656

5757
if (existingResource) {
5858
resources.forEach((resource) => {
59-
if (!existingResource.resources.includes(resource)) {
59+
if (!existingResource.resources.includes(resource) && !resource.endsWith('.map')) {
6060
existingResource.resources.push(resource)
6161
}
6262
})
@@ -81,13 +81,17 @@ export class WebResourcesPlugin {
8181
}
8282

8383
if (manifest.manifest_version === 3) {
84-
manifest.web_accessible_resources =
85-
webAccessibleResourcesV3 as Manifest['web_accessible_resources']
84+
if (webAccessibleResourcesV3.length > 0) {
85+
manifest.web_accessible_resources =
86+
webAccessibleResourcesV3 as Manifest['web_accessible_resources']
87+
}
8688
} else {
87-
// @ts-expect-error - web_accessible_resources is a string[] in V2
88-
manifest.web_accessible_resources = Array.from(
89-
new Set(webAccessibleResourcesV2)
90-
)
89+
if (webAccessibleResourcesV2.length > 0) {
90+
// @ts-expect-error - web_accessible_resources is a string[] in V2
91+
manifest.web_accessible_resources = Array.from(
92+
new Set(webAccessibleResourcesV2)
93+
)
94+
}
9195
}
9296

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

0 commit comments

Comments
 (0)