Skip to content

Commit

Permalink
fix(gatsby): pluginOptionsSchema in local TS plugin (#37443)
Browse files Browse the repository at this point in the history
  • Loading branch information
LekoArts committed Jan 12, 2023
1 parent 5e7e8fa commit c57c060
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
@@ -1,11 +1,20 @@
import { GatsbyNode } from "gatsby"

export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"] = ({ actions }) => {
// This isn't strictly relevant for typegen but I'm lazy...
// By adding the schema here and re-using it inside createSchemaCustomization we can make sure that Gatsby correctly reads the pluginOptionsSchema from .ts files. If it wouldn't work the checkMePleaseKey would be undefined and the e2e test would fail
export const pluginOptionsSchema: GatsbyNode["pluginOptionsSchema"] = ({ Joi }) => {
return Joi.object({
checkMePleaseString: Joi.string().default(`hello`)
})
}

export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"] = ({ actions }, pluginOptions) => {
const { createTypes } = actions
const checkMePleaseKey = pluginOptions?.checkMePleaseString

createTypes(`
type CheckMePlease {
hello: String!
${checkMePleaseKey}: String!
}
`)
}
Expand Down
Expand Up @@ -30,9 +30,13 @@ jest.mock(`gatsby-cli/lib/reporter`, () => {
// After switching to import to support esm, point file path resolution to the real compiled JS files in dist instead.
jest.mock(`../../resolve-js-file-path`, () => {
return {
resolveJSFilepath: jest.fn(
({ filePath }) => `${filePath.replace(`src`, `dist`)}.js`
),
resolveJSFilepath: jest.fn(({ filePath }: { filePath: string }) => {
if (filePath.includes(`load-plugins/__tests__/fixtures`)) {
return filePath
}

return `${filePath.replace(`src`, `dist`)}.js`
}),
maybeAddFileProtocol: jest.fn(val => val),
}
})
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby/src/bootstrap/load-plugins/validate.ts
Expand Up @@ -20,6 +20,7 @@ import {
} from "./types"
import { resolvePlugin } from "./resolve-plugin"
import { preferDefault } from "../prefer-default"
import { importGatsbyPlugin } from "../../utils/import-gatsby-plugin"

interface IApi {
version?: string
Expand Down Expand Up @@ -213,7 +214,7 @@ async function validatePluginsOptions(
let gatsbyNode
try {
const resolvedPlugin = resolvePlugin(plugin, rootDir)
gatsbyNode = require(`${resolvedPlugin.resolve}/gatsby-node`)
gatsbyNode = await importGatsbyPlugin(resolvedPlugin, `gatsby-node`)
} catch (err) {
gatsbyNode = {}
}
Expand Down

0 comments on commit c57c060

Please sign in to comment.