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

fix(gatsby): pluginOptionsSchema in local TS plugin #37443

Merged
merged 4 commits into from
Jan 12, 2023
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
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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