Skip to content

Commit

Permalink
refactor: use lodash.get/opstics style for warning helper
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Dec 22, 2022
1 parent 27b3126 commit e6bc1a0
Showing 1 changed file with 38 additions and 46 deletions.
84 changes: 38 additions & 46 deletions packages/next/server/config.ts
Expand Up @@ -128,38 +128,25 @@ function setFontLoaderDefaults(config: NextConfigComplete) {

function warnOptionHasBeenMovedOutOfExperimental(
config: NextConfig,
key: keyof NextConfigComplete['compiler'],
configFileName: string,
isCompilerFeature: true
): NextConfig
function warnOptionHasBeenMovedOutOfExperimental(
config: NextConfig,
key: keyof NextConfigComplete,
configFileName: string,
isCompilerFeature: false
): NextConfig
function warnOptionHasBeenMovedOutOfExperimental(
config: NextConfig,
key: keyof NextConfigComplete['compiler'] | keyof NextConfigComplete,
configFileName: string,
isCompilerFeature: boolean
): NextConfig {
if (config.experimental && key in config.experimental) {
oldKey: string,
newKey: string,
configFileName: string
) {
if (config.experimental && oldKey in config.experimental) {
Log.warn(
`\`${key}\` has been moved out of \`experimental\`${
isCompilerFeature ? ' and into `compiler`' : ''
}. Please update your ${configFileName} file accordingly.`
`\`${oldKey}\` has been moved out of \`experimental\`` +
(newKey.includes('.') ? ` and into \`${newKey}\`` : '') +
`. Please update your ${configFileName} file accordingly.`
)
if (isCompilerFeature) {
config.compiler = config.compiler || {}
// TODO: remove "as" once TypeScript supports type narrowing in overloaded functions
// https://github.com/microsoft/TypeScript/issues/22609
config.compiler[key as keyof NextConfigComplete['compiler']] = (
config.experimental as any
)[key]
} else {
config[key] = (config.experimental as any)[key]

let current = config
const newKeys = newKey.split('.')
while (newKeys.length > 1) {
const key = newKeys.shift()!
current[key] = current[key] || {}
current = current[key]
}
current[newKeys.shift()!] = (config.experimental as any)[oldKey]
}

return config
Expand Down Expand Up @@ -581,30 +568,35 @@ function assignDefaults(dir: string, userConfig: { [key: string]: any }) {
}
}

warnOptionHasBeenMovedOutOfExperimental(result, 'relay', configFileName, true)
warnOptionHasBeenMovedOutOfExperimental(
result,
'relay',
'compiler.relay',
configFileName
)
warnOptionHasBeenMovedOutOfExperimental(
result,
'styledComponents',
configFileName,
true
'compiler.styledComponents',
configFileName
)
warnOptionHasBeenMovedOutOfExperimental(
result,
'emotion',
configFileName,
true
'compiler.emotion',
configFileName
)
warnOptionHasBeenMovedOutOfExperimental(
result,
'reactRemoveProperties',
configFileName,
true
'compiler.reactRemoveProperties',
configFileName
)
warnOptionHasBeenMovedOutOfExperimental(
result,
'removeConsole',
configFileName,
true
'compiler.removeConsole',
configFileName
)

if (result.experimental?.swcMinifyDebugOptions) {
Expand All @@ -623,26 +615,26 @@ function assignDefaults(dir: string, userConfig: { [key: string]: any }) {
warnOptionHasBeenMovedOutOfExperimental(
result,
'transpilePackages',
configFileName,
false
'transpilePackages',
configFileName
)
warnOptionHasBeenMovedOutOfExperimental(
result,
'allowMiddlewareResponseBody',
configFileName,
false
'allowMiddlewareResponseBody',
configFileName
)
warnOptionHasBeenMovedOutOfExperimental(
result,
'skipMiddlewareUrlNormalize',
configFileName,
false
'skipMiddlewareUrlNormalize',
configFileName
)
warnOptionHasBeenMovedOutOfExperimental(
result,
'skipTrailingSlashRedirect',
configFileName,
false
'skipTrailingSlashRedirect',
configFileName
)

if (
Expand Down

0 comments on commit e6bc1a0

Please sign in to comment.