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,22 +195,27 @@ export async function writeConfigurationDefaults( ...@@ -195,22 +195,27 @@ 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 hasNextPlugin = [ const plugins = [
...(Array.isArray(tsOptions.plugins) ? tsOptions.plugins : []), ...(Array.isArray(tsOptions.plugins) ? tsOptions.plugins : []),
...(Array.isArray(userTsConfig.compilerOptions.plugins) ...(userTsConfig.compilerOptions &&
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(
...@@ -245,7 +250,6 @@ export async function writeConfigurationDefaults( ...@@ -245,7 +250,6 @@ export async function writeConfigurationDefaults(
) )
} }
} }
}
if (!('exclude' in rawConfig)) { if (!('exclude' in rawConfig)) {
userTsConfig.exclude = ['node_modules'] userTsConfig.exclude = ['node_modules']
......
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