Skip to content

Commit 8b6bfa6

Browse files
authored
feat(gatsby-plugin-sharp): Add image plugin defaults (#29147)
* feat(gatsby-plugin-sharp): Add image plugin defaults * Add transformOptions
1 parent eb2bede commit 8b6bfa6

File tree

7 files changed

+110
-3
lines changed

7 files changed

+110
-3
lines changed

packages/gatsby-plugin-sharp/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"@types/sharp": "^0.26.1",
3434
"babel-preset-gatsby-package": "^0.12.0-next.0",
3535
"cross-env": "^7.0.3",
36-
"gatsby-plugin-image": "^0.7.0-next.0"
36+
"gatsby-plugin-image": "^0.7.0-next.0",
37+
"gatsby-plugin-utils": "^0.9.0-next.0"
3738
},
3839
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-sharp#readme",
3940
"keywords": [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { testPluginOptionsSchema } from "gatsby-plugin-utils"
2+
import { pluginOptionsSchema } from "../../gatsby-node"
3+
4+
describe(`pluginOptionsSchema`, () => {
5+
it(`should reject incorrect options`, async () => {
6+
const options = {
7+
defaults: {
8+
formats: [`gif`, `webp`],
9+
placeholder: `base64`,
10+
quality: `great`,
11+
breakpoints: [`mobile`],
12+
backgroundColor: 0,
13+
tracedSVGOptions: null,
14+
transformOptions: false,
15+
blurredOptions: 1,
16+
jpgOptions: `none`,
17+
pngOptions: [{}],
18+
webpOptions: /a/,
19+
avifOptions: 1,
20+
},
21+
}
22+
const { isValid, errors } = await testPluginOptionsSchema(
23+
pluginOptionsSchema,
24+
options
25+
)
26+
expect(isValid).toBe(false)
27+
expect(errors).toEqual([
28+
`"defaults.formats[0]" must be one of [auto, png, jpg, webp, avif]`,
29+
`"defaults.placeholder" must be one of [tracedSVG, dominantColor, blurred, none]`,
30+
`"defaults.quality" must be a number`,
31+
`"defaults.breakpoints[0]" must be a number`,
32+
`"defaults.backgroundColor" must be a string`,
33+
`"defaults.transformOptions" must be of type object`,
34+
`"defaults.tracedSVGOptions" must be of type object`,
35+
`"defaults.blurredOptions" must be of type object`,
36+
`"defaults.jpgOptions" must be of type object`,
37+
`"defaults.pngOptions" must be of type object`,
38+
`"defaults.avifOptions" must be of type object`,
39+
])
40+
})
41+
42+
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 { isValid } = await testPluginOptionsSchema(
59+
pluginOptionsSchema,
60+
options
61+
)
62+
expect(isValid).toBe(true)
63+
})
64+
})

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

+23
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,27 @@ exports.pluginOptionsSchema = ({ Joi }) =>
215215
stripMetadata: Joi.boolean().default(true),
216216
defaultQuality: Joi.number().default(50),
217217
failOnError: Joi.boolean().default(true),
218+
defaults: Joi.object({
219+
formats: Joi.array().items(
220+
Joi.string().valid(`auto`, `png`, `jpg`, `webp`, `avif`)
221+
),
222+
placeholder: Joi.string().valid(
223+
`tracedSVG`,
224+
`dominantColor`,
225+
`blurred`,
226+
`none`
227+
),
228+
quality: Joi.number(),
229+
breakpoints: Joi.array().items(Joi.number()),
230+
backgroundColor: Joi.string(),
231+
transformOptions: Joi.object(),
232+
tracedSVGOptions: Joi.object(),
233+
blurredOptions: Joi.object(),
234+
jpgOptions: Joi.object(),
235+
pngOptions: Joi.object(),
236+
webpOptions: Joi.object(),
237+
avifOptions: Joi.object(),
238+
}).description(
239+
`Default options used by gatsby-plugin-image. \nSee https://gatsbyjs.com/docs/reference/built-in-components/gatsby-plugin-image/`
240+
),
218241
})

packages/gatsby-plugin-sharp/src/image-data.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Reporter } from "gatsby-cli/lib/reporter/reporter"
55
import { rgbToHex, calculateImageSizes, getSrcSet, getSizes } from "./utils"
66
import { traceSVG, getImageSizeAsync, base64, batchQueueImageResizing } from "."
77
import sharp from "./safe-sharp"
8-
import { createTransformObject } from "./plugin-options"
8+
import { createTransformObject, mergeDefaults } from "./plugin-options"
99
import { reportError } from "./report-error"
1010

1111
const DEFAULT_BLURRED_IMAGE_WIDTH = 20
@@ -92,6 +92,8 @@ export async function generateImageData({
9292
reporter,
9393
cache,
9494
}: IImageDataArgs): Promise<IGatsbyImageData | undefined> {
95+
args = mergeDefaults(args)
96+
9597
const {
9698
layout = `constrained`,
9799
placeholder = `dominantColor`,

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

+11
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ exports.createTransformObject = args => {
7171
return _.pickBy(options, _.identity)
7272
}
7373

74+
/**
75+
* Used for gatsbyImageData and StaticImage only
76+
*/
77+
exports.mergeDefaults = args => {
78+
const { defaults } = pluginOptions
79+
if (!defaults) {
80+
return args
81+
}
82+
return _.defaultsDeep(args, defaults)
83+
}
84+
7485
exports.healOptions = (
7586
{ defaultQuality: quality, base64Width },
7687
args,

packages/gatsby-transformer-sharp/src/customize-schema.js

-1
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,6 @@ const imageNodeType = ({
430430
},
431431
placeholder: {
432432
type: ImagePlaceholderType,
433-
defaultValue: `dominantColor`,
434433
description: stripIndent`
435434
Format of generated placeholder image, displayed while the main image loads.
436435
BLURRED: a blurred, low resolution image, encoded as a base64 data URI (default)

yarn.lock

+7
Original file line numberDiff line numberDiff line change
@@ -11629,6 +11629,13 @@ gatsby-node-helpers@^0.3.0:
1162911629
lodash "^4.17.4"
1163011630
p-is-promise "^1.1.0"
1163111631

11632+
gatsby-plugin-utils@^0.8.0:
11633+
version "0.8.0"
11634+
resolved "https://registry.yarnpkg.com/gatsby-plugin-utils/-/gatsby-plugin-utils-0.8.0.tgz#2ecd848e6e3362ee929e496bc11528267d2fb96e"
11635+
integrity sha512-EQC1U7LQVHaI6jXMbx4ryvA8rV1yYrlyxwO2T4nuLUDOO1STUpKTYCH4ySOEtXi6f4P5v7NxgHkFoid6ayY9HA==
11636+
dependencies:
11637+
joi "^17.2.1"
11638+
1163211639
gatsby-plugin-webfonts@^1.1.4:
1163311640
version "1.1.4"
1163411641
resolved "https://registry.yarnpkg.com/gatsby-plugin-webfonts/-/gatsby-plugin-webfonts-1.1.4.tgz#f6fb8daf0acc4c59511c98964fceca35504014ac"

0 commit comments

Comments
 (0)