Skip to content

Commit

Permalink
fix(gatsby-transformer-sharp): Add checkSupportedExtensions… (#22565)
Browse files Browse the repository at this point in the history
* Add 'checkSupportedExtensions' option

* modify "parsing algorithm" note

Co-authored-by: gatsbybot <[email protected]>
  • Loading branch information
LekoArts and gatsbybot authored Mar 27, 2020
1 parent 2d6f701 commit 91e7bfb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
22 changes: 22 additions & 0 deletions packages/gatsby-transformer-sharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ It recognizes files with the following extensions as images.

Each image file is parsed into a node of type `ImageSharp`.

## Configuration options

`checkSupportedExtensions` [boolean][optional]

Sharp only supports certain image formats (see the Parsing algorithm section above) and hence throws a warning when you e.g. use a .gif in an `ImageSharp` query. You'll need to use `publicURL` instead. With this option you can disable the warning behavior.

```javascript
// In your gatsby-config.js
module.exports = {
plugins: [
`gatsby-plugin-sharp`,
{
resolve: `gatsby-transformer-sharp`,
options: {
// The option defaults to true
checkSupportedExtensions: false,
},
},
],
}
```

## Troubleshooting

### Incompatible library version: sharp.node requires version X or later, but Z provides version Y
Expand Down
10 changes: 8 additions & 2 deletions packages/gatsby-transformer-sharp/src/create-resolvers.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
const { supportedExtensions } = require(`./supported-extensions`)

module.exports = ({ createResolvers, reporter }) => {
module.exports = ({ createResolvers, reporter }, pluginOptions = {}) => {
const { checkSupportedExtensions = true } = pluginOptions

const resolvers = {
File: {
childImageSharp: {
resolve: (parent, args, context, info) => {
if (!supportedExtensions[parent.extension]) {
// TODO: In future when components from GraphQL are possible make sure that we can support both supported & unsupported image formats
if (
!supportedExtensions[parent.extension] &&
checkSupportedExtensions
) {
reporter.warn(
`You can't use childImageSharp together with ${parent.name}.${parent.extension} — use publicURL instead. The childImageSharp portion of the query in this file will return null:\n${context.componentPath}`
)
Expand Down

0 comments on commit 91e7bfb

Please sign in to comment.