Skip to content

Commit

Permalink
refactor: add warning helper for removed experimental option (#44213)
Browse files Browse the repository at this point in the history
Continues #44202, implements
#44202 (comment).

cc @styfle 

## 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
SukkaW committed Jan 3, 2023
1 parent 0549a46 commit c728575
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 82 deletions.
156 changes: 74 additions & 82 deletions packages/next/src/server/config.ts
Expand Up @@ -126,6 +126,32 @@ function setFontLoaderDefaults(config: NextConfigComplete) {
} catch {}
}

export function warnOptionHasBeenMovedOutOfExperimental(
config: NextConfig,
oldKey: string,
newKey: string,
configFileName: string
) {
if (config.experimental && oldKey in config.experimental) {
Log.warn(
`\`${oldKey}\` has been moved out of \`experimental\`` +
(newKey.includes('.') ? ` and into \`${newKey}\`` : '') +
`. Please update your ${configFileName} file accordingly.`
)

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
}

function assignDefaults(dir: string, userConfig: { [key: string]: any }) {
const configFileName = userConfig.configFileName
if (typeof userConfig.exportTrailingSlash !== 'undefined') {
Expand Down Expand Up @@ -542,55 +568,36 @@ function assignDefaults(dir: string, userConfig: { [key: string]: any }) {
}
}

if (result.experimental && 'relay' in (result.experimental as any)) {
Log.warn(
`\`relay\` has been moved out of \`experimental\` and into \`compiler\`. Please update your ${configFileName} file accordingly.`
)
result.compiler = result.compiler || {}
result.compiler.relay = (result.experimental as any).relay
}

if (
result.experimental &&
'styledComponents' in (result.experimental as any)
) {
Log.warn(
`\`styledComponents\` has been moved out of \`experimental\` and into \`compiler\`. Please update your ${configFileName} file accordingly.`
)
result.compiler = result.compiler || {}
result.compiler.styledComponents = (
result.experimental as any
).styledComponents
}

if (result.experimental && 'emotion' in (result.experimental as any)) {
Log.warn(
`\`emotion\` has been moved out of \`experimental\` and into \`compiler\`. Please update your ${configFileName} file accordingly.`
)
result.compiler = result.compiler || {}
result.compiler.emotion = (result.experimental as any).emotion
}

if (
result.experimental &&
'reactRemoveProperties' in (result.experimental as any)
) {
Log.warn(
`\`reactRemoveProperties\` has been moved out of \`experimental\` and into \`compiler\`. Please update your ${configFileName} file accordingly.`
)
result.compiler = result.compiler || {}
result.compiler.reactRemoveProperties = (
result.experimental as any
).reactRemoveProperties
}

if (result.experimental && 'removeConsole' in (result.experimental as any)) {
Log.warn(
`\`removeConsole\` has been moved out of \`experimental\` and into \`compiler\`. Please update your ${configFileName} file accordingly.`
)
result.compiler = result.compiler || {}
result.compiler.removeConsole = (result.experimental as any).removeConsole
}
warnOptionHasBeenMovedOutOfExperimental(
result,
'relay',
'compiler.relay',
configFileName
)
warnOptionHasBeenMovedOutOfExperimental(
result,
'styledComponents',
'compiler.styledComponents',
configFileName
)
warnOptionHasBeenMovedOutOfExperimental(
result,
'emotion',
'compiler.emotion',
configFileName
)
warnOptionHasBeenMovedOutOfExperimental(
result,
'reactRemoveProperties',
'compiler.reactRemoveProperties',
configFileName
)
warnOptionHasBeenMovedOutOfExperimental(
result,
'removeConsole',
'compiler.removeConsole',
configFileName
)

if (result.experimental?.swcMinifyDebugOptions) {
Log.warn(
Expand All @@ -605,39 +612,24 @@ function assignDefaults(dir: string, userConfig: { [key: string]: any }) {
result.output = 'standalone'
}

if (
result.experimental &&
'transpilePackages' in (result.experimental as any)
) {
Log.warn(
`\`transpilePackages\` has been moved out of \`experimental\`. Please update your ${configFileName} file accordingly.`
)
result.transpilePackages = (result.experimental as any).transpilePackages
}

if (
result.experimental &&
'skipMiddlewareUrlNormalize' in (result.experimental as any)
) {
Log.warn(
`\`skipMiddlewareUrlNormalize\` has been moved out of \`experimental\`. Please update your ${configFileName} file accordingly.`
)
result.skipMiddlewareUrlNormalize = (
result.experimental as any
).skipMiddlewareUrlNormalize
}

if (
result.experimental &&
'skipTrailingSlashRedirect' in (result.experimental as any)
) {
Log.warn(
`\`skipTrailingSlashRedirect\` has been moved out of \`experimental\`. Please update your ${configFileName} file accordingly.`
)
result.skipTrailingSlashRedirect = (
result.experimental as any
).skipTrailingSlashRedirect
}
warnOptionHasBeenMovedOutOfExperimental(
result,
'transpilePackages',
'transpilePackages',
configFileName
)
warnOptionHasBeenMovedOutOfExperimental(
result,
'skipMiddlewareUrlNormalize',
'skipMiddlewareUrlNormalize',
configFileName
)
warnOptionHasBeenMovedOutOfExperimental(
result,
'skipTrailingSlashRedirect',
'skipTrailingSlashRedirect',
configFileName
)

if (
result.experimental?.outputFileTracingRoot &&
Expand Down
98 changes: 98 additions & 0 deletions test/unit/warn-removed-experimental-config.test.ts
@@ -0,0 +1,98 @@
import { warnOptionHasBeenMovedOutOfExperimental } from 'next/dist/server/config'

describe('warnOptionHasBeenMovedOutOfExperimental', () => {
let spy: jest.SpyInstance
beforeAll(() => {
spy = jest.spyOn(console, 'warn').mockImplementation(() => {})
})

it('should not log warning message without experimental config', () => {
warnOptionHasBeenMovedOutOfExperimental(
{},
'skipTrailingSlashRedirect',
'skipTrailingSlashRedirect',
'next.config.js'
)

warnOptionHasBeenMovedOutOfExperimental(
{
experimental: {},
},
'skipTrailingSlashRedirect',
'skipTrailingSlashRedirect',
'next.config.js'
)

expect(spy).not.toBeCalled()
})

it('should log warning message with removed experimental config', () => {
warnOptionHasBeenMovedOutOfExperimental(
{
experimental: {
skipTrailingSlashRedirect: true,
},
} as any,
'skipTrailingSlashRedirect',
'skipTrailingSlashRedirect',
'next.config.js'
)

expect(spy).toHaveBeenCalledWith(
expect.stringContaining('warn'),
'`skipTrailingSlashRedirect` has been moved out of `experimental`. Please update your next.config.js file accordingly.'
)
})

it('should log warning message with removed experimental config - complex key', () => {
warnOptionHasBeenMovedOutOfExperimental(
{
experimental: {
relay: true,
},
} as any,
'relay',
'compiler.relay',
'next.config.js'
)

expect(spy).toHaveBeenCalledWith(
expect.stringContaining('warn'),
'`relay` has been moved out of `experimental` and into `compiler.relay`. Please update your next.config.js file accordingly.'
)
})

it('should update removed experimental config into new config', () => {
const config = {
experimental: {
skipTrailingSlashRedirect: true,
},
} as any
warnOptionHasBeenMovedOutOfExperimental(
config,
'skipTrailingSlashRedirect',
'skipTrailingSlashRedirect',
'next.config.js'
)

expect(config.experimental.skipTrailingSlashRedirect).toBe(true)
expect(config.skipTrailingSlashRedirect).toBe(true)
})

it('should update removed experimental config into new config - complex key', () => {
const config = {
experimental: {
foo: 'bar',
},
} as any
warnOptionHasBeenMovedOutOfExperimental(
config,
'foo',
'deep.prop.baz',
'next.config.js'
)

expect(config.experimental.foo).toBe('bar')
expect(config.deep.prop.baz).toBe('bar')
})
})

0 comments on commit c728575

Please sign in to comment.