Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-plugin-sharp): Pass format-specific options in image data resolver #28826

Merged
merged 1 commit into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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