Skip to content

Commit

Permalink
Revert "perf(gatsby-plugin-mdx): performance changes (#26004)"
Browse files Browse the repository at this point in the history
This reverts commit 9e02abe.
  • Loading branch information
pvdz committed Aug 3, 2020
1 parent 138ef07 commit b7309b1
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 99 deletions.
21 changes: 11 additions & 10 deletions packages/gatsby-plugin-mdx/gatsby/on-create-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const babel = require(`@babel/core`)
const { createContentDigest } = require(`gatsby-core-utils`)

const defaultOptions = require(`../utils/default-options`)
const createMDXNodeWithScope = require(`../utils/mdx-node-with-scope`)
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 Down Expand Up @@ -45,14 +46,18 @@ module.exports = async (

const content = await loadNodeContent(node)

const {
mdxNode,
scopeIdentifiers,
scopeImports,
} = await createMDXNodeWithScope({
const mdxNode = 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,
reporter,
Expand All @@ -64,10 +69,6 @@ module.exports = async (
createNodeId,
...helpers,
})

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

await cacheScope({
cache,
scopeIdentifiers,
Expand Down
12 changes: 3 additions & 9 deletions packages/gatsby-plugin-mdx/loaders/mdx-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const debugMore = require(`debug`)(`gatsby-plugin-mdx-info:mdx-loader`)

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

const DEFAULT_OPTIONS = {
Expand Down Expand Up @@ -94,7 +94,6 @@ module.exports = async function (content) {
const {
getNode: rawGetNode,
getNodes,
getNodesByType,
reporter,
cache,
pathPrefix,
Expand Down Expand Up @@ -122,15 +121,11 @@ module.exports = async function (content) {

let mdxNode
try {
// This node attempts to break the chicken-egg problem, where parsing mdx
// allows for custom plugins, which can receive a mdx node
;({ mdxNode } = await createMDXNodeWithScope({
mdxNode = await createMDXNode({
id: `fakeNodeIdMDXFileABugIfYouSeeThis`,
node: fileNode,
content,
options,
getNodesByType,
}))
})
} catch (e) {
return callback(e)
}
Expand Down Expand Up @@ -197,7 +192,6 @@ ${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
57 changes: 43 additions & 14 deletions packages/gatsby-plugin-mdx/utils/create-mdx-node.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,46 @@
const withDefaultOptions = require(`../utils/default-options`)
const { getNodesByType } = require(`gatsby/dist/redux/nodes.js`)
const createMdxNodeWithScope = require(`../utils/mdx-node-with-scope`)
const { createContentDigest } = require(`gatsby-core-utils`)

async function createMdxNodeLegacy({ id, node, content } = {}) {
const nodeWithScope = await createMdxNodeWithScope({
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 = {
id,
node,
content,
getNodesByType,
options: withDefaultOptions({ plugins: [] }),
})
return nodeWithScope.mdxNode
}
children: [],
parent: node.id,
internal: {
content: content,
type: `Mdx`,
},
}

mdxNode.frontmatter = {
title: ``, // always include a title
...frontmatter,
}

// This function is deprecated in favor of createMDXNodeWithScope and slated to be dropped in v3
module.exports = createMdxNodeLegacy
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
}
33 changes: 14 additions & 19 deletions packages/gatsby-plugin-mdx/utils/gen-mdx.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,8 @@ ${code}`
module.exports = genMDX // Legacy API, drop in v3 in favor of named export
module.exports.genMDX = genMDX

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

const gatsbyRemarkPluginsAsremarkPlugins = await getSourcePluginsAsRemarkPlugins(
{
Expand All @@ -228,7 +226,7 @@ async function findImportsExports({
)

const compilerOptions = {
filepath: absolutePath,
filepath: node.fileAbsolutePath,
...options,
remarkPlugins: [
...options.remarkPlugins,
Expand All @@ -238,8 +236,8 @@ async function findImportsExports({
const compiler = mdx.createCompiler(compilerOptions)

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

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

mdast.children.forEach(node => {
if (node.type === `import`) {
const importCode = node.value
imports.push(importCode)
const bindings = parseImportBindings(importCode)
identifiers.push(...bindings)
} else if (node.type === `export`) {
exports.push(node.value)
}
if (node.type !== `import`) return

const importCode = node.value

imports.push(importCode)

const bindings = parseImportBindings(importCode)
identifiers.push(...bindings)
})

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

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

module.exports.findImportsExports = findImportsExports
module.exports.findImports = findImports
45 changes: 0 additions & 45 deletions packages/gatsby-plugin-mdx/utils/mdx-node-with-scope.js

This file was deleted.

0 comments on commit b7309b1

Please sign in to comment.