Skip to content

Commit f87164f

Browse files
authoredNov 9, 2021
fix(gatsby): allow not defining entry for subplugins (#33900)
1 parent 9aef89c commit f87164f

File tree

16 files changed

+162
-86
lines changed

16 files changed

+162
-86
lines changed
 

‎packages/gatsby-plugin-mdx/gatsby-node.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ exports.pluginOptionsSchema = function ({ Joi }) {
8080
.unknown(true)
8181
.default({})
8282
.description(`Set the layout components for MDX source types`),
83-
gatsbyRemarkPlugins: Joi.subPlugins({ entry: `index` }).description(
83+
gatsbyRemarkPlugins: Joi.subPlugins().description(
8484
`Use Gatsby-specific remark plugins`
8585
),
8686
lessBabel: Joi.boolean()
@@ -122,8 +122,6 @@ exports.pluginOptionsSchema = function ({ Joi }) {
122122
),
123123
commonmark: Joi.boolean()
124124
.default(false)
125-
.description(
126-
"MDX will be parsed using CommonMark."
127-
),
125+
.description("MDX will be parsed using CommonMark."),
128126
})
129127
}

‎packages/gatsby-plugin-utils/src/test-plugin-options-schema.ts

+22-21
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,30 @@ export async function testPluginOptionsSchema(
1515
const pluginSchema = pluginSchemaFunction({
1616
Joi: Joi.extend(joi => {
1717
return {
18-
base: joi.any(),
1918
type: `subPlugins`,
20-
args: (): any =>
21-
joi
22-
.array()
23-
.items(
24-
joi
25-
.alternatives(
26-
joi.string(),
27-
joi.object({
28-
resolve: Joi.string(),
29-
options: Joi.object({}).unknown(true),
30-
})
31-
)
32-
.custom(value => {
33-
if (typeof value === `string`) {
34-
value = { resolve: value }
35-
}
36-
37-
return value
38-
}, `Gatsby specific subplugin validation`)
19+
base: joi
20+
.array()
21+
.items(
22+
joi.alternatives(
23+
joi.string(),
24+
joi.object({
25+
resolve: Joi.string(),
26+
options: Joi.object({}).unknown(true),
27+
})
3928
)
40-
.default([]),
29+
)
30+
.custom(
31+
arrayValue =>
32+
arrayValue.map(value => {
33+
if (typeof value === `string`) {
34+
value = { resolve: value }
35+
}
36+
37+
return value
38+
}),
39+
`Gatsby specific subplugin validation`
40+
)
41+
.default([]),
4142
}
4243
}),
4344
})

‎packages/gatsby-transformer-remark/src/gatsby-node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ exports.pluginOptionsSchema = function ({ Joi }) {
2020
),
2121
plugins:
2222
_CFLAGS_.GATSBY_MAJOR === `4`
23-
? Joi.subPlugins({ entry: `index` }).description(
23+
? Joi.subPlugins().description(
2424
`A list of remark plugins. See also: https://github.com/gatsbyjs/gatsby/tree/master/examples/using-remark for examples`
2525
)
2626
: Joi.array()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!node_modules

‎packages/gatsby/src/bootstrap/load-plugins/__tests__/fixtures/subplugins/node_modules/gatsby-plugin-child-no-main/index.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/gatsby/src/bootstrap/load-plugins/__tests__/fixtures/subplugins/node_modules/gatsby-plugin-child-no-main/package.json

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/gatsby/src/bootstrap/load-plugins/__tests__/fixtures/subplugins/node_modules/gatsby-plugin-child-with-main/lib/index.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/gatsby/src/bootstrap/load-plugins/__tests__/fixtures/subplugins/node_modules/gatsby-plugin-child-with-main/package.json

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/gatsby/src/bootstrap/load-plugins/__tests__/fixtures/subplugins/node_modules/gatsby-plugin-parent/gatsby-node.js

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/gatsby/src/bootstrap/load-plugins/__tests__/fixtures/subplugins/node_modules/gatsby-plugin-parent/package.json

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

‎packages/gatsby/src/bootstrap/load-plugins/__tests__/load-plugins.ts

+45
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,51 @@ describe(`Load plugins`, () => {
545545
expect(mockProcessExit).toHaveBeenCalledWith(1)
546546
})
547547

548+
it(`subplugins are resolved using "main" in package.json`, async () => {
549+
// in fixtures/subplugins/node_modules/gatsby-plugin-child-with-main/package.json
550+
// "main" field points to "lib/index.js"
551+
const plugins = await loadPlugins(
552+
{
553+
plugins: [
554+
{
555+
resolve: `gatsby-plugin-parent`,
556+
options: {
557+
testSubplugins: [
558+
`gatsby-plugin-child-no-main`,
559+
`gatsby-plugin-child-with-main`,
560+
],
561+
},
562+
},
563+
],
564+
},
565+
__dirname + `/fixtures/subplugins`
566+
)
567+
568+
// "module.exports" in entry files for subplugins contain just a string
569+
// for tests purposes (so we can assert "module" field on subplugins items)
570+
expect(
571+
plugins.find(plugin => plugin.name === `gatsby-plugin-parent`)
572+
?.pluginOptions?.testSubplugins
573+
).toEqual(
574+
expect.arrayContaining([
575+
expect.objectContaining({
576+
name: `gatsby-plugin-child-with-main`,
577+
module: `export-test-gatsby-plugin-child-with-main`,
578+
modulePath: expect.stringMatching(
579+
/gatsby-plugin-child-with-main[/\\]lib[/\\]index\.js$/
580+
),
581+
}),
582+
expect.objectContaining({
583+
name: `gatsby-plugin-child-no-main`,
584+
module: `export-test-gatsby-plugin-child-no-main`,
585+
modulePath: expect.stringMatching(
586+
/gatsby-plugin-child-no-main[/\\]index\.js$/
587+
),
588+
}),
589+
])
590+
)
591+
})
592+
548593
it(`validates local plugin schemas using require.resolve`, async () => {
549594
await loadPlugins(
550595
{

‎packages/gatsby/src/bootstrap/load-plugins/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const flattenPlugins = (plugins: Array<IPluginInfo>): Array<IPluginInfo> => {
5151
roots = roots.map(root => root[segment])
5252
}
5353
}
54+
roots = roots.flat()
5455

5556
roots.forEach(subPlugin => {
5657
flattened.push(subPlugin)

‎packages/gatsby/src/bootstrap/load-plugins/load.ts

+1
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ export function loadPlugins(
226226
roots = roots.map(root => root[segment])
227227
}
228228
}
229+
roots = roots.flat()
229230

230231
const processed = roots.map(processPlugin)
231232
_.set(plugin.options, pathToSwap, processed)

‎packages/gatsby/src/bootstrap/load-plugins/validate.ts

+68-60
Original file line numberDiff line numberDiff line change
@@ -208,67 +208,75 @@ async function validatePluginsOptions(
208208
)({
209209
Joi: Joi.extend(joi => {
210210
return {
211-
base: joi.any(),
212211
type: `subPlugins`,
213-
args: (_, args: any): any => {
214-
const entry = args?.entry ?? `index`
215-
216-
return joi
217-
.array()
218-
.items(
219-
joi
220-
.alternatives(
221-
joi.string(),
222-
joi.object({
223-
resolve: Joi.string(),
224-
options: Joi.object({}).unknown(true),
225-
})
226-
)
227-
.custom((value, helpers) => {
228-
if (typeof value === `string`) {
229-
value = { resolve: value }
230-
}
231-
232-
try {
233-
const resolvedPlugin = resolvePlugin(value, rootDir)
234-
const modulePath = require.resolve(
235-
`${resolvedPlugin.resolve}/${entry}`
236-
)
237-
value.modulePath = modulePath
238-
value.module = require(modulePath)
239-
240-
const normalizedPath = helpers.state.path
241-
.map((key, index) => {
242-
// if subplugin is part of an array - swap concrete index key with `[]`
243-
if (
244-
typeof key === `number` &&
245-
Array.isArray(
246-
helpers.state.ancestors[
247-
helpers.state.path.length - index - 1
248-
]
249-
)
250-
) {
251-
if (index !== helpers.state.path.length - 1) {
252-
throw new Error(
253-
`No support for arrays not at the end of path`
254-
)
255-
}
256-
return `[]`
257-
}
258-
259-
return key
260-
})
261-
.join(`.`)
262-
263-
subPluginPaths.add(normalizedPath)
264-
} catch (err) {
265-
console.log(err)
266-
}
267-
268-
return value
269-
}, `Gatsby specific subplugin validation`)
212+
base: joi
213+
.array()
214+
.items(
215+
joi.alternatives(
216+
joi.string(),
217+
joi.object({
218+
resolve: Joi.string(),
219+
options: Joi.object({}).unknown(true),
220+
})
270221
)
271-
.default([])
222+
)
223+
.custom((arrayValue, helpers) => {
224+
const entry = helpers.schema._flags.entry
225+
return arrayValue.map(value => {
226+
if (typeof value === `string`) {
227+
value = { resolve: value }
228+
}
229+
230+
try {
231+
const resolvedPlugin = resolvePlugin(value, rootDir)
232+
const modulePath = require.resolve(
233+
`${resolvedPlugin.resolve}${entry ? `/${entry}` : ``}`
234+
)
235+
value.modulePath = modulePath
236+
value.module = require(modulePath)
237+
238+
const normalizedPath = helpers.state.path
239+
.map((key, index) => {
240+
// if subplugin is part of an array - swap concrete index key with `[]`
241+
if (
242+
typeof key === `number` &&
243+
Array.isArray(
244+
helpers.state.ancestors[
245+
helpers.state.path.length - index - 1
246+
]
247+
)
248+
) {
249+
if (index !== helpers.state.path.length - 1) {
250+
throw new Error(
251+
`No support for arrays not at the end of path`
252+
)
253+
}
254+
return `[]`
255+
}
256+
257+
return key
258+
})
259+
.join(`.`)
260+
261+
subPluginPaths.add(normalizedPath)
262+
} catch (err) {
263+
console.log(err)
264+
}
265+
266+
return value
267+
})
268+
}, `Gatsby specific subplugin validation`)
269+
.default([]),
270+
args: (schema: any, args: any): any => {
271+
if (
272+
args?.entry &&
273+
schema &&
274+
typeof schema === `object` &&
275+
schema.$_setFlag
276+
) {
277+
return schema.$_setFlag(`entry`, args.entry, { clone: true })
278+
}
279+
return schema
272280
},
273281
}
274282
}),
@@ -305,7 +313,7 @@ async function validatePluginsOptions(
305313
)
306314
plugin.options.plugins = subPlugins
307315
if (subPlugins.length > 0) {
308-
subPluginPaths.add(`plugins.[]`)
316+
subPluginPaths.add(`plugins`)
309317
}
310318
errors += subErrors
311319
}

‎packages/gatsby/src/schema/graphql-engine/print-plugins.ts

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ function getSubpluginsByPluginPath(
176176
roots = roots.map(root => root[segment])
177177
}
178178
}
179+
roots = roots.flat()
179180

180181
return roots
181182
}

0 commit comments

Comments
 (0)
Please sign in to comment.