Unverified Commit 8fd9a391 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix writeConfigurationDefaults.ts to correctly suggest changes in monorepos (#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">
parent abbf3526
...@@ -195,55 +195,59 @@ export async function writeConfigurationDefaults( ...@@ -195,55 +195,59 @@ export async function writeConfigurationDefaults(
// Enable the Next.js typescript plugin. // Enable the Next.js typescript plugin.
if (isAppDirEnabled) { if (isAppDirEnabled) {
if (userTsConfig.compilerOptions) { // Check if the config or the resolved config has the plugin already.
// Check if the config or the resolved config has the plugin already. const plugins = [
const hasNextPlugin = [ ...(Array.isArray(tsOptions.plugins) ? tsOptions.plugins : []),
...(Array.isArray(tsOptions.plugins) ? tsOptions.plugins : []), ...(userTsConfig.compilerOptions &&
...(Array.isArray(userTsConfig.compilerOptions.plugins) Array.isArray(userTsConfig.compilerOptions.plugins)
? userTsConfig.compilerOptions.plugins ? userTsConfig.compilerOptions.plugins
: []), : []),
].some(({ name }: { name: string }) => name === 'next') ]
const hasNextPlugin = plugins.some(
({ name }: { name: string }) => name === 'next'
)
// If the TS config extends on another config, we can't add the `plugin` field // If the TS config extends on another config, we can't add the `plugin` field
// because that will override the parent config's plugins. // because that will override the parent config's plugins.
// Instead we have to show a message to the user to add the plugin manually. // Instead we have to show a message to the user to add the plugin manually.
if ( if (
!userTsConfig.compilerOptions ||
(plugins.length &&
!hasNextPlugin && !hasNextPlugin &&
'extends' in userTsConfig && 'extends' in rawConfig &&
!('plugins' in userTsConfig.compilerOptions) (!rawConfig.compilerOptions || !rawConfig.compilerOptions.plugins))
) { ) {
console.log( console.log(
`\nYour ${chalk.cyan( `\nYour ${chalk.cyan(
'tsconfig.json' '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( )} 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" }]' '"plugins": [{ "name": "next" }]'
)}\`) manually to your TypeScript configuration. Learn more: https://beta.nextjs.org/docs/configuring/typescript#using-the-typescript-plugin\n` )}\`) manually to your TypeScript configuration. Learn more: https://beta.nextjs.org/docs/configuring/typescript#using-the-typescript-plugin\n`
) )
} else if (!hasNextPlugin) { } else if (!hasNextPlugin) {
if (!('plugins' in userTsConfig.compilerOptions)) { if (!('plugins' in userTsConfig.compilerOptions)) {
userTsConfig.compilerOptions.plugins = [] userTsConfig.compilerOptions.plugins = []
}
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`, // If `strict` is set to `false` or `strictNullChecks` is set to `false`,
// then set `strictNullChecks` to `true`. // then set `strictNullChecks` to `true`.
if ( if (
hasPagesDir && hasPagesDir &&
isAppDirEnabled && isAppDirEnabled &&
!userTsConfig.compilerOptions.strict && !userTsConfig.compilerOptions.strict &&
!('strictNullChecks' in userTsConfig.compilerOptions) !('strictNullChecks' in userTsConfig.compilerOptions)
) { ) {
userTsConfig.compilerOptions.strictNullChecks = true userTsConfig.compilerOptions.strictNullChecks = true
suggestedActions.push( suggestedActions.push(
chalk.cyan('strictNullChecks') + ' was set to ' + chalk.bold(`true`) chalk.cyan('strictNullChecks') + ' was set to ' + chalk.bold(`true`)
) )
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment