Skip to content

Commit 4519c05

Browse files
LekoArtsGatsbyJS Bot
andauthored
fix(gatsby-transformer-sharp): Warn when using unsupported ext… (#20782)
Co-authored-by: GatsbyJS Bot <mathews.kyle+gatsbybot@gmail.com>
1 parent 232cf77 commit 4519c05

File tree

6 files changed

+62
-8
lines changed

6 files changed

+62
-8
lines changed

packages/gatsby-transformer-sharp/src/__tests__/__snapshots__/gatsby-node.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,7 @@ Array [
4141
],
4242
]
4343
`;
44+
45+
exports[`Process image nodes correctly doesn't create an ImageSharp node for a .gif file 1`] = `Array []`;
46+
47+
exports[`Process image nodes correctly doesn't create an ImageSharp node for a .gif file 2`] = `Array []`;

packages/gatsby-transformer-sharp/src/__tests__/gatsby-node.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,30 @@ describe(`Process image nodes correctly`, () => {
2727
expect(createParentChildLink).toHaveBeenCalledTimes(1)
2828
})
2929
})
30+
it(`doesn't create an ImageSharp node for a .gif file`, async () => {
31+
const node = {
32+
extension: `gif`,
33+
id: `whatever`,
34+
children: [],
35+
internal: {
36+
contentDigest: `whatever`,
37+
},
38+
}
39+
const createNode = jest.fn()
40+
const createParentChildLink = jest.fn()
41+
const actions = { createNode, createParentChildLink }
42+
const createNodeId = jest.fn()
43+
createNodeId.mockReturnValue(`uuid-from-gatsby`)
44+
45+
await onCreateNode({
46+
node,
47+
actions,
48+
createNodeId,
49+
}).then(() => {
50+
expect(createNode.mock.calls).toMatchSnapshot()
51+
expect(createParentChildLink.mock.calls).toMatchSnapshot()
52+
expect(createNode).toHaveBeenCalledTimes(0)
53+
expect(createParentChildLink).toHaveBeenCalledTimes(0)
54+
})
55+
})
3056
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const supportedExtensions = require(`./supported-extensions`)
2+
3+
module.exports = ({ createResolvers, reporter }) => {
4+
const resolvers = {
5+
File: {
6+
childImageSharp: {
7+
resolve: (parent, args, context, info) => {
8+
if (!supportedExtensions[parent.extension]) {
9+
reporter.warn(
10+
`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}`
11+
)
12+
}
13+
return info.originalResolver(parent, args, context, info)
14+
},
15+
},
16+
},
17+
}
18+
19+
createResolvers(resolvers)
20+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
exports.onCreateNode = require(`./on-node-create`)
22
exports.createSchemaCustomization = require(`./customize-schema`)
3+
exports.createResolvers = require(`./create-resolvers`)

packages/gatsby-transformer-sharp/src/on-node-create.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
const supportedExtensions = {
2-
jpeg: true,
3-
jpg: true,
4-
png: true,
5-
webp: true,
6-
tif: true,
7-
tiff: true,
8-
}
1+
import supportedExtensions from "./supported-extensions"
92

103
module.exports = async function onCreateNode({ node, actions, createNodeId }) {
114
const { createNode, createParentChildLink } = actions
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const supportedExtensions = {
2+
jpeg: true,
3+
jpg: true,
4+
png: true,
5+
webp: true,
6+
tif: true,
7+
tiff: true,
8+
}
9+
10+
export default supportedExtensions

0 commit comments

Comments
 (0)