Skip to content

Commit 201a4f5

Browse files
moonmeisterDSchau
authored andcommitted
fix(gatsby-plugin-manifest): ensure icon_options is stripped (#12907)
* fix: bugs in 'icon_options' implementation * fix: make icons array take presidence over icon_options * test: write icon_options tests and fix bug * fix: small things * Update packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js Co-Authored-By: moonmeister <[email protected]> * Update packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js Co-Authored-By: moonmeister <[email protected]> * test: add test to validate icon_options object is removed from manifest * Update packages/gatsby-plugin-manifest/README.md Co-Authored-By: moonmeister <[email protected]>
1 parent faf96aa commit 201a4f5

File tree

3 files changed

+65
-35
lines changed

3 files changed

+65
-35
lines changed

packages/gatsby-plugin-manifest/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ module.exports = {
190190

191191
## Custom icon options
192192

193-
In order to specify manifest options merged with each item of the `icons` array, `icon_options` may be used as follows:
193+
The `icon_options` object may be used to iteratively add configuration items to the `icons` array. Any options included in this object will be merged with each object of the `icons` array (custom or default). Key value pairs already in the `icons` array will take precedence over duplicate items in the `icon_options` array.
194+
195+
`icon_options` may be used as follows:
194196

195197
```js
196198
// in gatsby-config.js
@@ -205,7 +207,7 @@ module.exports = {
205207
background_color: `#f7f0eb`,
206208
theme_color: `#a2466c`,
207209
display: `standalone`,
208-
icon: `src/images/icon.png`, // This path is relative to the root of the site.
210+
icon: `src/images/icon.png`,
209211
icon_options: {
210212
// For all the options available, please see:
211213
// https://developer.mozilla.org/en-US/docs/Web/Manifest

packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js

+52-30
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ const manifestOptions = {
6060
src: `icons/icon-48x48.png`,
6161
sizes: `48x48`,
6262
type: `image/png`,
63+
purpose: `all`,
64+
},
65+
{
66+
src: `icons/icon-128x128.png`,
67+
sizes: `128x128`,
68+
type: `image/png`,
6369
},
6470
],
6571
}
@@ -99,23 +105,22 @@ describe(`Test plugin manifest options`, () => {
99105
const icon = `pretend/this/exists.png`
100106
const size = 48
101107

108+
const pluginSpecificOptions = {
109+
icon: icon,
110+
icons: [
111+
{
112+
src: `icons/icon-48x48.png`,
113+
sizes: `${size}x${size}`,
114+
type: `image/png`,
115+
},
116+
],
117+
}
118+
102119
await onPostBootstrap(
103120
{ reporter },
104121
{
105-
name: `GatsbyJS`,
106-
short_name: `GatsbyJS`,
107-
start_url: `/`,
108-
background_color: `#f7f0eb`,
109-
theme_color: `#a2466c`,
110-
display: `standalone`,
111-
icon,
112-
icons: [
113-
{
114-
src: `icons/icon-48x48.png`,
115-
sizes: `${size}x${size}`,
116-
type: `image/png`,
117-
},
118-
],
122+
...manifestOptions,
123+
...pluginSpecificOptions,
119124
}
120125
)
121126

@@ -126,23 +131,15 @@ describe(`Test plugin manifest options`, () => {
126131
it(`fails on non existing icon`, async () => {
127132
fs.statSync.mockReturnValueOnce({ isFile: () => false })
128133

134+
const pluginSpecificOptions = {
135+
icon: `non/existing/path`,
136+
}
137+
129138
return onPostBootstrap(
130139
{ reporter },
131140
{
132-
name: `GatsbyJS`,
133-
short_name: `GatsbyJS`,
134-
start_url: `/`,
135-
background_color: `#f7f0eb`,
136-
theme_color: `#a2466c`,
137-
display: `standalone`,
138-
icon: `non/existing/path`,
139-
icons: [
140-
{
141-
src: `icons/icon-48x48.png`,
142-
sizes: `48x48`,
143-
type: `image/png`,
144-
},
145-
],
141+
...manifestOptions,
142+
...pluginSpecificOptions,
146143
}
147144
).catch(err => {
148145
expect(sharp).toHaveBeenCalledTimes(0)
@@ -159,6 +156,7 @@ describe(`Test plugin manifest options`, () => {
159156
plugins: [],
160157
theme_color_in_head: false,
161158
cache_busting_mode: `name`,
159+
icon_options: {},
162160
}
163161
await onPostBootstrap(
164162
{ reporter },
@@ -167,6 +165,7 @@ describe(`Test plugin manifest options`, () => {
167165
...pluginSpecificOptions,
168166
}
169167
)
168+
170169
expect(sharp).toHaveBeenCalledTimes(0)
171170
const content = JSON.parse(fs.writeFileSync.mock.calls[0][1])
172171
expect(content).toEqual(manifestOptions)
@@ -188,7 +187,7 @@ describe(`Test plugin manifest options`, () => {
188187
}
189188
)
190189

191-
expect(sharp).toHaveBeenCalledTimes(2)
190+
expect(sharp).toHaveBeenCalledTimes(3)
192191
const content = JSON.parse(fs.writeFileSync.mock.calls[0][1])
193192
expect(content).toEqual(manifestOptions)
194193
})
@@ -209,8 +208,31 @@ describe(`Test plugin manifest options`, () => {
209208
}
210209
)
211210

212-
expect(sharp).toHaveBeenCalledTimes(2)
211+
expect(sharp).toHaveBeenCalledTimes(3)
213212
const content = JSON.parse(fs.writeFileSync.mock.calls[0][1])
214213
expect(content).toEqual(manifestOptions)
215214
})
215+
216+
it(`icon options iterator adds options and the icon array take precedence`, async () => {
217+
fs.statSync.mockReturnValueOnce({ isFile: () => true })
218+
219+
const pluginSpecificOptions = {
220+
icon: `images/gatsby-logo.png`,
221+
icon_options: {
222+
purpose: `maskable`,
223+
},
224+
}
225+
await onPostBootstrap(
226+
{ reporter },
227+
{
228+
...manifestOptions,
229+
...pluginSpecificOptions,
230+
}
231+
)
232+
233+
expect(sharp).toHaveBeenCalledTimes(3)
234+
const content = JSON.parse(fs.writeFileSync.mock.calls[0][1])
235+
expect(content.icons[0].purpose).toEqual(`all`)
236+
expect(content.icons[1].purpose).toEqual(`maskable`)
237+
})
216238
})

packages/gatsby-plugin-manifest/src/gatsby-node.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,22 @@ exports.onPostBootstrap = async ({ reporter }, pluginOptions) => {
5252
delete manifest.theme_color_in_head
5353
delete manifest.cache_busting_mode
5454
delete manifest.crossOrigin
55+
delete manifest.icon_options
5556

5657
// If icons are not manually defined, use the default icon set.
5758
if (!manifest.icons) {
5859
manifest.icons = defaultIcons
5960
}
6061

6162
// Specify extra options for each icon (if requested).
62-
manifest.icons.forEach(icon => {
63-
Object.assign(icon, pluginOptions.icon_options)
64-
})
63+
if (pluginOptions.icon_options) {
64+
manifest.icons = manifest.icons.map(icon => {
65+
return {
66+
...pluginOptions.icon_options,
67+
...icon,
68+
}
69+
})
70+
}
6571

6672
// Determine destination path for icons.
6773
const iconPath = path.join(`public`, path.dirname(manifest.icons[0].src))

0 commit comments

Comments
 (0)