70 %
Chris Biscardi

Using theme options in gatsby-config.js

Gatsby themes allow you to pass in options in the same way plugins get options.

gatsby-config.js
JS
module.exports = {
plugins: [
{
resolve: "gatsby-theme-awesome",
options: {
myThing: true,
blogPostFormat: "mdx"
}
}
]
};

Since gatsby-config has historically exported an object, as seen above, we needed to add a way to grab the options. This is why the function gatsby-config exists, which we can use in our theme to control which plugins are added (or anything else).

gatsby-theme-awesome/gatsby-config.js
JS
module.exports = options => {
const plugins = [];
if (options.blogPostFormat === "mdx") {
plugins.push({
resolve: `gatsby-plugin-mdx`
});
}
return {
plugins
};
};

Note that trying to use the function gatsby-config in the root of a site will fail because there's nowhere to get the options from.