Skip to content

Commit

Permalink
fix:(gatsby-plugin-mdx) Require a default export when needed (#21861)
Browse files Browse the repository at this point in the history
* Require default export when needed

* Fix typo
  • Loading branch information
jlkiri committed Mar 2, 2020
1 parent a87f1bd commit a8b6f48
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/gatsby-plugin-mdx/gatsby/source-nodes.js
Expand Up @@ -14,6 +14,7 @@ const getTableOfContents = require(`../utils/get-table-of-content`)
const defaultOptions = require(`../utils/default-options`)
const genMDX = require(`../utils/gen-mdx`)
const { mdxHTMLLoader: loader } = require(`../utils/render-html`)
const { interopDefault } = require(`../utils/interop-default`)

async function getCounts({ mdast }) {
let counts = {}
Expand Down Expand Up @@ -96,7 +97,7 @@ module.exports = (
*/
for (let plugin of options.gatsbyRemarkPlugins) {
debug(`requiring`, plugin.resolve)
const requiredPlugin = require(plugin.resolve)
const requiredPlugin = interopDefault(require(plugin.resolve))
debug(`required`, plugin)
if (_.isFunction(requiredPlugin.setParserPlugins)) {
for (let parserPlugin of requiredPlugin.setParserPlugins(
Expand Down
@@ -1,6 +1,7 @@
const visit = require(`unist-util-visit`)
const _ = require(`lodash`)
const debug = require(`debug`)(`get-source-plugins-as-remark-plugins`)
const { interopDefault } = require(`./interop-default`)

let fileNodes

Expand Down Expand Up @@ -43,7 +44,7 @@ module.exports = async function getSourcePluginsAsRemarkPlugins({
// return list of remarkPlugins
const userPlugins = gatsbyRemarkPlugins
.filter(plugin => {
if (_.isFunction(require(plugin.resolve))) {
if (_.isFunction(interopDefault(require(plugin.resolve)))) {
return true
} else {
debug(`userPlugins: filtering out`, plugin)
Expand All @@ -52,7 +53,7 @@ module.exports = async function getSourcePluginsAsRemarkPlugins({
})
.map(plugin => {
debug(`userPlugins: constructing remark plugin for `, plugin)
const requiredPlugin = require(plugin.resolve)
const requiredPlugin = interopDefault(require(plugin.resolve))
const wrappedPlugin = () =>
async function transformer(markdownAST) {
await requiredPlugin(
Expand Down
4 changes: 4 additions & 0 deletions packages/gatsby-plugin-mdx/utils/interop-default.js
@@ -0,0 +1,4 @@
const interopDefault = exp =>
exp && typeof exp === `object` && `default` in exp ? exp[`default`] : exp

exports.interopDefault = interopDefault

0 comments on commit a8b6f48

Please sign in to comment.