Skip to content

Commit eb2bede

Browse files
authored
feat(gatsby-plugin-image): Add support for backgroundColor in sharp (#29141)
* feat(gatsby-plugin-image): Add support for backgroundColor in sharp * Changes from review
1 parent 76be221 commit eb2bede

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

packages/gatsby-plugin-image/src/babel-helpers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const SHARP_ATTRIBUTES = new Set([
2121
`placeholder`,
2222
`tracedSVGOptions`,
2323
`sizes`,
24-
`background`,
24+
`backgroundColor`,
2525
])
2626

2727
export function normalizeProps(

packages/gatsby-plugin-image/src/image-utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export interface ISharpGatsbyImageArgs {
4040
avifOptions?: Record<string, unknown>
4141
blurredOptions?: { width?: number; toFormat?: ImageFormat }
4242
breakpoints?: Array<number>
43+
backgroundColor?: string
4344
}
4445

4546
export interface IImageSizeArgs {

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

+17-18
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const metadataCache = new Map<string, IImageMetadata>()
3131
export async function getImageMetadata(
3232
file: FileNode,
3333
getDominantColor?: boolean
34-
): Promise<IImageMetadata | undefined> {
34+
): Promise<IImageMetadata> {
3535
if (!getDominantColor) {
3636
// If we don't need the dominant color we can use the cheaper size function
3737
const { width, height, type } = await getImageSizeAsync(file)
@@ -57,6 +57,7 @@ export async function getImageMetadata(
5757
metadataCache.set(file.internal.contentDigest, metadata)
5858
} catch (err) {
5959
reportError(`Failed to process image ${file.absolutePath}`, err)
60+
return {}
6061
}
6162

6263
return metadata
@@ -97,6 +98,7 @@ export async function generateImageData({
9798
tracedSVGOptions = {},
9899
transformOptions = {},
99100
quality,
101+
backgroundColor,
100102
} = args
101103

102104
args.formats = args.formats || [`auto`, `webp`]
@@ -118,7 +120,7 @@ export async function generateImageData({
118120
reporter.warn(
119121
`Specifying fullWidth images will ignore the width and height arguments, you may want a constrained image instead. Otherwise, use the breakpoints argument.`
120122
)
121-
args.width = metadata.width
123+
args.width = metadata?.width
122124
args.height = undefined
123125
}
124126

@@ -187,15 +189,19 @@ export async function generateImageData({
187189
reporter,
188190
})
189191

192+
const sharedOptions = {
193+
quality,
194+
...transformOptions,
195+
fit,
196+
cropFocus,
197+
background: backgroundColor,
198+
}
199+
190200
const transforms = imageSizes.sizes.map(outputWidth => {
191201
const width = Math.round(outputWidth)
192202
const transform = createTransformObject({
193-
quality,
194-
...transformOptions,
195-
fit,
196-
cropFocus,
203+
...sharedOptions,
197204
...options,
198-
tracedSVGOptions,
199205
width,
200206
height: Math.round(width / imageSizes.aspectRatio),
201207
toFormat: primaryFormat,
@@ -237,6 +243,7 @@ export async function generateImageData({
237243
const imageProps: IGatsbyImageData = {
238244
layout,
239245
placeholder: undefined,
246+
backgroundColor,
240247
images: {
241248
fallback: {
242249
src: primaryImage.src,
@@ -251,10 +258,7 @@ export async function generateImageData({
251258
const transforms = imageSizes.sizes.map(outputWidth => {
252259
const width = Math.round(outputWidth)
253260
const transform = createTransformObject({
254-
quality,
255-
...transformOptions,
256-
fit,
257-
cropFocus,
261+
...sharedOptions,
258262
...args.avifOptions,
259263
width,
260264
height: Math.round(width / imageSizes.aspectRatio),
@@ -283,10 +287,7 @@ export async function generateImageData({
283287
const transforms = imageSizes.sizes.map(outputWidth => {
284288
const width = Math.round(outputWidth)
285289
const transform = createTransformObject({
286-
quality,
287-
...transformOptions,
288-
fit,
289-
cropFocus,
290+
...sharedOptions,
290291
...args.webpOptions,
291292
width,
292293
height: Math.round(width / imageSizes.aspectRatio),
@@ -317,10 +318,8 @@ export async function generateImageData({
317318
const { src: fallback } = await base64({
318319
file,
319320
args: {
321+
...sharedOptions,
320322
...options,
321-
...transformOptions,
322-
fit,
323-
cropFocus,
324323
toFormatBase64: args.blurredOptions?.toFormat,
325324
width: placeholderWidth,
326325
height: Math.round(placeholderWidth / imageSizes.aspectRatio),

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,8 @@ const imageNodeType = ({
504504
type: TransformOptionsType,
505505
description: `Options to pass to sharp to control cropping and other image manipulations.`,
506506
},
507-
background: {
507+
backgroundColor: {
508508
type: GraphQLString,
509-
defaultValue: `rgba(0,0,0,0)`,
510509
description: `Background color applied to the wrapper. Also passed to sharp to use as a background when "letterboxing" an image to another aspect ratio.`,
511510
},
512511
},

0 commit comments

Comments
 (0)