From ae5f581cb00c0f3be21b07a38780e65a041b3882 Mon Sep 17 00:00:00 2001 From: Lucas Terra Date: Mon, 3 Aug 2020 19:00:15 +0200 Subject: [PATCH] fix: gatsby-plugin-mdx's findImports not running plugins (#26191) --- .../utils/__tests__/fixtures/test-plugin.js | 14 +++++ .../utils/__tests__/gen-mdx.js | 59 +++++++++++++++++++ packages/gatsby-plugin-mdx/utils/gen-mdx.js | 3 +- 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 packages/gatsby-plugin-mdx/utils/__tests__/fixtures/test-plugin.js create mode 100644 packages/gatsby-plugin-mdx/utils/__tests__/gen-mdx.js diff --git a/packages/gatsby-plugin-mdx/utils/__tests__/fixtures/test-plugin.js b/packages/gatsby-plugin-mdx/utils/__tests__/fixtures/test-plugin.js new file mode 100644 index 0000000000000..e1c2e43fe7440 --- /dev/null +++ b/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 +} diff --git a/packages/gatsby-plugin-mdx/utils/__tests__/gen-mdx.js b/packages/gatsby-plugin-mdx/utils/__tests__/gen-mdx.js new file mode 100644 index 0000000000000..fe7341f21eaa9 --- /dev/null +++ b/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`], + }) + }) +}) diff --git a/packages/gatsby-plugin-mdx/utils/gen-mdx.js b/packages/gatsby-plugin-mdx/utils/gen-mdx.js index 3c2d9105122a9..2b6c0c8584f16 100644 --- a/packages/gatsby-plugin-mdx/utils/gen-mdx.js +++ b/packages/gatsby-plugin-mdx/utils/gen-mdx.js @@ -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.