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

feat(gatsby-transformer-remark): use subplugin annotation to use automatic subplugin module loading #33039

Merged
merged 1 commit into from
Sep 7, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ module.exports = (nodeApiArgs, pluginOptions = {}) => {
// to customize the GraphQL schema. This makes it possible for subplugins to
// modify types owned by `gatsby-transformer-remark`.
plugins.forEach(plugin => {
const resolvedPlugin = require(plugin.resolve)
const resolvedPlugin =
_CFLAGS_.GATSBY_MAJOR === `4` ? plugin.module : require(plugin.resolve)
Comment on lines +43 to +44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the old behavior will be incompatible with v4 and will be a breaking change? Did we consider keeping it compatible? I am fine with both but if it will be a breaking change we should probably add a note in #32860

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require(plugin.resolve) will continue to work in V4, but it will be undesirable to use it due to bundling problems with dynamic imports and using .subPlugins will be recomended. Not sure if there is nice way to "deprecate" old style here


if (typeof resolvedPlugin.createSchemaCustomization === `function`) {
resolvedPlugin.createSchemaCustomization(
nodeApiArgs,
Expand Down
13 changes: 10 additions & 3 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ module.exports = function remarkExtendNodeType(
}

for (const plugin of pluginOptions.plugins) {
const requiredPlugin = require(plugin.resolve)
const requiredPlugin =
_CFLAGS_.GATSBY_MAJOR === `4` ? plugin.module : require(plugin.resolve)
if (_.isFunction(requiredPlugin.setParserPlugins)) {
for (const parserPlugin of requiredPlugin.setParserPlugins(
plugin.pluginOptions
Expand Down Expand Up @@ -201,7 +202,10 @@ module.exports = function remarkExtendNodeType(
}
// Use a for loop to run remark plugins serially.
for (const plugin of pluginOptions.plugins) {
const requiredPlugin = require(plugin.resolve)
const requiredPlugin =
_CFLAGS_.GATSBY_MAJOR === `4`
? plugin.module
: require(plugin.resolve)
// Allow both exports = function(), and exports.default = function()
const defaultFunction = _.isFunction(requiredPlugin)
? requiredPlugin
Expand Down Expand Up @@ -242,7 +246,10 @@ module.exports = function remarkExtendNodeType(
//
// Use for loop to run remark plugins serially.
for (const plugin of pluginOptions.plugins) {
const requiredPlugin = require(plugin.resolve)
const requiredPlugin =
_CFLAGS_.GATSBY_MAJOR === `4`
? plugin.module
: require(plugin.resolve)
if (typeof requiredPlugin.mutateSource === `function`) {
await requiredPlugin.mutateSource(
{
Expand Down
28 changes: 17 additions & 11 deletions packages/gatsby-transformer-remark/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ exports.pluginOptionsSchema = function ({ Joi }) {
excerpt_separator: Joi.string().description(
`If your Markdown file contains HTML, excerpt will not return a value. In that case, you can set an excerpt_separator to an HTML tag. Edit your Markdown files to include that HTML tag after the text you’d like to appear in the excerpt.`
),
plugins: Joi.array()
.items(
Joi.string(),
Joi.object({
resolve: Joi.string(),
options: Joi.object({}).unknown(true),
})
)
.description(
`A list of remark plugins. See also: https://github.com/gatsbyjs/gatsby/tree/master/examples/using-remark for examples`
),
plugins:
_CFLAGS_.GATSBY_MAJOR === `4`
? Joi.subPlugins({ entry: `index` }).description(
`A list of remark plugins. See also: https://github.com/gatsbyjs/gatsby/tree/master/examples/using-remark for examples`
)
: Joi.array()
.items(
Joi.string(),
Joi.object({
resolve: Joi.string(),
options: Joi.object({}).unknown(true),
})
)
.default([])
.description(
`A list of remark plugins. See also: https://github.com/gatsbyjs/gatsby/tree/master/examples/using-remark for examples`
),
})
}