Skip to content

Commit

Permalink
feat(gatsby-plugin-typescript): Add schema (#27361)
Browse files Browse the repository at this point in the history
* Add schema for gatsby-plugin-typescript

* Update packages/gatsby-plugin-typescript/src/gatsby-node.js

Co-authored-by: Matt Kane <matt@gatsbyjs.com>

* eronuous curly braces

* listen to experimental flag

* update load plugins snapshot

* update plugin test exepected result

* add unit tests for schema

Co-authored-by: Matt Kane <matt@gatsbyjs.com>
Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
  • Loading branch information
3 people authored Oct 14, 2020
1 parent 27ae3aa commit 072ef6c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const {
} = require(`../gatsby-node`)
const path = require(`path`)

const { testPluginOptionsSchema } = require(`gatsby-plugin-utils`)
const { pluginOptionsSchema } = require(`../gatsby-node`)

describe(`gatsby-plugin-typescript`, () => {
describe(`resolvableExtensions`, () => {
it(`returns the correct resolvable extensions`, () => {
Expand Down Expand Up @@ -69,4 +72,32 @@ describe(`gatsby-plugin-typescript`, () => {
expect(actions.setWebpackConfig).not.toHaveBeenCalled()
})
})

describe(`plugin schema`, () => {
it(`should provide meaningful errors when fields are invalid`, () => {
const expectedErrors = [
`"isTSX" must be a boolean`,
`"jsxPragma" must be a string`,
`"allExtensions" must be a boolean`,
]

const { errors } = testPluginOptionsSchema(pluginOptionsSchema, {
isTSX: `this should be a boolean`,
jsxPragma: 123,
allExtensions: `this should be a boolean`,
})

expect(errors).toEqual(expectedErrors)
})

it(`should validate the schema`, () => {
const { isValid } = testPluginOptionsSchema(pluginOptionsSchema, {
isTSX: true,
jsxPragma: `ReactFunction`,
allExtensions: false,
})

expect(isValid).toBe(true)
})
})
})
15 changes: 15 additions & 0 deletions packages/gatsby-plugin-typescript/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ function onCreateWebpackConfig({ actions, loaders }) {
})
}

if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) {
exports.pluginOptionsSchema = ({ Joi }) =>
Joi.object({
isTSX: Joi.boolean().description(`Enables jsx parsing.`).default(true),
jsxPragma: Joi.string()
.description(
`Replace the function used when compiling JSX expressions.`
)
.default(`React`),
allExtensions: Joi.boolean()
.description(`Indicates that every file should be parsed as TS or TSX.`)
.default(false),
})
}

exports.resolvableExtensions = resolvableExtensions
exports.onCreateBabelConfig = onCreateBabelConfig
exports.onCreateWebpackConfig = onCreateWebpackConfig
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ Array [
"id": "",
"name": "gatsby-plugin-typescript",
"nodeAPIs": Array [
"pluginOptionsSchema",
"resolvableExtensions",
"onCreateBabelConfig",
"onCreateWebpackConfig",
Expand Down Expand Up @@ -478,6 +479,7 @@ Array [
"id": "",
"name": "gatsby-plugin-typescript",
"nodeAPIs": Array [
"pluginOptionsSchema",
"resolvableExtensions",
"onCreateBabelConfig",
"onCreateWebpackConfig",
Expand Down Expand Up @@ -762,6 +764,7 @@ Array [
"id": "",
"name": "gatsby-plugin-typescript",
"nodeAPIs": Array [
"pluginOptionsSchema",
"resolvableExtensions",
"onCreateBabelConfig",
"onCreateWebpackConfig",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ describe(`Load plugins`, () => {
id: ``,
name: `gatsby-plugin-typescript`,
nodeAPIs: [
`pluginOptionsSchema`,
`resolvableExtensions`,
`onCreateBabelConfig`,
`onCreateWebpackConfig`,
Expand Down Expand Up @@ -162,6 +163,7 @@ describe(`Load plugins`, () => {
id: ``,
name: `gatsby-plugin-typescript`,
nodeAPIs: [
`pluginOptionsSchema`,
`resolvableExtensions`,
`onCreateBabelConfig`,
`onCreateWebpackConfig`,
Expand Down

0 comments on commit 072ef6c

Please sign in to comment.