Skip to content

Commit 107926a

Browse files
ascorbicgatsbybot
and
gatsbybot
authored
fix(gatsby-plugin-sharp): Fix defaults handling (#29564)
Co-authored-by: gatsbybot <[email protected]>
1 parent a5210b2 commit 107926a

File tree

3 files changed

+98
-20
lines changed

3 files changed

+98
-20
lines changed

packages/gatsby-plugin-sharp/README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,30 @@ plugins: [
3030
{
3131
resolve: `gatsby-plugin-sharp`,
3232
options: {
33-
// Available options and their defaults:
33+
// Defaults used for gatsbyImageData and StaticImage
34+
defaults: {},
35+
// Set to false to allow builds to continue on image errors
36+
failOnError: true,
37+
// deprecated options and their defaults:
3438
base64Width: 20,
3539
forceBase64Format: ``, // valid formats: png,jpg,webp
3640
useMozJpeg: process.env.GATSBY_JPEG_ENCODER === `MOZJPEG`,
3741
stripMetadata: true,
3842
defaultQuality: 50,
39-
failOnError: true,
4043
},
4144
},
4245
]
4346
```
4447

48+
## Options
49+
50+
- `defaults`: default values used for `gatsbyImageData` and `StaticImage` from [gatsby-plugin-image](https://www.gatsbyjs.com/plugins/gatsby-plugin-image).
51+
Available options are: `formats`,`placeholder`,`quality`,`breakpoints`,`backgroundColor`,`tracedSVGOptions`,`blurredOptions`,`jpgOptions`,`pngOptions`,`webpOptions`,`avifOptions`.
52+
For details of these, see [the reference guide](https://www.gatsbyjs.com/docs/reference/built-in-components/gatsby-plugin-image).
53+
- `failOnError`: default = `true`. By default builds will fail if there is a corrupted image. Set to false to continue the build on error. The image will return `undefined`.
54+
55+
Other options are deprecated, and should only be used for the legacy `fixed` and `fluid` functions.
56+
4557
## Methods
4658

4759
### resize

packages/gatsby-plugin-sharp/src/__tests__/plugin-options.ts

+75-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
import { testPluginOptionsSchema } from "gatsby-plugin-utils"
22
import { pluginOptionsSchema } from "../../gatsby-node"
3+
import { doMergeDefaults } from "../plugin-options"
4+
5+
const defaults = {
6+
formats: [`auto`, `webp`],
7+
placeholder: `dominantColor`,
8+
quality: 50,
9+
breakpoints: [100, 200],
10+
backgroundColor: `rebeccapurple`,
11+
tracedSVGOptions: {},
12+
blurredOptions: { quality: 20 },
13+
jpgOptions: { quality: 20 },
14+
pngOptions: { quality: 20 },
15+
webpOptions: { quality: 20 },
16+
avifOptions: { quality: 20 },
17+
}
318

419
describe(`pluginOptionsSchema`, () => {
520
it(`should reject incorrect options`, async () => {
@@ -40,25 +55,70 @@ describe(`pluginOptionsSchema`, () => {
4055
})
4156

4257
it(`should accept correct options`, async () => {
43-
const options = {
44-
defaults: {
45-
formats: [`auto`, `webp`],
46-
placeholder: `dominantColor`,
47-
quality: 50,
48-
breakpoints: [100, 200],
49-
backgroundColor: `rebeccapurple`,
50-
tracedSVGOptions: {},
51-
blurredOptions: { quality: 20 },
52-
jpgOptions: { quality: 20 },
53-
pngOptions: { quality: 20 },
54-
webpOptions: { quality: 20 },
55-
avifOptions: { quality: 20 },
56-
},
57-
}
58+
const options = { defaults }
5859
const { isValid } = await testPluginOptionsSchema(
5960
pluginOptionsSchema,
6061
options
6162
)
6263
expect(isValid).toBe(true)
6364
})
6465
})
66+
67+
describe(`plugin defaults`, () => {
68+
it(`uses defaults`, () => {
69+
const output = doMergeDefaults({}, defaults)
70+
expect(output).toMatchInlineSnapshot(`
71+
Object {
72+
"avifOptions": Object {
73+
"quality": 20,
74+
},
75+
"backgroundColor": "rebeccapurple",
76+
"blurredOptions": Object {
77+
"quality": 20,
78+
},
79+
"breakpoints": Array [
80+
100,
81+
200,
82+
],
83+
"formats": Array [
84+
"auto",
85+
"webp",
86+
],
87+
"jpgOptions": Object {
88+
"quality": 20,
89+
},
90+
"placeholder": "dominantColor",
91+
"pngOptions": Object {
92+
"quality": 20,
93+
},
94+
"quality": 50,
95+
"tracedSVGOptions": Object {},
96+
"webpOptions": Object {
97+
"quality": 20,
98+
},
99+
}
100+
`)
101+
})
102+
103+
it(`allows overrides`, () => {
104+
const output = doMergeDefaults({ backgroundColor: `papayawhip` }, defaults)
105+
expect(output.backgroundColor).toEqual(`papayawhip`)
106+
expect(output.quality).toEqual(50)
107+
})
108+
109+
it(`allows overrides of arrays`, () => {
110+
const output = doMergeDefaults({ formats: [`auto`, `avif`] }, defaults)
111+
expect(output.formats).toEqual([`auto`, `avif`])
112+
expect(output.breakpoints).toEqual([100, 200])
113+
})
114+
115+
it(`allows override of deep objects`, () => {
116+
const output = doMergeDefaults({ avifOptions: { quality: 50 } }, defaults)
117+
expect(output.avifOptions).toEqual({ quality: 50 })
118+
})
119+
120+
it(`allows extra keys in objects`, () => {
121+
const output = doMergeDefaults({ avifOptions: { speed: 50 } }, defaults)
122+
expect(output.avifOptions).toEqual({ quality: 20, speed: 50 })
123+
})
124+
})

packages/gatsby-plugin-sharp/src/plugin-options.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,20 @@ exports.createTransformObject = args => {
7474
/**
7575
* Used for gatsbyImageData and StaticImage only
7676
*/
77-
exports.mergeDefaults = args => {
78-
const { defaults } = pluginOptions
77+
exports.mergeDefaults = args => doMergeDefaults(args, pluginOptions.defaults)
78+
79+
const customizer = (objValue, srcValue) =>
80+
Array.isArray(objValue) ? srcValue : undefined
81+
82+
function doMergeDefaults(args, defaults) {
7983
if (!defaults) {
8084
return args
8185
}
82-
return _.defaultsDeep(args, defaults)
86+
return _.mergeWith({}, defaults, args, customizer)
8387
}
8488

89+
exports.doMergeDefaults = doMergeDefaults
90+
8591
exports.healOptions = (
8692
{ defaultQuality: quality, base64Width },
8793
args,

0 commit comments

Comments
 (0)