-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Expand file tree
/
Copy pathgatsby-node.js
More file actions
36 lines (29 loc) · 1.08 KB
/
gatsby-node.js
File metadata and controls
36 lines (29 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const fs = require(`fs-extra`)
exports.onCreateNode = require(`./on-node-create`)
exports.setFieldsOnGraphQLNodeType = require(`./extend-node-type`)
exports.onPreExtractQueries = async ({ store, getNodes }) => {
const program = store.getState().program
// Check if there are any ImageSharp nodes and if gatsby-image is installed. If so
// add fragments for ImageSharp and gatsby-image. The fragment will cause an error
// if there's not ImageSharp nodes and without gatsby-image, the fragment is useless.
const nodes = getNodes()
if (!nodes.some(n => n.internal.type === `ImageSharp`)) {
return
}
let gatsbyImageDoesNotExist = true
try {
require.resolve(`gatsby-image`)
gatsbyImageDoesNotExist = false
} catch (e) {
// Ignore
}
if (gatsbyImageDoesNotExist) {
return
}
// We have both gatsby-image installed as well as ImageSharp nodes so let's
// add our fragments to .cache/fragments.
await fs.copy(
require.resolve(`gatsby-transformer-sharp/src/fragments.js`),
`${program.directory}/.cache/fragments/image-sharp-fragments.js`
)
}