-
Notifications
You must be signed in to change notification settings - Fork 320
/
gatsby-node.js
95 lines (83 loc) · 1.94 KB
/
gatsby-node.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const { transforms, queries } = require('blocks-ui')
const BlockTemplate = require.resolve('./src/templates/block')
const toComponentName = name => name.charAt(0).toUpperCase() + name.slice(1)
exports.sourceNodes = ({ actions }) => {
actions.createTypes(`
type Block implements Node {
displayName: String!
slug: String!
src: String!
usage: String!
transformed: String!
}
`)
}
exports.onCreateNode = async ({
node,
actions,
createNodeId,
loadNodeContent,
createContentDigest
}) => {
const { createNode, createParentChildLink } = actions
if (
node.sourceInstanceName !== 'blocks' ||
node.ext !== '.js' ||
node.name === 'index'
) {
return
}
const src = await loadNodeContent(node)
const usage = queries.getBlocksUsage(src)
const transformed = transforms.toTransformedJSX(
'export default () => ' + usage
)
const displayName = [node.relativeDirectory.replace(/s$/, ''), node.name]
.map(toComponentName)
.join('')
const nodeId = `${node.id}--${displayName}--Block`
const contentDigest = createContentDigest(src)
const slug = ['blocks', node.relativeDirectory, node.name].join('/')
const blocksNode = {
src,
slug,
transformed,
displayName,
usage,
id: createNodeId(nodeId),
children: [],
internal: {
contentDigest,
type: 'Block'
}
}
createParentChildLink({ parent: node, child: blocksNode })
createNode(blocksNode)
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const result = await graphql(`
{
blocks: allBlock {
nodes {
id
slug
}
}
}
`)
result.data.blocks.nodes.forEach(block => {
createPage({
path: block.slug,
component: BlockTemplate,
context: block
})
})
}
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
node: {
fs: 'empty'
}
})
}