Skip to content

Commit 2439b44

Browse files
author
LB
authored
feat(gatsby-codemods): Handle or warn on nested options changes (#29046)
* handle or warn on options changes * remove srcSetBreakpoints from list * use forEach instead
1 parent c0e6c92 commit 2439b44

File tree

5 files changed

+69
-5
lines changed

5 files changed

+69
-5
lines changed

packages/gatsby-codemods/src/transforms/__testfixtures__/gatsby-plugin-image/fluid.input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const query = graphql`
88
{
99
file(relativePath: { eq: "headers/default.jpg" }) {
1010
childImageSharp {
11-
fluid(maxWidth: 1000) {
11+
fluid(maxWidth: 1000, maxHeight: 500) {
1212
...GatsbyImageSharpFluid
1313
}
1414
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from "react"
2+
import { graphql } from "gatsby"
3+
4+
export const query = graphql`
5+
{
6+
file(relativePath: {eq: "icon.png"}) {
7+
childImageSharp {
8+
fluid(duotone: {highlight: "#f00e2e", shadow: "#192550"}, rotate: 50) {
9+
...GatsbyImageSharpFluid
10+
}
11+
}
12+
}
13+
}
14+
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from "react"
2+
import { graphql } from "gatsby"
3+
4+
export const query = graphql`{
5+
file(relativePath: {eq: "icon.png"}) {
6+
childImageSharp {
7+
gatsbyImageData(transformOptions: {duotone: {highlight: "#f00e2e", shadow: "#192550"}, rotate: 50}, layout: FULL_WIDTH)
8+
}
9+
}
10+
}
11+
`

packages/gatsby-codemods/src/transforms/__tests__/gatsby-plugin-image-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const tests = [
1818
`optional-chaining`,
1919
`styled`,
2020
`fluid`,
21+
`transform-options`,
2122
`variable-src`,
2223
]
2324

packages/gatsby-codemods/src/transforms/gatsby-plugin-image.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ const legacyFragmentsSVGPlaceholder = [
2525
`GatsbyImageSharpFluid_withWebp_tracedSVG`,
2626
]
2727

28+
const transformOptions = [
29+
`grayscale`,
30+
`duotone`,
31+
`rotate`,
32+
`trim`,
33+
`cropFocus`,
34+
`fit`,
35+
]
36+
37+
const otherOptions = [
38+
`jpegQuality`,
39+
`webpQuality`,
40+
`pngQuality`,
41+
`pngCompressionSpeed`,
42+
]
43+
2844
const typeMapper = {
2945
fixed: `FIXED`,
3046
fluid: `FULL_WIDTH`,
@@ -313,29 +329,51 @@ function processArguments(queryArguments, fragment, layout, state) {
313329
}
314330
queryArguments.push(placeholderArgument)
315331
}
316-
for (let index in queryArguments) {
317-
const argument = queryArguments[index]
332+
let transformOptionsToNest = []
333+
let newLayout = layout
334+
queryArguments.forEach((argument, index) => {
318335
if (argument.name.value === `maxWidth`) {
319336
argument.name.value = `width`
320337
if (layout === `fluid` && Number(argument.value.value) >= 1000) {
321338
delete queryArguments[index]
339+
const maxHeightIndex = queryArguments.findIndex(
340+
arg => arg?.name?.value === `maxHeight`
341+
)
342+
343+
delete queryArguments?.[maxHeightIndex]
344+
322345
console.log(
323346
`maxWidth is no longer supported for fluid (now fullWidth) images. It's been removed from your query in ${state.opts.filename}.`
324347
)
325348
} else if (layout === `fluid` && Number(argument.value.value) < 1000) {
326349
console.log(
327350
`maxWidth is no longer supported for fluid (now fullWidth) images. We've updated the query in ${state.opts.filename} to use a constrained image instead.`
328351
)
329-
return `constrained`
352+
newLayout = `constrained`
330353
}
331354
} else if (argument.name.value === `maxHeight`) {
332355
argument.name.value = `height`
333356
console.log(
334357
`maxHeight is no longer supported for fluid (now fullWidth) images. It's been removed from your query in ${state.opts.filename}.`
335358
)
359+
} else if (transformOptions.includes(argument.name.value)) {
360+
transformOptionsToNest.push(argument)
361+
delete queryArguments[index]
362+
} else if (otherOptions.includes(argument.name.value)) {
363+
console.log(
364+
`${argument.name.value} is now a nested value, please see the API docs and update the query in ${state.opts.filename} manually.`
365+
)
366+
}
367+
})
368+
if (transformOptionsToNest.length > 0) {
369+
const newOptions = {
370+
kind: `Argument`,
371+
name: { kind: `Name`, value: `transformOptions` },
372+
value: { kind: `ObjectValue`, fields: transformOptionsToNest },
336373
}
374+
queryArguments.push(newOptions)
337375
}
338-
return layout
376+
return newLayout
339377
}
340378

341379
function processGraphQLQuery(query, state) {

0 commit comments

Comments
 (0)