Skip to content

Commit

Permalink
perf(gatsby-plugin-mdx): drop another babel step during sourcing
Browse files Browse the repository at this point in the history
This Babel step was used to get exports and remark header but we can use the mdast for this purpose, just like for imports. See PR for perf numbers but they are big.
  • Loading branch information
pvdz committed Jul 15, 2020
1 parent 254b9a9 commit 7299f00
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 51 deletions.
17 changes: 8 additions & 9 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,6 @@ 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)

Expand All @@ -18,6 +17,7 @@ module.exports = async (
createNodeId,
getNode,
getNodes,
getNodesByType,
reporter,
cache,
pathPrefix,
Expand Down Expand Up @@ -46,20 +46,14 @@ module.exports = async (

const content = await loadNodeContent(node)

const mdxNode = await createMDXNode({
const { mdxNode, scopeIdentifiers, scopeImports } = 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,
getNodesByType,
reporter,
cache,
pathPrefix,
Expand All @@ -69,6 +63,11 @@ module.exports = async (
createNodeId,
...helpers,
})

createNode(mdxNode)
createParentChildLink({ parent: node, child: mdxNode })

// write scope files into .cache for later consumption
await cacheScope({
cache,
scopeIdentifiers,
Expand Down
8 changes: 6 additions & 2 deletions packages/gatsby-plugin-mdx/loaders/mdx-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ module.exports = async function (content) {
const {
getNode: rawGetNode,
getNodes,
getNodesByType,
reporter,
cache,
pathPrefix,
Expand Down Expand Up @@ -120,11 +121,13 @@ module.exports = async function (content) {

let mdxNode
try {
mdxNode = await createMDXNode({
;({ mdxNode } = await createMDXNode({
id: `fakeNodeIdMDXFileABugIfYouSeeThis`,
node: fileNode,
content,
})
options,
getNodesByType,
}))
} catch (e) {
return callback(e)
}
Expand Down Expand Up @@ -168,6 +171,7 @@ ${contentWithoutFrontmatter}`
node: { ...mdxNode, rawBody: code },
getNode,
getNodes,
getNodesByType,
reporter,
cache,
pathPrefix,
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-plugin-mdx/loaders/mdx-loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ array: [1,2,3]
)`,
namedExports: `export const meta = {author: "chris"}`,
body: `# Some title
a bit of a paragraph
some content`,
}

Expand Down
71 changes: 47 additions & 24 deletions packages/gatsby-plugin-mdx/utils/create-mdx-node.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
const { createContentDigest } = require(`gatsby-core-utils`)

const mdx = require(`../utils/mdx`)
const extractExports = require(`../utils/extract-exports`)
const { findImportsExports } = require(`../utils/gen-mdx`)

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)
async function createMdxNode({
id,
node,
content,
getNode,
getNodes,
getNodesByType,
reporter,
cache,
pathPrefix,
options,
loadNodeContent,
actions,
createNodeId,
...helpers
}) {
const {
frontmatter,
scopeImports,
scopeExports,
scopeIdentifiers,
} = await findImportsExports({
rawInput: content,
absolutePath: node.absolutePath,
getNode,
getNodes,
getNodesByType,
reporter,
cache,
pathPrefix,
options,
loadNodeContent,
actions,
createNodeId,
...helpers,
})

const mdxNode = {
id,
Expand All @@ -24,23 +47,23 @@ module.exports = async ({ id, node, content }) => {
content: content,
type: `Mdx`,
},
excerpt: frontmatter.excerpt,
exports: scopeExports,
rawBody: content,
frontmatter: {
title: ``, // always include a title
...frontmatter,
},
}

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
return { mdxNode, scopeIdentifiers, scopeImports }
}

module.exports = createMdxNode
37 changes: 23 additions & 14 deletions packages/gatsby-plugin-mdx/utils/gen-mdx.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ async function genMDX(
options,
getNode,
getNodes,
getNodesByType,
reporter,
cache,
pathPrefix,
Expand Down Expand Up @@ -114,6 +115,7 @@ export const _frontmatter = ${JSON.stringify(data)}`
// files,
getNode,
getNodes,
getNodesByType,
reporter,
cache,
pathPrefix,
Expand Down Expand Up @@ -194,8 +196,9 @@ ${code}`
module.exports = genMDX // Legacy API, drop in v3 in favor of named export
module.exports.genMDX = genMDX

async function findImports({
node,
async function findImportsExports({
rawInput,
absolutePath = null,
options,
getNode,
getNodes,
Expand All @@ -205,12 +208,12 @@ async function findImports({
pathPrefix,
...helpers
}) {
const { content } = grayMatter(node.rawBody)
const stuff = grayMatter(rawInput)
const { data: frontmatter, content } = stuff

const gatsbyRemarkPluginsAsremarkPlugins = await getSourcePluginsAsRemarkPlugins(
{
gatsbyRemarkPlugins: options.gatsbyRemarkPlugins,
mdxNode: node,
getNode,
getNodes,
getNodesByType,
Expand All @@ -226,18 +229,19 @@ async function findImports({
)

const compilerOptions = {
filepath: node.fileAbsolutePath,
filepath: absolutePath,
...options,
remarkPlugins: [
...options.remarkPlugins,
...gatsbyRemarkPluginsAsremarkPlugins,
],
}

const compiler = mdx.createCompiler(compilerOptions)

const fileOpts = { contents: content }
if (node.fileAbsolutePath) {
fileOpts.path = node.fileAbsolutePath
if (absolutePath) {
fileOpts.path = absolutePath
}

const mdast = await compiler.parse(fileOpts)
Expand All @@ -246,16 +250,19 @@ async function findImports({
// we don't need to dedupe the symbols here.
const identifiers = []
const imports = []
const exports = []

mdast.children.forEach(node => {
if (node.type !== `import`) return

const importCode = node.value
if (node.type === `import`) {
const importCode = node.value

imports.push(importCode)
imports.push(importCode)

const bindings = parseImportBindings(importCode)
identifiers.push(...bindings)
const bindings = parseImportBindings(importCode)
identifiers.push(...bindings)
} else if (node.type === `export`) {
exports.push(node.value)
}
})

if (!identifiers.includes(`React`)) {
Expand All @@ -264,9 +271,11 @@ async function findImports({
}

return {
frontmatter,
scopeImports: imports,
scopeExports: exports,
scopeIdentifiers: identifiers,
}
}

module.exports.findImports = findImports
module.exports.findImportsExports = findImportsExports

0 comments on commit 7299f00

Please sign in to comment.