-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(gatsby-plugin-mdx): performance changes (#26004)
Re-submit of #24595 Drops a babel step This Babel step was used to get exports and remark header but we can use the mdast for this purpose, just like for imports. > This cuts off 20 to 30% of the total runtime of a baseline mdx benchmark! Co-authored-by: Laurie Barth <laurie@LauriesrkLaptop.fios-router.home> Co-authored-by: Peter van der Zee <github-public@qfox.nl>
- Loading branch information
1 parent
33aff39
commit 9e02abe
Showing
6 changed files
with
99 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,17 @@ | ||
const { createContentDigest } = require(`gatsby-core-utils`) | ||
const withDefaultOptions = require(`../utils/default-options`) | ||
const { getNodesByType } = require(`gatsby/dist/redux/nodes.js`) | ||
const createMdxNodeWithScope = require(`../utils/mdx-node-with-scope`) | ||
|
||
const mdx = require(`../utils/mdx`) | ||
const extractExports = require(`../utils/extract-exports`) | ||
|
||
module.exports = async ({ id, node, content }) => { | ||
let code | ||
try { | ||
code = await mdx(content) | ||
} catch (e) { | ||
// add the path of the file to simplify debugging error messages | ||
e.message += `${node.absolutePath}: ${e.message}` | ||
throw e | ||
} | ||
|
||
// extract all the exports | ||
const { frontmatter, ...nodeExports } = extractExports(code) | ||
|
||
const mdxNode = { | ||
async function createMdxNodeLegacy({ id, node, content } = {}) { | ||
const nodeWithScope = await createMdxNodeWithScope({ | ||
id, | ||
children: [], | ||
parent: node.id, | ||
internal: { | ||
content: content, | ||
type: `Mdx`, | ||
}, | ||
} | ||
|
||
mdxNode.frontmatter = { | ||
title: ``, // always include a title | ||
...frontmatter, | ||
} | ||
|
||
mdxNode.excerpt = frontmatter.excerpt | ||
mdxNode.exports = nodeExports | ||
mdxNode.rawBody = content | ||
|
||
// Add path to the markdown file path | ||
if (node.internal.type === `File`) { | ||
mdxNode.fileAbsolutePath = node.absolutePath | ||
} | ||
|
||
mdxNode.internal.contentDigest = createContentDigest(mdxNode) | ||
|
||
return mdxNode | ||
node, | ||
content, | ||
getNodesByType, | ||
options: withDefaultOptions({ plugins: [] }), | ||
}) | ||
return nodeWithScope.mdxNode | ||
} | ||
|
||
// This function is deprecated in favor of createMDXNodeWithScope and slated to be dropped in v3 | ||
module.exports = createMdxNodeLegacy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const { createContentDigest } = require(`gatsby-core-utils`) | ||
|
||
const { findImportsExports } = require(`../utils/gen-mdx`) | ||
|
||
async function createMdxNodeWithScope({ id, node, content, ...helpers }) { | ||
const { | ||
frontmatter, | ||
scopeImports, | ||
scopeExports, | ||
scopeIdentifiers, | ||
} = await findImportsExports({ | ||
node, | ||
rawInput: content, | ||
absolutePath: node.absolutePath, | ||
...helpers, | ||
}) | ||
|
||
const mdxNode = { | ||
id, | ||
children: [], | ||
parent: node.id, | ||
internal: { | ||
content, | ||
type: `Mdx`, | ||
}, | ||
excerpt: frontmatter.excerpt, | ||
exports: scopeExports, | ||
rawBody: content, | ||
frontmatter: { | ||
title: ``, // always include a title | ||
...frontmatter, | ||
}, | ||
} | ||
|
||
// Add path to the markdown file path | ||
if (node.internal.type === `File`) { | ||
mdxNode.fileAbsolutePath = node.absolutePath | ||
} | ||
|
||
mdxNode.internal.contentDigest = createContentDigest(mdxNode) | ||
|
||
return { mdxNode, scopeIdentifiers, scopeImports } | ||
} | ||
|
||
module.exports = createMdxNodeWithScope |