Skip to content

Commit

Permalink
fix(gatsby): allow not defining entry for subplugins (#33900) (#33909)
Browse files Browse the repository at this point in the history
Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
  • Loading branch information
GatsbyJS Bot and pieh committed Nov 9, 2021
1 parent 95a3524 commit 4975ee2
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 86 deletions.
6 changes: 2 additions & 4 deletions packages/gatsby-plugin-mdx/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ exports.pluginOptionsSchema = function ({ Joi }) {
.unknown(true)
.default({})
.description(`Set the layout components for MDX source types`),
gatsbyRemarkPlugins: Joi.subPlugins({ entry: `index` }).description(
gatsbyRemarkPlugins: Joi.subPlugins().description(
`Use Gatsby-specific remark plugins`
),
lessBabel: Joi.boolean()
Expand Down Expand Up @@ -122,8 +122,6 @@ exports.pluginOptionsSchema = function ({ Joi }) {
),
commonmark: Joi.boolean()
.default(false)
.description(
"MDX will be parsed using CommonMark."
),
.description("MDX will be parsed using CommonMark."),
})
}
43 changes: 22 additions & 21 deletions packages/gatsby-plugin-utils/src/test-plugin-options-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,30 @@ export async function testPluginOptionsSchema(
const pluginSchema = pluginSchemaFunction({
Joi: Joi.extend(joi => {
return {
base: joi.any(),
type: `subPlugins`,
args: (): any =>
joi
.array()
.items(
joi
.alternatives(
joi.string(),
joi.object({
resolve: Joi.string(),
options: Joi.object({}).unknown(true),
})
)
.custom(value => {
if (typeof value === `string`) {
value = { resolve: value }
}

return value
}, `Gatsby specific subplugin validation`)
base: joi
.array()
.items(
joi.alternatives(
joi.string(),
joi.object({
resolve: Joi.string(),
options: Joi.object({}).unknown(true),
})
)
.default([]),
)
.custom(
arrayValue =>
arrayValue.map(value => {
if (typeof value === `string`) {
value = { resolve: value }
}

return value
}),
`Gatsby specific subplugin validation`
)
.default([]),
}
}),
})
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-transformer-remark/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exports.pluginOptionsSchema = function ({ Joi }) {
),
plugins:
_CFLAGS_.GATSBY_MAJOR === `4`
? Joi.subPlugins({ entry: `index` }).description(
? Joi.subPlugins().description(
`A list of remark plugins. See also: https://github.com/gatsbyjs/gatsby/tree/master/examples/using-remark for examples`
)
: Joi.array()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!node_modules

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,51 @@ describe(`Load plugins`, () => {
expect(mockProcessExit).toHaveBeenCalledWith(1)
})

it(`subplugins are resolved using "main" in package.json`, async () => {
// in fixtures/subplugins/node_modules/gatsby-plugin-child-with-main/package.json
// "main" field points to "lib/index.js"
const plugins = await loadPlugins(
{
plugins: [
{
resolve: `gatsby-plugin-parent`,
options: {
testSubplugins: [
`gatsby-plugin-child-no-main`,
`gatsby-plugin-child-with-main`,
],
},
},
],
},
__dirname + `/fixtures/subplugins`
)

// "module.exports" in entry files for subplugins contain just a string
// for tests purposes (so we can assert "module" field on subplugins items)
expect(
plugins.find(plugin => plugin.name === `gatsby-plugin-parent`)
?.pluginOptions?.testSubplugins
).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: `gatsby-plugin-child-with-main`,
module: `export-test-gatsby-plugin-child-with-main`,
modulePath: expect.stringMatching(
/gatsby-plugin-child-with-main[/\\]lib[/\\]index\.js$/
),
}),
expect.objectContaining({
name: `gatsby-plugin-child-no-main`,
module: `export-test-gatsby-plugin-child-no-main`,
modulePath: expect.stringMatching(
/gatsby-plugin-child-no-main[/\\]index\.js$/
),
}),
])
)
})

it(`validates local plugin schemas using require.resolve`, async () => {
await loadPlugins(
{
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/bootstrap/load-plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const flattenPlugins = (plugins: Array<IPluginInfo>): Array<IPluginInfo> => {
roots = roots.map(root => root[segment])
}
}
roots = roots.flat()

roots.forEach(subPlugin => {
flattened.push(subPlugin)
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/bootstrap/load-plugins/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export function loadPlugins(
roots = roots.map(root => root[segment])
}
}
roots = roots.flat()

const processed = roots.map(processPlugin)
_.set(plugin.options, pathToSwap, processed)
Expand Down
128 changes: 68 additions & 60 deletions packages/gatsby/src/bootstrap/load-plugins/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,67 +208,75 @@ async function validatePluginsOptions(
)({
Joi: Joi.extend(joi => {
return {
base: joi.any(),
type: `subPlugins`,
args: (_, args: any): any => {
const entry = args?.entry ?? `index`

return joi
.array()
.items(
joi
.alternatives(
joi.string(),
joi.object({
resolve: Joi.string(),
options: Joi.object({}).unknown(true),
})
)
.custom((value, helpers) => {
if (typeof value === `string`) {
value = { resolve: value }
}

try {
const resolvedPlugin = resolvePlugin(value, rootDir)
const modulePath = require.resolve(
`${resolvedPlugin.resolve}/${entry}`
)
value.modulePath = modulePath
value.module = require(modulePath)

const normalizedPath = helpers.state.path
.map((key, index) => {
// if subplugin is part of an array - swap concrete index key with `[]`
if (
typeof key === `number` &&
Array.isArray(
helpers.state.ancestors[
helpers.state.path.length - index - 1
]
)
) {
if (index !== helpers.state.path.length - 1) {
throw new Error(
`No support for arrays not at the end of path`
)
}
return `[]`
}

return key
})
.join(`.`)

subPluginPaths.add(normalizedPath)
} catch (err) {
console.log(err)
}

return value
}, `Gatsby specific subplugin validation`)
base: joi
.array()
.items(
joi.alternatives(
joi.string(),
joi.object({
resolve: Joi.string(),
options: Joi.object({}).unknown(true),
})
)
.default([])
)
.custom((arrayValue, helpers) => {
const entry = helpers.schema._flags.entry
return arrayValue.map(value => {
if (typeof value === `string`) {
value = { resolve: value }
}

try {
const resolvedPlugin = resolvePlugin(value, rootDir)
const modulePath = require.resolve(
`${resolvedPlugin.resolve}${entry ? `/${entry}` : ``}`
)
value.modulePath = modulePath
value.module = require(modulePath)

const normalizedPath = helpers.state.path
.map((key, index) => {
// if subplugin is part of an array - swap concrete index key with `[]`
if (
typeof key === `number` &&
Array.isArray(
helpers.state.ancestors[
helpers.state.path.length - index - 1
]
)
) {
if (index !== helpers.state.path.length - 1) {
throw new Error(
`No support for arrays not at the end of path`
)
}
return `[]`
}

return key
})
.join(`.`)

subPluginPaths.add(normalizedPath)
} catch (err) {
console.log(err)
}

return value
})
}, `Gatsby specific subplugin validation`)
.default([]),
args: (schema: any, args: any): any => {
if (
args?.entry &&
schema &&
typeof schema === `object` &&
schema.$_setFlag
) {
return schema.$_setFlag(`entry`, args.entry, { clone: true })
}
return schema
},
}
}),
Expand Down Expand Up @@ -305,7 +313,7 @@ async function validatePluginsOptions(
)
plugin.options.plugins = subPlugins
if (subPlugins.length > 0) {
subPluginPaths.add(`plugins.[]`)
subPluginPaths.add(`plugins`)
}
errors += subErrors
}
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/schema/graphql-engine/print-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ function getSubpluginsByPluginPath(
roots = roots.map(root => root[segment])
}
}
roots = roots.flat()

return roots
}
Expand Down

0 comments on commit 4975ee2

Please sign in to comment.