From 1c87f30816bccf692a5656bbe2c54e6d9c298b9f Mon Sep 17 00:00:00 2001 From: LekoArts Date: Wed, 11 Jan 2023 15:03:34 +0100 Subject: [PATCH 1/4] fix --- packages/gatsby/src/bootstrap/load-plugins/validate.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/gatsby/src/bootstrap/load-plugins/validate.ts b/packages/gatsby/src/bootstrap/load-plugins/validate.ts index 2be49f41cd664..ac6fe91ad6160 100644 --- a/packages/gatsby/src/bootstrap/load-plugins/validate.ts +++ b/packages/gatsby/src/bootstrap/load-plugins/validate.ts @@ -213,7 +213,8 @@ async function validatePluginsOptions( let gatsbyNode try { const resolvedPlugin = resolvePlugin(plugin, rootDir) - gatsbyNode = require(`${resolvedPlugin.resolve}/gatsby-node`) + gatsbyNode = require(resolvedPlugin.resolvedCompiledGatsbyNode ?? + `${resolvedPlugin.resolve}/gatsby-node`) } catch (err) { gatsbyNode = {} } From 155f8401af9af22534d3244a02064e1d395d76dc Mon Sep 17 00:00:00 2001 From: LekoArts Date: Wed, 11 Jan 2023 15:14:45 +0100 Subject: [PATCH 2/4] add test --- .../plugins/gatsby-node-typegen/gatsby-node.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/e2e-tests/development-runtime/plugins/gatsby-node-typegen/gatsby-node.ts b/e2e-tests/development-runtime/plugins/gatsby-node-typegen/gatsby-node.ts index 053527f63e331..0f86897e582e4 100644 --- a/e2e-tests/development-runtime/plugins/gatsby-node-typegen/gatsby-node.ts +++ b/e2e-tests/development-runtime/plugins/gatsby-node-typegen/gatsby-node.ts @@ -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! } `) } From 7b42d6a5d80afd5704cc7f2ea09f067fa4a64fb4 Mon Sep 17 00:00:00 2001 From: LekoArts Date: Wed, 11 Jan 2023 16:18:57 +0100 Subject: [PATCH 3/4] use importGatsbyPlugin util --- packages/gatsby/src/bootstrap/load-plugins/validate.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/src/bootstrap/load-plugins/validate.ts b/packages/gatsby/src/bootstrap/load-plugins/validate.ts index ac6fe91ad6160..e91a963548bee 100644 --- a/packages/gatsby/src/bootstrap/load-plugins/validate.ts +++ b/packages/gatsby/src/bootstrap/load-plugins/validate.ts @@ -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 @@ -213,8 +214,7 @@ async function validatePluginsOptions( let gatsbyNode try { const resolvedPlugin = resolvePlugin(plugin, rootDir) - gatsbyNode = require(resolvedPlugin.resolvedCompiledGatsbyNode ?? - `${resolvedPlugin.resolve}/gatsby-node`) + gatsbyNode = await importGatsbyPlugin(resolvedPlugin, `gatsby-node`) } catch (err) { gatsbyNode = {} } From 1efd32e69a79cb378005312a0daa03180183ca71 Mon Sep 17 00:00:00 2001 From: LekoArts Date: Thu, 12 Jan 2023 09:00:46 +0100 Subject: [PATCH 4/4] make hacky test workaround even hackier --- .../bootstrap/load-plugins/__tests__/load-plugins.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/gatsby/src/bootstrap/load-plugins/__tests__/load-plugins.ts b/packages/gatsby/src/bootstrap/load-plugins/__tests__/load-plugins.ts index 535b8bcccfb7d..7cae03721ecc6 100644 --- a/packages/gatsby/src/bootstrap/load-plugins/__tests__/load-plugins.ts +++ b/packages/gatsby/src/bootstrap/load-plugins/__tests__/load-plugins.ts @@ -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), } })