-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
on-create-node.js
151 lines (130 loc) · 3.79 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
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 createMDXNode = 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
module.exports.onCreateNode = async (
{
node,
loadNodeContent,
actions,
createNodeId,
getNode,
getNodes,
reporter,
cache,
pathPrefix,
...helpers
},
pluginOptions
) => {
const { createNode, createParentChildLink } = actions
const options = defaultOptions(pluginOptions)
if (!_unstable_shouldOnCreateNode({ node }, options)) {
return
}
const content = await loadNodeContent(node)
const mdxNode = await createMDXNode({
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,
reporter,
cache,
pathPrefix,
options,
loadNodeContent,
actions,
createNodeId,
...helpers,
})
await cacheScope({
cache,
scopeIdentifiers,
scopeImports,
createContentDigest: contentDigest,
parentNode: node,
})
}
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(`, `)} }`
// 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
}
},
},
}
})
}
}