Skip to content

Commit

Permalink
fix(gatsby-plugin-typescript): Broader webpack support (#22003)
Browse files Browse the repository at this point in the history
  • Loading branch information
nemophrost committed Mar 6, 2020
1 parent 831e9e6 commit 4b93826
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
43 changes: 43 additions & 0 deletions packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,49 @@ describe(`gatsby-plugin-typescript`, () => {
})
})

it(`sets the correct webpack config with rule.loader shortcut`, () => {
const actions = { setWebpackConfig: jest.fn() }
const jsLoader = {}
const loaders = { js: jest.fn(() => jsLoader) }
const stage = `develop`
const webpackConfig = {
module: {
rules: [
{
enforce: `pre`,
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: `eslint-loader`,
},
],
},
}
const getConfig = jest.fn(() => webpackConfig)
onCreateWebpackConfig({ actions, getConfig, loaders, stage })
expect(actions.setWebpackConfig).toHaveBeenCalledWith({
module: {
rules: [
{
test: /\.tsx?$/,
use: jsLoader,
},
],
},
})
expect(actions.setWebpackConfig).toHaveBeenCalledWith({
module: {
rules: [
{
enforce: `pre`,
test: /\.tsx?$/,
exclude: /(node_modules|bower_components)/,
loader: `eslint-loader`,
},
],
},
})
})

it(`does not set the webpack config if there isn't a js loader`, () => {
const actions = { setWebpackConfig: jest.fn() }
const loaders = { js: jest.fn() }
Expand Down
6 changes: 5 additions & 1 deletion packages/gatsby-plugin-typescript/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ function onCreateWebpackConfig({
if (isTypescriptDepAvailable) {
const builtInEslintRule = getConfig().module.rules.find(rule => {
if (rule.enforce === `pre`) {
return rule.use.some(use => /eslint-loader/.test(use.loader))
if (rule.use) {
return rule.use.some(use => /eslint-loader/.test(use.loader))
} else {
return /eslint-loader/.test(rule.loader)
}
}
return false
})
Expand Down

0 comments on commit 4b93826

Please sign in to comment.