Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gatsby-plugin-mdx not running plugins on findImports(...) #26191

Merged
merged 1 commit into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/gatsby-plugin-mdx/utils/__tests__/fixtures/test-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function createImportAST(name, filePath) {
return {
type: `import`,
value: `import ${name} from '${filePath}'`,
}
}

module.exports = ({ markdownAST }) => {
markdownAST.children = [
createImportAST(`* as testInjection`, `@private/test-inject`),
...markdownAST.children,
]
return markdownAST
}
59 changes: 59 additions & 0 deletions packages/gatsby-plugin-mdx/utils/__tests__/gen-mdx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const path = require(`path`)
const { findImports } = require(`../gen-mdx`)

const markdownContent = `---
title: Introduction
---

# Header 1
## Header 2
### Header 3

\`\`\`js
console.log('hello world')
\`\`\`
`

describe(`find imports`, () => {
it(`allows injecting imports via plugins`, async () => {
const results = await findImports({
node: {
id: `bbffffbb-bfff-bfff-bfff-dededededede`,
children: [],
parent: `bbffffbb-bfff-bfff-bfff-dededededebb`,
internal: {
content: markdownContent,
type: `Mdx`,
contentDigest: `6433c536b5eb922ad01f8d420bcabf6d`,
counter: 38,
owner: `gatsby-plugin-mdx`,
},
frontmatter: { title: `Introduction` },
excerpt: undefined,
exports: {},
rawBody: markdownContent,
fileAbsolutePath: `/path/to/getting-started.mdx`,
},
options: {
remarkPlugins: [],
gatsbyRemarkPlugins: [
{ resolve: path.join(__dirname, `fixtures`, `test-plugin`) },
],
},
getNode: () => null,
getNodes: () => [],
getNodesByType: () => [],
repoter: () => {},
cache: () => {},
pathPrefix: ``,
})

expect(results).toEqual({
scopeImports: [
`import * as testInjection from '@private/test-inject'`,
`import * as React from 'react'`,
],
scopeIdentifiers: [`testInjection`, `React`],
})
})
})
3 changes: 2 additions & 1 deletion packages/gatsby-plugin-mdx/utils/gen-mdx.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ async function findImports({
fileOpts.path = node.fileAbsolutePath
}

const mdast = await compiler.parse(fileOpts)
let mdast = await compiler.parse(fileOpts)
mdast = await compiler.run(mdast)

// Assuming valid code, identifiers must be unique (they are consts) so
// we don't need to dedupe the symbols here.
Expand Down