Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance experimental feature warning #37752

Merged
merged 7 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions packages/next/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,23 @@ export { DomainLocale, NextConfig, normalizeConfig } from './config-shared'

const targets = ['server', 'serverless', 'experimental-serverless-trace']

const experimentalWarning = execOnce(() => {
Log.warn(chalk.bold('You have enabled experimental feature(s).'))
Log.warn(
`Experimental features are not covered by semver, and may cause unexpected or broken application behavior. ` +
`Use them at your own risk.`
)
console.warn()
})
const experimentalWarning = execOnce(
(configFileName: string, features: string[]) => {
const s = features.length > 1 ? 's' : ''
Log.warn(
chalk.bold(
`You have enabled experimental feature${s} (${features.join(
', '
)}) in ${configFileName}.`
)
)
Log.warn(
`Experimental features are not covered by semver, and may cause unexpected or broken application behavior. ` +
`Use at your own risk.`
)
console.warn()
}
)

function assignDefaults(userConfig: { [key: string]: any }) {
const configFileName = userConfig.configFileName
Expand Down Expand Up @@ -74,7 +83,7 @@ function assignDefaults(userConfig: { [key: string]: any }) {
typeof value === 'object' &&
Object.keys(value).length > 0
) {
experimentalWarning()
experimentalWarning(configFileName, Object.keys(value))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that all values are read instead of the supported experimental features? It seems this would cause the following code to display the keys:

module.exports = {
  experimental: {
    randomFeatureThatDoesNotExist: true
  }
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! This was fixed in #37755

}

if (key === 'distDir') {
Expand Down
52 changes: 46 additions & 6 deletions test/integration/config-experimental-warning/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ import { nextBuild, File } from 'next-test-utils'

const appDir = join(__dirname, '..')
const configFile = new File(join(appDir, '/next.config.js'))
const configFileMjs = new File(join(appDir, '/next.config.mjs'))

describe('Promise in next config', () => {
afterAll(() => configFile.delete())
describe('Config Experimental Warning', () => {
afterEach(() => {
configFile.write('')
configFile.delete()
configFileMjs.write('')
configFileMjs.delete()
})

it('should not show warning with default config from function', async () => {
configFile.write(`
Expand All @@ -20,7 +26,7 @@ describe('Promise in next config', () => {
`)

const { stderr } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).not.toMatch(/experimental feature/)
expect(stderr).not.toMatch('You have enabled experimental feature')
})

it('should not show warning with config from object', async () => {
Expand All @@ -30,7 +36,7 @@ describe('Promise in next config', () => {
}
`)
const { stderr } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).not.toMatch(/experimental feature/)
expect(stderr).not.toMatch('You have enabled experimental feature')
})

it('should show warning with config from object with experimental', async () => {
Expand All @@ -43,7 +49,9 @@ describe('Promise in next config', () => {
}
`)
const { stderr } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).toMatch(/experimental feature/)
expect(stderr).toMatch(
'You have enabled experimental feature (something) in next.config.js.'
)
})

it('should show warning with config from function with experimental', async () => {
Expand All @@ -56,6 +64,38 @@ describe('Promise in next config', () => {
})
`)
const { stderr } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).toMatch(/experimental feature/)
expect(stderr).toMatch(
'You have enabled experimental feature (something) in next.config.js.'
)
})

it('should show warning with config from object with experimental and multiple keys', async () => {
configFile.write(`
module.exports = {
experimental: {
something: true,
another: 1,
}
}
`)
const { stderr } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).toMatch(
'You have enabled experimental features (something, another) in next.config.js.'
)
})

it('should show warning with next.config.mjs from object with experimental', async () => {
configFileMjs.write(`
const config = {
experimental: {
something: true,
}
}
export default config
`)
const { stderr } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).toMatch(
'You have enabled experimental feature (something) in next.config.mjs.'
)
})
})