-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
on-create-node.js
143 lines (125 loc) · 3.46 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
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 contentDigest = val => createContentDigest(val)
module.exports = async (
{
node,
loadNodeContent,
actions,
createNodeId,
getNode,
getNodes,
getNodesByType,
reporter,
cache,
pathPrefix,
...helpers
},
pluginOptions
) => {
const { createNode, createParentChildLink } = actions
const options = defaultOptions(pluginOptions)
// options check to stop transformation of the node
if (options.shouldBlockNodeFromTransformation(node)) {
return
}
// if we shouldn't process this node, then return
if (
!(node.internal.type === `File` && options.extensions.includes(node.ext)) &&
!(
node.internal.type !== `File` &&
options.mediaTypes.includes(node.internal.mediaType)
)
) {
return
}
const content = await loadNodeContent(node)
const { mdxNode, scopeIdentifiers, scopeImports } = await createMDXNode({
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,
})
}
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
}
},
},
}
})
}
}