-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
on-create-node.js
235 lines (203 loc) · 5.56 KB
/
on-create-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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const fs = require(`fs`)
const path = require(`path`)
const babel = require(`@babel/core`)
const { createContentDigest } = require(`gatsby-core-utils`)
const defaultOptions = require(`../utils/default-options`)
const {
createMdxNodeExtraBabel,
createMdxNodeLessBabel,
} = require(`../utils/create-mdx-node`)
const { MDX_SCOPES_LOCATION } = require(`../constants`)
const { findImports } = require(`../utils/gen-mdx`)
const contentDigest = val => createContentDigest(val)
function unstable_shouldOnCreateNode({ node }, pluginOptions) {
const options = defaultOptions(pluginOptions)
return _unstable_shouldOnCreateNode({ node }, options)
}
function _unstable_shouldOnCreateNode({ node }, options) {
// options check to stop transformation of the node
if (options.shouldBlockNodeFromTransformation(node)) {
return false
}
return node.internal.type === `File`
? options.extensions.includes(node.ext)
: options.mediaTypes.includes(node.internal.mediaType)
}
module.exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
async function onCreateNode(api, pluginOptions) {
const options = defaultOptions(pluginOptions)
if (!_unstable_shouldOnCreateNode({ node: api.node }, options)) {
return
}
const content = await api.loadNodeContent(api.node)
if (options.lessBabel) {
await onCreateNodeLessBabel(content, api, options)
} else {
await onCreateNodeExtraBabel(content, api, options)
}
}
module.exports.onCreateNode = onCreateNode
async function onCreateNodeExtraBabel(
content,
{
node,
loadNodeContent,
actions,
createNodeId,
getNode,
getNodes,
getNodesByType,
reporter,
cache,
pathPrefix,
...helpers
},
options
) {
const { createNode, createParentChildLink } = actions
const mdxNode = await createMdxNodeExtraBabel({
id: createNodeId(`${node.id} >>> Mdx`),
node,
content,
})
createNode(mdxNode)
createParentChildLink({ parent: node, child: mdxNode })
// write scope files into .cache for later consumption
const { scopeImports, scopeIdentifiers } = await findImports({
node: mdxNode,
getNode,
getNodes,
getNodesByType,
reporter,
cache,
pathPrefix,
options,
loadNodeContent,
actions,
createNodeId,
...helpers,
})
// write scope files into .cache for later consumption
await cacheScope({
cache,
scopeIdentifiers,
scopeImports,
createContentDigest: contentDigest,
parentNode: node,
})
}
async function onCreateNodeLessBabel(
content,
{
node,
loadNodeContent,
actions,
createNodeId,
getNode,
getNodes,
getNodesByType,
reporter,
cache,
pathPrefix,
...helpers
},
options
) {
const { createNode, createParentChildLink } = actions
const {
mdxNode,
scopeIdentifiers,
scopeImports,
} = await createMdxNodeLessBabel({
id: createNodeId(`${node.id} >>> Mdx`),
node,
content,
getNode,
getNodes,
getNodesByType,
reporter,
cache,
pathPrefix,
options,
loadNodeContent,
actions,
createNodeId,
...helpers,
})
createNode(mdxNode)
createParentChildLink({ parent: node, child: mdxNode })
// write scope files into .cache for later consumption
await cacheScope({
cache,
scopeIdentifiers,
scopeImports,
createContentDigest: contentDigest,
parentNode: node,
})
}
const writeCache = new Set()
async function cacheScope({
cache,
scopeImports,
scopeIdentifiers,
createContentDigest,
parentNode,
}) {
// scope files are the imports from an MDX file pulled out and re-exported.
let scopeFileContent = `${scopeImports.join(`\n`)}
export default { ${scopeIdentifiers.join(`, `)} }`
// Multiple files sharing the same imports/exports will lead to the same file writes.
// Prevent writing the same content to the same file over and over again (reduces io pressure).
// This also prevents an expensive babel step whose outcome is based on this same value
if (writeCache.has(scopeFileContent)) {
return
}
// Make sure other calls see this value being processed during async time
writeCache.add(scopeFileContent)
// if parent node is a file, convert relative imports to be
// relative to new .cache location
if (parentNode.internal.type === `File`) {
const instance = new BabelPluginTransformRelativeImports({
parentFilepath: parentNode.dir,
cache: cache,
})
const result = babel.transform(scopeFileContent, {
configFile: false,
plugins: [instance.plugin],
})
scopeFileContent = result.code
}
const filePath = path.join(
cache.directory,
MDX_SCOPES_LOCATION,
`${createContentDigest(scopeFileContent)}.js`
)
fs.writeFileSync(filePath, scopeFileContent)
}
const declare = require(`@babel/helper-plugin-utils`).declare
class BabelPluginTransformRelativeImports {
constructor({ parentFilepath, cache }) {
this.plugin = declare(api => {
api.assertVersion(7)
return {
visitor: {
StringLiteral({ node }) {
let split = node.value.split(`!`)
const nodePath = split.pop()
const loaders = `${split.join(`!`)}${split.length > 0 ? `!` : ``}`
if (nodePath.startsWith(`.`)) {
const valueAbsPath = path.resolve(parentFilepath, nodePath)
const replacementPath =
loaders +
path.relative(
path.join(cache.directory, MDX_SCOPES_LOCATION),
valueAbsPath
)
node.value = replacementPath
}
},
},
}
})
}
}