Unverified Commit 1fffbcac authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

Flag excess properties in Next.js config with TypeScript (#84069)

parent 7f29f174
...@@ -2459,7 +2459,7 @@ export default async function getBaseWebpackConfig( ...@@ -2459,7 +2459,7 @@ export default async function getBaseWebpackConfig(
assetPrefix: config.assetPrefix || '', assetPrefix: config.assetPrefix || '',
sassOptions: config.sassOptions, sassOptions: config.sassOptions,
productionBrowserSourceMaps: config.productionBrowserSourceMaps, productionBrowserSourceMaps: config.productionBrowserSourceMaps,
future: config.future, future: (config as any).future,
experimental: config.experimental, experimental: config.experimental,
disableStaticImages: config.images.disableStaticImages, disableStaticImages: config.images.disableStaticImages,
transpilePackages: config.transpilePackages, transpilePackages: config.transpilePackages,
...@@ -2636,8 +2636,8 @@ export default async function getBaseWebpackConfig( ...@@ -2636,8 +2636,8 @@ export default async function getBaseWebpackConfig(
} }
// Backwards compat with webpack-dev-middleware options object // Backwards compat with webpack-dev-middleware options object
if (typeof config.webpackDevMiddleware === 'function') { if (typeof (config as any).webpackDevMiddleware === 'function') {
const options = config.webpackDevMiddleware({ const options = (config as any).webpackDevMiddleware({
watchOptions: webpackConfig.watchOptions, watchOptions: webpackConfig.watchOptions,
}) })
if (options.watchOptions) { if (options.watchOptions) {
......
...@@ -39,9 +39,10 @@ export async function buildConfiguration( ...@@ -39,9 +39,10 @@ export async function buildConfiguration(
sassOptions: any sassOptions: any
productionBrowserSourceMaps: boolean productionBrowserSourceMaps: boolean
transpilePackages: NextConfigComplete['transpilePackages'] transpilePackages: NextConfigComplete['transpilePackages']
// @ts-expect-error TODO: remove any
future: NextConfigComplete['future'] future: NextConfigComplete['future']
experimental: NextConfigComplete['experimental'] experimental: NextConfigComplete['experimental']
disableStaticImages: NextConfigComplete['disableStaticImages'] disableStaticImages: NextConfigComplete['images']['disableStaticImages']
serverSourceMaps: NextConfigComplete['experimental']['serverSourceMaps'] serverSourceMaps: NextConfigComplete['experimental']['serverSourceMaps']
} }
): Promise<webpack.Configuration> { ): Promise<webpack.Configuration> {
......
...@@ -26,6 +26,7 @@ export type ConfigurationContext = { ...@@ -26,6 +26,7 @@ export type ConfigurationContext = {
transpilePackages: NextConfigComplete['transpilePackages'] transpilePackages: NextConfigComplete['transpilePackages']
// @ts-expect-error TODO: remove any
future: NextConfigComplete['future'] future: NextConfigComplete['future']
experimental: NextConfigComplete['experimental'] experimental: NextConfigComplete['experimental']
} }
......
...@@ -127,7 +127,7 @@ async function exportAppImpl( ...@@ -127,7 +127,7 @@ async function exportAppImpl(
) )
} }
const customRoutes = ['rewrites', 'redirects', 'headers'].filter( const customRoutes = (['rewrites', 'redirects', 'headers'] as const).filter(
(config) => typeof nextConfig[config] === 'function' (config) => typeof nextConfig[config] === 'function'
) )
......
...@@ -16,11 +16,10 @@ import { INFINITE_CACHE } from '../lib/constants' ...@@ -16,11 +16,10 @@ import { INFINITE_CACHE } from '../lib/constants'
import { isStableBuild } from '../shared/lib/canary-only' import { isStableBuild } from '../shared/lib/canary-only'
import type { FallbackRouteParam } from '../build/static-paths/types' import type { FallbackRouteParam } from '../build/static-paths/types'
export type NextConfigComplete = Required<NextConfig> & { export type NextConfigComplete = Required<Omit<NextConfig, 'configFile'>> & {
images: Required<ImageConfigComplete> images: Required<ImageConfigComplete>
typescript: TypeScriptConfig typescript: TypeScriptConfig
configOrigin?: string configFile: string | undefined
configFile?: string
configFileName: string configFileName: string
// override NextConfigComplete.experimental.htmlLimitedBots to string // override NextConfigComplete.experimental.htmlLimitedBots to string
// because it's not defined in NextConfigComplete.experimental // because it's not defined in NextConfigComplete.experimental
...@@ -876,7 +875,7 @@ export type ExportPathMap = { ...@@ -876,7 +875,7 @@ export type ExportPathMap = {
* *
* Read more: [Next.js Docs: `next.config.js`](https://nextjs.org/docs/app/api-reference/config/next-config-js) * Read more: [Next.js Docs: `next.config.js`](https://nextjs.org/docs/app/api-reference/config/next-config-js)
*/ */
export interface NextConfig extends Record<string, any> { export interface NextConfig {
allowedDevOrigins?: string[] allowedDevOrigins?: string[]
exportPathMap?: ( exportPathMap?: (
...@@ -1291,6 +1290,26 @@ export interface NextConfig extends Record<string, any> { ...@@ -1291,6 +1290,26 @@ export interface NextConfig extends Record<string, any> {
* /Mediapartners-Google|Slurp|DuckDuckBot|baiduspider|yandex|sogou|bitlybot|tumblr|vkShare|quora link preview|redditbot|ia_archiver|Bingbot|BingPreview|applebot|facebookexternalhit|facebookcatalog|Twitterbot|LinkedInBot|Slackbot|Discordbot|WhatsApp|SkypeUriPreview/i * /Mediapartners-Google|Slurp|DuckDuckBot|baiduspider|yandex|sogou|bitlybot|tumblr|vkShare|quora link preview|redditbot|ia_archiver|Bingbot|BingPreview|applebot|facebookexternalhit|facebookcatalog|Twitterbot|LinkedInBot|Slackbot|Discordbot|WhatsApp|SkypeUriPreview/i
*/ */
htmlLimitedBots?: RegExp htmlLimitedBots?: RegExp
/**
* @internal
*/
configFile?: string | undefined
/**
* @internal
*/
configOrigin?: string | undefined
/**
* @internal
*/
_originalRedirects?: any
/**
* @internal
*/
_originalRewrites?: any
} }
export const defaultConfig = Object.freeze({ export const defaultConfig = Object.freeze({
......
...@@ -76,8 +76,8 @@ export function warnOptionHasBeenDeprecated( ...@@ -76,8 +76,8 @@ export function warnOptionHasBeenDeprecated(
let found = true let found = true
const nestedPropertyKeys = nestedPropertyKey.split('.') const nestedPropertyKeys = nestedPropertyKey.split('.')
for (const key of nestedPropertyKeys) { for (const key of nestedPropertyKeys) {
if (current[key] !== undefined) { if ((current as any)[key] !== undefined) {
current = current[key] current = (current as any)[key]
} else { } else {
found = false found = false
break break
...@@ -167,10 +167,12 @@ export function warnOptionHasBeenMovedOutOfExperimental( ...@@ -167,10 +167,12 @@ export function warnOptionHasBeenMovedOutOfExperimental(
const newKeys = newKey.split('.') const newKeys = newKey.split('.')
while (newKeys.length > 1) { while (newKeys.length > 1) {
const key = newKeys.shift()! const key = newKeys.shift()!
current[key] = current[key] || {} ;(current as any)[key] = (current as any)[key] || {}
current = current[key] current = (current as any)[key]
} }
current[newKeys.shift()!] = (config.experimental as any)[oldExperimentalKey] ;(current as any)[newKeys.shift()!] = (config.experimental as any)[
oldExperimentalKey
]
} }
return config return config
...@@ -192,7 +194,7 @@ function warnCustomizedOption( ...@@ -192,7 +194,7 @@ function warnCustomizedOption(
if (!(seg in current)) { if (!(seg in current)) {
return return
} }
current = current[seg] current = (current as any)[seg]
} }
if (!silent && current !== defaultValue) { if (!silent && current !== defaultValue) {
...@@ -218,16 +220,16 @@ function assignDefaultsAndValidate( ...@@ -218,16 +220,16 @@ function assignDefaultsAndValidate(
phase: PHASE_TYPE phase: PHASE_TYPE
): NextConfigComplete { ): NextConfigComplete {
const configFileName = userConfig.configFileName const configFileName = userConfig.configFileName
if (typeof userConfig.exportTrailingSlash !== 'undefined') { if (typeof (userConfig as any).exportTrailingSlash !== 'undefined') {
if (!silent) { if (!silent) {
Log.warn( Log.warn(
`The "exportTrailingSlash" option has been renamed to "trailingSlash". Please update your ${configFileName}.` `The "exportTrailingSlash" option has been renamed to "trailingSlash". Please update your ${configFileName}.`
) )
} }
if (typeof userConfig.trailingSlash === 'undefined') { if (typeof userConfig.trailingSlash === 'undefined') {
userConfig.trailingSlash = userConfig.exportTrailingSlash userConfig.trailingSlash = (userConfig as any).exportTrailingSlash
} }
delete userConfig.exportTrailingSlash delete (userConfig as any).exportTrailingSlash
} }
// Handle migration of experimental.dynamicIO to experimental.cacheComponents // Handle migration of experimental.dynamicIO to experimental.cacheComponents
...@@ -245,7 +247,7 @@ function assignDefaultsAndValidate( ...@@ -245,7 +247,7 @@ function assignDefaultsAndValidate(
const config = Object.keys(userConfig).reduce<{ [key: string]: any }>( const config = Object.keys(userConfig).reduce<{ [key: string]: any }>(
(currentConfig, key) => { (currentConfig, key) => {
const value = userConfig[key] const value = (userConfig as any)[key]
if (value === undefined || value === null) { if (value === undefined || value === null) {
return currentConfig return currentConfig
...@@ -1266,7 +1268,6 @@ function getCacheKey( ...@@ -1266,7 +1268,6 @@ function getCacheKey(
return djb2Hash(keyData).toString(36) return djb2Hash(keyData).toString(36)
} }
export default async function loadConfig( export default async function loadConfig(
phase: PHASE_TYPE, phase: PHASE_TYPE,
dir: string, dir: string,
...@@ -1371,7 +1372,7 @@ export default async function loadConfig( ...@@ -1371,7 +1372,7 @@ export default async function loadConfig(
silent, silent,
configuredExperimentalFeatures, configuredExperimentalFeatures,
phase phase
) as NextConfigComplete, ),
phase, phase,
silent silent
) )
...@@ -1481,7 +1482,7 @@ export default async function loadConfig( ...@@ -1481,7 +1482,7 @@ export default async function loadConfig(
validateConfigSchema(userConfig, configFileName, curLog.warn) validateConfigSchema(userConfig, configFileName, curLog.warn)
} }
if (userConfig.target && userConfig.target !== 'server') { if ((userConfig as any).target && (userConfig as any).target !== 'server') {
throw new Error( throw new Error(
`The "target" property is no longer supported in ${configFileName}.\n` + `The "target" property is no longer supported in ${configFileName}.\n` +
'See more info here https://nextjs.org/docs/messages/deprecated-target-config' 'See more info here https://nextjs.org/docs/messages/deprecated-target-config'
...@@ -1538,7 +1539,7 @@ export default async function loadConfig( ...@@ -1538,7 +1539,7 @@ export default async function loadConfig(
silent, silent,
configuredExperimentalFeatures, configuredExperimentalFeatures,
phase phase
) as NextConfigComplete )
const finalConfig = await applyModifyConfig(completeConfig, phase, silent) const finalConfig = await applyModifyConfig(completeConfig, phase, silent)
......
...@@ -23,17 +23,17 @@ describe('config', () => { ...@@ -23,17 +23,17 @@ describe('config', () => {
}) })
it('Should get the configuration', async () => { it('Should get the configuration', async () => {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfig) const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfig)
expect(config.customConfig).toBe(true) expect((config as any).customConfig).toBe(true)
}) })
it('Should pass the phase correctly', async () => { it('Should pass the phase correctly', async () => {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfigFn) const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfigFn)
expect(config.phase).toBe(PHASE_DEVELOPMENT_SERVER) expect((config as any).phase).toBe(PHASE_DEVELOPMENT_SERVER)
}) })
it('Should pass the defaultConfig correctly', async () => { it('Should pass the defaultConfig correctly', async () => {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfigFn) const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfigFn)
expect(config.defaultConfig).toBeDefined() expect((config as any).defaultConfig).toBeDefined()
}) })
it('Should assign object defaults deeply to user config', async () => { it('Should assign object defaults deeply to user config', async () => {
...@@ -48,7 +48,7 @@ describe('config', () => { ...@@ -48,7 +48,7 @@ describe('config', () => {
customConfigKey: 'customConfigValue', customConfigKey: 'customConfigValue',
}, },
}) })
expect(config.customConfigKey).toBe('customConfigValue') expect((config as any).customConfigKey).toBe('customConfigValue')
}) })
it('Should assign object defaults deeply to customConfig', async () => { it('Should assign object defaults deeply to customConfig', async () => {
...@@ -58,7 +58,7 @@ describe('config', () => { ...@@ -58,7 +58,7 @@ describe('config', () => {
onDemandEntries: { custom: true }, onDemandEntries: { custom: true },
}, },
}) })
expect(config.customConfig).toBe(true) expect((config as any).customConfig).toBe(true)
expect(config.onDemandEntries.maxInactiveAge).toBeDefined() expect(config.onDemandEntries.maxInactiveAge).toBeDefined()
}) })
...@@ -68,8 +68,8 @@ describe('config', () => { ...@@ -68,8 +68,8 @@ describe('config', () => {
bogusSetting: { custom: true }, bogusSetting: { custom: true },
}, },
}) })
expect(config.bogusSetting).toBeDefined() expect((config as any).bogusSetting).toBeDefined()
expect(config.bogusSetting.custom).toBe(true) expect((config as any).bogusSetting.custom).toBe(true)
}) })
it('Should override defaults for arrays from user arrays', async () => { it('Should override defaults for arrays from user arrays', async () => {
...@@ -107,7 +107,7 @@ describe('config', () => { ...@@ -107,7 +107,7 @@ describe('config', () => {
PHASE_DEVELOPMENT_SERVER, PHASE_DEVELOPMENT_SERVER,
join(__dirname, '_resolvedata', 'js-ts-config') join(__dirname, '_resolvedata', 'js-ts-config')
) )
expect(config.__test__ext).toBe('js') expect((config as any).__test__ext).toBe('js')
}) })
it('Should not throw an error when next.config.ts is present', async () => { it('Should not throw an error when next.config.ts is present', async () => {
...@@ -115,7 +115,7 @@ describe('config', () => { ...@@ -115,7 +115,7 @@ describe('config', () => {
PHASE_DEVELOPMENT_SERVER, PHASE_DEVELOPMENT_SERVER,
join(__dirname, '_resolvedata', 'typescript-config') join(__dirname, '_resolvedata', 'typescript-config')
) )
expect(config.__test__ext).toBe('ts') expect((config as any).__test__ext).toBe('ts')
}) })
describe('outputFileTracingRoot and turbopack.root consistency', () => { describe('outputFileTracingRoot and turbopack.root consistency', () => {
......
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