-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-node.js
144 lines (128 loc) · 3.85 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const _ = require(`lodash`)
const babylon = require(`@babel/parser`)
const traverse = require(`@babel/traverse`).default
const fileExtsToProcess = [`js`, `jsx`, `ts`, `tsx`]
function unstable_shouldOnCreateNode({ node }) {
// This only processes JavaScript and TypeScript files.
return fileExtsToProcess.includes(node.extension)
}
async function onCreateNode({
node,
actions,
loadNodeContent,
createContentDigest,
}) {
if (!unstable_shouldOnCreateNode({ node })) {
return
}
const { createNode, createParentChildLink } = actions
const code = await loadNodeContent(node)
const options = {
sourceType: `module`,
allowImportExportEverywhere: true,
plugins: [
`jsx`,
`doExpressions`,
`objectRestSpread`,
[
`decorators`,
{
decoratorsBeforeExport: true,
},
],
`classProperties`,
`exportExtensions`,
`asyncGenerators`,
`functionBind`,
`functionSent`,
`dynamicImport`,
_.includes([`ts`, `tsx`], node.extension) ? `typescript` : `flow`,
],
}
let exportsData, frontmatter, error
try {
const ast = babylon.parse(code, options)
const parseData = function parseData(node) {
let value
if (node.type === `TemplateLiteral`) {
// Experimental basic support for template literals:
// Extract and join any text content; ignore interpolations
value = node.quasis.map(quasi => quasi.value.cooked).join(``)
} else if (node.type === `ObjectExpression`) {
value = {}
node.properties.forEach(elem => {
value[elem.key.name] = parseData(elem.value)
})
} else if (node.type === `ArrayExpression`) {
value = node.elements.map(elem => parseData(elem))
} else {
value = node.value
}
return value
}
frontmatter = {}
error = false
traverse(ast, {
AssignmentExpression: function AssignmentExpression(astPath) {
if (
astPath.node.left.type === `MemberExpression` &&
astPath.node.left.property.name === `frontmatter`
) {
astPath.node.right.properties.forEach(node => {
frontmatter[node.key.name] = parseData(node.value)
})
}
},
ExportNamedDeclaration: function ExportNamedDeclaration(astPath) {
const { declaration } = astPath.node
if (declaration && declaration.type === `VariableDeclaration`) {
const dataVariableDeclarator = _.find(
declaration.declarations,
d => d.id.name === `frontmatter`
)
if (dataVariableDeclarator && dataVariableDeclarator.init) {
dataVariableDeclarator.init.properties.forEach(node => {
frontmatter[node.key.name] = parseData(node.value)
})
}
}
},
})
} catch (e) {
// stick the error on the query so the user can
// react to an error as they see fit
error = {
err: true,
code: e.code,
message: e.message,
stack: e.stack,
}
} finally {
// only create node if frontmatter is not empty
if (!_.isEmpty(frontmatter)) {
exportsData = {
...frontmatter,
error: error,
}
const contentDigest = createContentDigest(node)
const nodeData = {
id: `${node.id} >>> JavascriptFrontmatter`,
children: [],
parent: node.id,
node: { ...node },
internal: {
contentDigest,
type: `JavascriptFrontmatter`,
},
}
nodeData.frontmatter = { ...exportsData }
if (node.internal.type === `File`) {
nodeData.fileAbsolutePath = node.absolutePath
}
createNode(nodeData)
createParentChildLink({ parent: node, child: nodeData })
}
}
}
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
exports.onCreateNode = onCreateNode