-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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): fix subplugin validation #27616
Conversation
@@ -233,15 +233,20 @@ export function loadPlugins( | |||
|
|||
// TypeScript support by default! use the user-provided one if it exists | |||
const typescriptPlugin = (config.plugins || []).find( | |||
plugin => | |||
(plugin as IPluginRefObject).resolve === `gatsby-plugin-typescript` || | |||
plugin === `gatsby-plugin-typescript` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: removed the string case because plugin
cannot be a string at this point anymore now that we normalize them ahead of time. TypeScript helpfully pointed that out to me!
) | ||
|
||
if (typescriptPlugin === undefined) { | ||
plugins.push( | ||
processPlugin({ | ||
resolve: require.resolve(`gatsby-plugin-typescript`), | ||
options: { | ||
// TODO(@mxstbr): Do not hard-code these defaults but infer them from the | ||
// pluginOptionsSchema of gatsby-plugin-typescript |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't have to happen for now, but would be a small cleanup in the future should we ever change the typescript plugins' defaults!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tested it on gatsby-starter-blog and it wasn't validating sub plugins.
Co-authored-by: Ward Peeters <ward@coding-tech.com>
Co-authored-by: Ward Peeters <ward@coding-tech.com>
Thanks Ward! Updated 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! I don't like that we make gatsby-transformer etc as a dev dep of gatsby. Pretty sure it will bite us in the future
let optionsSchema = (gatsbyNode.pluginOptionsSchema as Exclude< | ||
GatsbyNode["pluginOptionsSchema"], | ||
undefined | ||
>)({ | ||
Joi, | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fun parts of typescript 😂
packages/gatsby/package.json
Outdated
"gatsby-transformer-remark": "2.8.46", | ||
"gatsby-remark-autolink-headers": "2.3.15", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have to do this? can't we mock these somehow? Feel free to rip out and do a follow up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We currently have to do this for the tests, but I'm not sure we actually even need to add them as we require.resolve("${plugin}/gatsby-node")
anyway. Let me test if it still works without them...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works without them, removed! We should definitely mock these at some point, as currently the Gatsby tests depend on the shape of packages/(gatsby-transfomer-remark|gatsby-remark-autolink-headers)/gatsby-node.js
, but this should be good enough for now 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
This (valid!) setup of gatsby-transformer-remark in gatsby-starter-blog:
shows this error:
However, the schema types this correctly, and in unit tests this configuration passes validation.
What our validation method does not account for is that Gatsby core adds more keys to the
plugins
array. When we try to validate it, it looks like this:Click to see
As you can see, we add a lot of keys to the subplugins. However, we do not care about validating those as we only want to validate what the user actually specified (imagine getting a validation error on something that isn't even in your gatsby-config!).
To fix this, we now validate before "flattening" the plugins, which adds all those keys!
[ch17523]