Skip to content

Commit 7e431fd

Browse files
authored
chore(gatsby-plugin-mdx): Revert "perf(gatsby-plugin-mdx): performance changes" (#26202)
This reverts commit 9e02abe.
1 parent 138ef07 commit 7e431fd

File tree

6 files changed

+73
-99
lines changed

6 files changed

+73
-99
lines changed

packages/gatsby-plugin-mdx/gatsby/on-create-node.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ const babel = require(`@babel/core`)
44
const { createContentDigest } = require(`gatsby-core-utils`)
55

66
const defaultOptions = require(`../utils/default-options`)
7-
const createMDXNodeWithScope = require(`../utils/mdx-node-with-scope`)
7+
const createMDXNode = require(`../utils/create-mdx-node`)
88
const { MDX_SCOPES_LOCATION } = require(`../constants`)
9+
const { findImports } = require(`../utils/gen-mdx`)
910

1011
const contentDigest = val => createContentDigest(val)
1112

@@ -45,14 +46,18 @@ module.exports = async (
4546

4647
const content = await loadNodeContent(node)
4748

48-
const {
49-
mdxNode,
50-
scopeIdentifiers,
51-
scopeImports,
52-
} = await createMDXNodeWithScope({
49+
const mdxNode = await createMDXNode({
5350
id: createNodeId(`${node.id} >>> Mdx`),
5451
node,
5552
content,
53+
})
54+
55+
createNode(mdxNode)
56+
createParentChildLink({ parent: node, child: mdxNode })
57+
58+
// write scope files into .cache for later consumption
59+
const { scopeImports, scopeIdentifiers } = await findImports({
60+
node: mdxNode,
5661
getNode,
5762
getNodes,
5863
reporter,
@@ -64,10 +69,6 @@ module.exports = async (
6469
createNodeId,
6570
...helpers,
6671
})
67-
68-
createNode(mdxNode)
69-
createParentChildLink({ parent: node, child: mdxNode })
70-
7172
await cacheScope({
7273
cache,
7374
scopeIdentifiers,

packages/gatsby-plugin-mdx/loaders/mdx-loader.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const debugMore = require(`debug`)(`gatsby-plugin-mdx-info:mdx-loader`)
2525

2626
const genMdx = require(`../utils/gen-mdx`)
2727
const withDefaultOptions = require(`../utils/default-options`)
28-
const createMDXNodeWithScope = require(`../utils/mdx-node-with-scope`)
28+
const createMDXNode = require(`../utils/create-mdx-node`)
2929
const { createFileNode } = require(`../utils/create-fake-file-node`)
3030

3131
const DEFAULT_OPTIONS = {
@@ -94,7 +94,6 @@ module.exports = async function (content) {
9494
const {
9595
getNode: rawGetNode,
9696
getNodes,
97-
getNodesByType,
9897
reporter,
9998
cache,
10099
pathPrefix,
@@ -122,15 +121,11 @@ module.exports = async function (content) {
122121

123122
let mdxNode
124123
try {
125-
// This node attempts to break the chicken-egg problem, where parsing mdx
126-
// allows for custom plugins, which can receive a mdx node
127-
;({ mdxNode } = await createMDXNodeWithScope({
124+
mdxNode = await createMDXNode({
128125
id: `fakeNodeIdMDXFileABugIfYouSeeThis`,
129126
node: fileNode,
130127
content,
131-
options,
132-
getNodesByType,
133-
}))
128+
})
134129
} catch (e) {
135130
return callback(e)
136131
}
@@ -197,7 +192,6 @@ ${contentWithoutFrontmatter}`
197192
node: { ...mdxNode, rawBody: code },
198193
getNode,
199194
getNodes,
200-
getNodesByType,
201195
reporter,
202196
cache,
203197
pathPrefix,

packages/gatsby-plugin-mdx/loaders/mdx-loader.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ array: [1,2,3]
1616
)`,
1717
namedExports: `export const meta = {author: "chris"}`,
1818
body: `# Some title
19-
19+
2020
a bit of a paragraph
21-
21+
2222
some content`,
2323
}
2424

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,46 @@
1-
const withDefaultOptions = require(`../utils/default-options`)
2-
const { getNodesByType } = require(`gatsby/dist/redux/nodes.js`)
3-
const createMdxNodeWithScope = require(`../utils/mdx-node-with-scope`)
1+
const { createContentDigest } = require(`gatsby-core-utils`)
42

5-
async function createMdxNodeLegacy({ id, node, content } = {}) {
6-
const nodeWithScope = await createMdxNodeWithScope({
3+
const mdx = require(`../utils/mdx`)
4+
const extractExports = require(`../utils/extract-exports`)
5+
6+
module.exports = async ({ id, node, content }) => {
7+
let code
8+
try {
9+
code = await mdx(content)
10+
} catch (e) {
11+
// add the path of the file to simplify debugging error messages
12+
e.message += `${node.absolutePath}: ${e.message}`
13+
throw e
14+
}
15+
16+
// extract all the exports
17+
const { frontmatter, ...nodeExports } = extractExports(code)
18+
19+
const mdxNode = {
720
id,
8-
node,
9-
content,
10-
getNodesByType,
11-
options: withDefaultOptions({ plugins: [] }),
12-
})
13-
return nodeWithScope.mdxNode
14-
}
21+
children: [],
22+
parent: node.id,
23+
internal: {
24+
content: content,
25+
type: `Mdx`,
26+
},
27+
}
28+
29+
mdxNode.frontmatter = {
30+
title: ``, // always include a title
31+
...frontmatter,
32+
}
1533

16-
// This function is deprecated in favor of createMDXNodeWithScope and slated to be dropped in v3
17-
module.exports = createMdxNodeLegacy
34+
mdxNode.excerpt = frontmatter.excerpt
35+
mdxNode.exports = nodeExports
36+
mdxNode.rawBody = content
37+
38+
// Add path to the markdown file path
39+
if (node.internal.type === `File`) {
40+
mdxNode.fileAbsolutePath = node.absolutePath
41+
}
42+
43+
mdxNode.internal.contentDigest = createContentDigest(mdxNode)
44+
45+
return mdxNode
46+
}

packages/gatsby-plugin-mdx/utils/gen-mdx.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,8 @@ ${code}`
194194
module.exports = genMDX // Legacy API, drop in v3 in favor of named export
195195
module.exports.genMDX = genMDX
196196

197-
async function findImportsExports({
197+
async function findImports({
198198
node,
199-
rawInput,
200-
absolutePath = null,
201199
options,
202200
getNode,
203201
getNodes,
@@ -207,7 +205,7 @@ async function findImportsExports({
207205
pathPrefix,
208206
...helpers
209207
}) {
210-
const { data: frontmatter, content } = grayMatter(rawInput)
208+
const { content } = grayMatter(node.rawBody)
211209

212210
const gatsbyRemarkPluginsAsremarkPlugins = await getSourcePluginsAsRemarkPlugins(
213211
{
@@ -228,7 +226,7 @@ async function findImportsExports({
228226
)
229227

230228
const compilerOptions = {
231-
filepath: absolutePath,
229+
filepath: node.fileAbsolutePath,
232230
...options,
233231
remarkPlugins: [
234232
...options.remarkPlugins,
@@ -238,8 +236,8 @@ async function findImportsExports({
238236
const compiler = mdx.createCompiler(compilerOptions)
239237

240238
const fileOpts = { contents: content }
241-
if (absolutePath) {
242-
fileOpts.path = absolutePath
239+
if (node.fileAbsolutePath) {
240+
fileOpts.path = node.fileAbsolutePath
243241
}
244242

245243
let mdast = await compiler.parse(fileOpts)
@@ -249,17 +247,16 @@ async function findImportsExports({
249247
// we don't need to dedupe the symbols here.
250248
const identifiers = []
251249
const imports = []
252-
const exports = []
253250

254251
mdast.children.forEach(node => {
255-
if (node.type === `import`) {
256-
const importCode = node.value
257-
imports.push(importCode)
258-
const bindings = parseImportBindings(importCode)
259-
identifiers.push(...bindings)
260-
} else if (node.type === `export`) {
261-
exports.push(node.value)
262-
}
252+
if (node.type !== `import`) return
253+
254+
const importCode = node.value
255+
256+
imports.push(importCode)
257+
258+
const bindings = parseImportBindings(importCode)
259+
identifiers.push(...bindings)
263260
})
264261

265262
if (!identifiers.includes(`React`)) {
@@ -268,11 +265,9 @@ async function findImportsExports({
268265
}
269266

270267
return {
271-
frontmatter,
272268
scopeImports: imports,
273-
scopeExports: exports,
274269
scopeIdentifiers: identifiers,
275270
}
276271
}
277272

278-
module.exports.findImportsExports = findImportsExports
273+
module.exports.findImports = findImports

packages/gatsby-plugin-mdx/utils/mdx-node-with-scope.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)