Skip to content

Commit

Permalink
Revert "chore(gatsby-plugin-typescript): Remove obsolete `onCreateWeb…
Browse files Browse the repository at this point in the history
…packConfig` (#36814)" (#36869)
  • Loading branch information
LekoArts committed Oct 24, 2022
1 parent 58d5f2a commit 24c185e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
31 changes: 30 additions & 1 deletion packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const { resolvableExtensions, onCreateBabelConfig } = require(`../gatsby-node`)
const {
resolvableExtensions,
onCreateBabelConfig,
onCreateWebpackConfig,
} = require(`../gatsby-node`)
const path = require(`path`)

const { testPluginOptionsSchema } = require(`gatsby-plugin-utils`)
Expand Down Expand Up @@ -43,6 +47,31 @@ describe(`gatsby-plugin-typescript`, () => {
})
})

describe(`onCreateWebpackConfig`, () => {
it(`sets the correct webpack config`, () => {
const actions = { setWebpackConfig: jest.fn() }
const loaders = { js: jest.fn(() => {}) }
onCreateWebpackConfig({ actions, loaders })
expect(actions.setWebpackConfig).toHaveBeenCalledWith({
module: {
rules: [
{
test: /\.tsx?$/,
use: expect.toBeFunction(),
},
],
},
})
})

it(`does not set the webpack config if there isn't a js loader`, () => {
const actions = { setWebpackConfig: jest.fn() }
const loaders = { js: undefined }
onCreateWebpackConfig({ actions, loaders })
expect(actions.setWebpackConfig).not.toHaveBeenCalled()
})
})

describe(`plugin schema`, () => {
it(`should provide meaningful errors when fields are invalid`, async () => {
const expectedErrors = [
Expand Down
37 changes: 37 additions & 0 deletions packages/gatsby-plugin-typescript/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,42 @@ function onCreateBabelConfig({ actions }, options) {
})
}

function onCreateWebpackConfig({ actions, loaders }) {
if (typeof loaders?.js !== `function`) {
return
}

let doesUsedGatsbyVersionSupportResourceQuery
try {
const { version } = require(`gatsby/package.json`)
const [major, minor] = version.split(`.`).map(Number)
doesUsedGatsbyVersionSupportResourceQuery =
(major === 4 && minor >= 19) || major > 4
} catch {
doesUsedGatsbyVersionSupportResourceQuery = true
}

actions.setWebpackConfig({
module: {
rules: [
{
test: /\.tsx?$/,
use: ({ resourceQuery, issuer }) => [
loaders.js(
doesUsedGatsbyVersionSupportResourceQuery
? {
isPageTemplate: /async-requires/.test(issuer),
resourceQuery,
}
: undefined
),
],
},
],
},
})
}

exports.pluginOptionsSchema = ({ Joi }) =>
Joi.object({
isTSX: Joi.boolean().description(`Enables jsx parsing.`).default(false),
Expand Down Expand Up @@ -49,3 +85,4 @@ exports.pluginOptionsSchema = ({ Joi }) =>

exports.resolvableExtensions = resolvableExtensions
exports.onCreateBabelConfig = onCreateBabelConfig
exports.onCreateWebpackConfig = onCreateWebpackConfig
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ Array [
"pluginOptionsSchema",
"resolvableExtensions",
"onCreateBabelConfig",
"onCreateWebpackConfig",
],
"pluginOptions": Object {
"allExtensions": false,
Expand Down Expand Up @@ -634,6 +635,7 @@ Array [
"pluginOptionsSchema",
"resolvableExtensions",
"onCreateBabelConfig",
"onCreateWebpackConfig",
],
"pluginOptions": Object {
"allExtensions": false,
Expand Down Expand Up @@ -1015,6 +1017,7 @@ Array [
"pluginOptionsSchema",
"resolvableExtensions",
"onCreateBabelConfig",
"onCreateWebpackConfig",
],
"pluginOptions": Object {
"allExtensions": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ describe(`Load plugins`, () => {
`pluginOptionsSchema`,
`resolvableExtensions`,
`onCreateBabelConfig`,
`onCreateWebpackConfig`,
],
pluginOptions: {
allExtensions: false,
Expand Down

0 comments on commit 24c185e

Please sign in to comment.