Skip to content

Commit

Permalink
fix(gatsby-plugin-sharp): Pass format-specific options in image-data (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic authored Jan 4, 2021
1 parent b72b5bc commit 7378fcf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-image/src/babel-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const SHARP_ATTRIBUTES = new Set([
`maxHeight`,
`quality`,
`avifOptions`,
`jpegOptions`,
`jpgOptions`,
`pngOptions`,
`webpOptions`,
`blurredOptions`,
Expand Down
14 changes: 9 additions & 5 deletions packages/gatsby-plugin-sharp/src/image-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,26 +148,30 @@ export async function generateImageData({
}

let primaryFormat: ImageFormat | undefined
let options: Record<string, unknown> | undefined
if (useAuto) {
primaryFormat = normalizeFormat(metadata.format || file.extension)
} else if (formats.has(`png`)) {
primaryFormat = `png`
options = args.pngOptions
} else if (formats.has(`jpg`)) {
primaryFormat = `jpg`
options = args.jpgOptions
} else if (formats.has(`webp`)) {
primaryFormat = `webp`
options = args.webpOptions
} else if (formats.has(`avif`)) {
reporter.warn(
`Image ${file.relativePath} specified only AVIF format, with no fallback format. This will mean your site cannot be viewed in all browsers.`
)
primaryFormat = `avif`
options = args.webpOptions
}

const optionsMap = {
jpg: args.jpgOptions,
png: args.pngOptions,
webp: args.webpOptions,
avif: args.avifOptions,
}

const options = primaryFormat ? optionsMap[primaryFormat] : undefined

const imageSizes: {
sizes: Array<number>
presentationWidth: number
Expand Down

0 comments on commit 7378fcf

Please sign in to comment.