Skip to content

Commit

Permalink
Fix redundant TS plugin warning (#46769)
Browse files Browse the repository at this point in the history
When the project's TS config extends another base configuration, and the
Next.js TS plugin was configured there, we always output this message:


![CleanShot-2023-03-04-F5TcATto@2x](https://user-images.githubusercontent.com/3676859/222918715-6c2026f4-a96a-4c6b-9440-fc3d6075abb3.png)

This PR fixes that by checking the resolved TS options which contains
plugins from parent configurations too.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)

---------
  • Loading branch information
shuding authored Mar 5, 2023
1 parent 745efd2 commit 139e573
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions packages/next/src/lib/typescript/writeConfigurationDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,19 @@ export async function writeConfigurationDefaults(
// Enable the Next.js typescript plugin.
if (isAppDirEnabled) {
if (userTsConfig.compilerOptions) {
// Check if the config or the resolved config has the plugin already.
const hasNextPlugin = [
...(Array.isArray(tsOptions.plugins) ? tsOptions.plugins : []),
...(Array.isArray(userTsConfig.compilerOptions.plugins)
? userTsConfig.compilerOptions.plugins
: []),
].some(({ name }: { name: string }) => name === 'next')

// If the TS config extends on another config, we can't add the `plugin` field
// because that will override the parent config's plugins.
// Instead we have to show a message to the user to add the plugin manually.
if (
!hasNextPlugin &&
'extends' in userTsConfig &&
!('plugins' in userTsConfig.compilerOptions)
) {
Expand All @@ -210,22 +219,16 @@ export async function writeConfigurationDefaults(
'"plugins": [{ "name": "next" }]'
)}\`) manually to your TypeScript configuration. Learn more: https://beta.nextjs.org/docs/configuring/typescript#using-the-typescript-plugin\n`
)
} else {
} else if (!hasNextPlugin) {
if (!('plugins' in userTsConfig.compilerOptions)) {
userTsConfig.compilerOptions.plugins = []
}
if (
!userTsConfig.compilerOptions.plugins.some(
(plugin: { name: string }) => plugin.name === 'next'
)
) {
userTsConfig.compilerOptions.plugins.push({ name: 'next' })
suggestedActions.push(
chalk.cyan('plugins') +
' was updated to add ' +
chalk.bold(`{ name: 'next' }`)
)
}
userTsConfig.compilerOptions.plugins.push({ name: 'next' })
suggestedActions.push(
chalk.cyan('plugins') +
' was updated to add ' +
chalk.bold(`{ name: 'next' }`)
)
}

// If `strict` is set to `false` or `strictNullChecks` is set to `false`,
Expand Down

0 comments on commit 139e573

Please sign in to comment.