Skip to content

Commit

Permalink
Fix writeConfigurationDefaults.ts to correctly suggest changes in mon…
Browse files Browse the repository at this point in the history
…orepos (#48668)

Closes #48653. It should either modify the TS config to add the Next.js
plugin, or output a log if it can't (to avoid overriding the base
config).

<img width="920" alt="CleanShot-2023-04-21-Klqel8BO@2x"
src="https://user-images.githubusercontent.com/3676859/233642532-5eed8d5d-d2c5-41e9-99bf-d0eb95685206.png">
  • Loading branch information
shuding authored Apr 21, 2023
1 parent abbf352 commit 8fd9a39
Showing 1 changed file with 49 additions and 45 deletions.
94 changes: 49 additions & 45 deletions packages/next/src/lib/typescript/writeConfigurationDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,55 +195,59 @@ 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')
// Check if the config or the resolved config has the plugin already.
const plugins = [
...(Array.isArray(tsOptions.plugins) ? tsOptions.plugins : []),
...(userTsConfig.compilerOptions &&
Array.isArray(userTsConfig.compilerOptions.plugins)
? userTsConfig.compilerOptions.plugins
: []),
]
const hasNextPlugin = 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 (
// 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 (
!userTsConfig.compilerOptions ||
(plugins.length &&
!hasNextPlugin &&
'extends' in userTsConfig &&
!('plugins' in userTsConfig.compilerOptions)
) {
console.log(
`\nYour ${chalk.cyan(
'tsconfig.json'
)} extends another configuration, which means we cannot add the Next.js TypeScript plugin automatically. To improve your development experience, we recommend adding the Next.js plugin (\`${chalk.cyan(
'"plugins": [{ "name": "next" }]'
)}\`) manually to your TypeScript configuration. Learn more: https://beta.nextjs.org/docs/configuring/typescript#using-the-typescript-plugin\n`
)
} else if (!hasNextPlugin) {
if (!('plugins' in userTsConfig.compilerOptions)) {
userTsConfig.compilerOptions.plugins = []
}
userTsConfig.compilerOptions.plugins.push({ name: 'next' })
suggestedActions.push(
chalk.cyan('plugins') +
' was updated to add ' +
chalk.bold(`{ name: 'next' }`)
)
'extends' in rawConfig &&
(!rawConfig.compilerOptions || !rawConfig.compilerOptions.plugins))
) {
console.log(
`\nYour ${chalk.cyan(
'tsconfig.json'
)} extends another configuration, which means we cannot add the Next.js TypeScript plugin automatically. To improve your development experience, we recommend adding the Next.js plugin (\`${chalk.cyan(
'"plugins": [{ "name": "next" }]'
)}\`) manually to your TypeScript configuration. Learn more: https://beta.nextjs.org/docs/configuring/typescript#using-the-typescript-plugin\n`
)
} else if (!hasNextPlugin) {
if (!('plugins' in userTsConfig.compilerOptions)) {
userTsConfig.compilerOptions.plugins = []
}
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`,
// then set `strictNullChecks` to `true`.
if (
hasPagesDir &&
isAppDirEnabled &&
!userTsConfig.compilerOptions.strict &&
!('strictNullChecks' in userTsConfig.compilerOptions)
) {
userTsConfig.compilerOptions.strictNullChecks = true
suggestedActions.push(
chalk.cyan('strictNullChecks') + ' was set to ' + chalk.bold(`true`)
)
}
// If `strict` is set to `false` or `strictNullChecks` is set to `false`,
// then set `strictNullChecks` to `true`.
if (
hasPagesDir &&
isAppDirEnabled &&
!userTsConfig.compilerOptions.strict &&
!('strictNullChecks' in userTsConfig.compilerOptions)
) {
userTsConfig.compilerOptions.strictNullChecks = true
suggestedActions.push(
chalk.cyan('strictNullChecks') + ' was set to ' + chalk.bold(`true`)
)
}
}

Expand Down

0 comments on commit 8fd9a39

Please sign in to comment.