Skip to content

Commit

Permalink
fix: gatsby-plugin-mdx's findImports not running plugins (#26191)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasterra committed Aug 3, 2020
1 parent 70cd249 commit ae5f581
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/gatsby-plugin-mdx/utils/__tests__/fixtures/test-plugin.js
@@ -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
@@ -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
Expand Up @@ -242,7 +242,8 @@ async function findImportsExports({
fileOpts.path = absolutePath
}

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

0 comments on commit ae5f581

Please sign in to comment.