Skip to content

Commit

Permalink
perf(gatsby-plugin-mdx): prevent babel parse step at sourcing time
Browse files Browse the repository at this point in the history
The mdx plugin was doing its default parsing step for every time it got called. At sourcing time it's only called to retrieve the import bindings and for this we can be much faster by manually processing the import statements. So that's what this does.

A very simple flat no-image mdx benchmark cuts down sourcing time in half for this change.
  • Loading branch information
pvdz committed Jul 1, 2020
1 parent 5c4c008 commit 10daace
Show file tree
Hide file tree
Showing 5 changed files with 1,154 additions and 18 deletions.
31 changes: 14 additions & 17 deletions packages/gatsby-plugin-mdx/gatsby/on-create-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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 genMDX = require(`../utils/gen-mdx`)
const { findImports } = require(`../utils/gen-mdx`)

const contentDigest = val => createContentDigest(val)

Expand Down Expand Up @@ -56,22 +56,19 @@ module.exports = async (
createParentChildLink({ parent: node, child: mdxNode })

// write scope files into .cache for later consumption
const { scopeImports, scopeIdentifiers } = await genMDX(
{
node: mdxNode,
getNode,
getNodes,
reporter,
cache,
pathPrefix,
options,
loadNodeContent,
actions,
createNodeId,
...helpers,
},
{ forceDisableCache: true }
)
const { scopeImports, scopeIdentifiers } = await findImports({
node: mdxNode,
getNode,
getNodes,
reporter,
cache,
pathPrefix,
options,
loadNodeContent,
actions,
createNodeId,
...helpers,
})
await cacheScope({
cache,
scopeIdentifiers,
Expand Down

0 comments on commit 10daace

Please sign in to comment.