Unverified Commit 9c93c054 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add some missing fields to the NextConfig type (#27126)

This adds some missing fields to the `NextConfig` type we expose under `next` and also adds a `NextConfigComplete` internal type that allows us to not treat all fields as optional like you would when using the type in `next.config.js`. 

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `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`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes
parent 3b978692
...@@ -9,7 +9,7 @@ import { warn } from './output/log' ...@@ -9,7 +9,7 @@ import { warn } from './output/log'
import { ClientPagesLoaderOptions } from './webpack/loaders/next-client-pages-loader' import { ClientPagesLoaderOptions } from './webpack/loaders/next-client-pages-loader'
import { ServerlessLoaderQuery } from './webpack/loaders/next-serverless-loader' import { ServerlessLoaderQuery } from './webpack/loaders/next-serverless-loader'
import { LoadedEnvFiles } from '@next/env' import { LoadedEnvFiles } from '@next/env'
import { NextConfig } from '../server/config' import { NextConfigComplete } from '../server/config-shared'
type PagesMapping = { type PagesMapping = {
[page: string]: string [page: string]: string
...@@ -72,7 +72,7 @@ export function createEntrypoints( ...@@ -72,7 +72,7 @@ export function createEntrypoints(
target: 'server' | 'serverless' | 'experimental-serverless-trace', target: 'server' | 'serverless' | 'experimental-serverless-trace',
buildId: string, buildId: string,
previewMode: __ApiPreviewProps, previewMode: __ApiPreviewProps,
config: NextConfig, config: NextConfigComplete,
loadedEnvFiles: LoadedEnvFiles loadedEnvFiles: LoadedEnvFiles
): Entrypoints { ): Entrypoints {
const client: WebpackEntrypoints = {} const client: WebpackEntrypoints = {}
...@@ -90,9 +90,9 @@ export function createEntrypoints( ...@@ -90,9 +90,9 @@ export function createEntrypoints(
distDir: DOT_NEXT_ALIAS, distDir: DOT_NEXT_ALIAS,
buildId, buildId,
assetPrefix: config.assetPrefix, assetPrefix: config.assetPrefix,
generateEtags: config.generateEtags, generateEtags: config.generateEtags ? 'true' : '',
poweredByHeader: config.poweredByHeader, poweredByHeader: config.poweredByHeader,
canonicalBase: config.amp.canonicalBase, canonicalBase: config.amp.canonicalBase || '',
basePath: config.basePath, basePath: config.basePath,
runtimeConfig: hasRuntimeConfig runtimeConfig: hasRuntimeConfig
? JSON.stringify({ ? JSON.stringify({
......
...@@ -52,10 +52,7 @@ import { ...@@ -52,10 +52,7 @@ import {
isDynamicRoute, isDynamicRoute,
} from '../shared/lib/router/utils' } from '../shared/lib/router/utils'
import { __ApiPreviewProps } from '../server/api-utils' import { __ApiPreviewProps } from '../server/api-utils'
import loadConfig, { import loadConfig, { isTargetLikeServerless } from '../server/config'
isTargetLikeServerless,
NextConfig,
} from '../server/config'
import { BuildManifest } from '../server/get-page-files' import { BuildManifest } from '../server/get-page-files'
import '../server/node-polyfill-fetch' import '../server/node-polyfill-fetch'
import { normalizePagePath } from '../server/normalize-page-path' import { normalizePagePath } from '../server/normalize-page-path'
...@@ -91,6 +88,7 @@ import { PagesManifest } from './webpack/plugins/pages-manifest-plugin' ...@@ -91,6 +88,7 @@ import { PagesManifest } from './webpack/plugins/pages-manifest-plugin'
import { writeBuildId } from './write-build-id' import { writeBuildId } from './write-build-id'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path' import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { isWebpack5 } from 'next/dist/compiled/webpack/webpack' import { isWebpack5 } from 'next/dist/compiled/webpack/webpack'
import { NextConfigComplete } from '../server/config-shared'
const staticCheckWorker = require.resolve('./utils') const staticCheckWorker = require.resolve('./utils')
...@@ -130,7 +128,7 @@ export default async function build( ...@@ -130,7 +128,7 @@ export default async function build(
.traceChild('load-dotenv') .traceChild('load-dotenv')
.traceFn(() => loadEnvConfig(dir, false, Log)) .traceFn(() => loadEnvConfig(dir, false, Log))
const config: NextConfig = await nextBuildSpan const config: NextConfigComplete = await nextBuildSpan
.traceChild('load-next-config') .traceChild('load-next-config')
.traceAsyncFn(() => loadConfig(PHASE_PRODUCTION_BUILD, dir, conf)) .traceAsyncFn(() => loadConfig(PHASE_PRODUCTION_BUILD, dir, conf))
const { target } = config const { target } = config
...@@ -1527,7 +1525,7 @@ export default async function build( ...@@ -1527,7 +1525,7 @@ export default async function build(
const images = { ...config.images } const images = { ...config.images }
const { deviceSizes, imageSizes } = images const { deviceSizes, imageSizes } = images
images.sizes = [...deviceSizes, ...imageSizes] ;(images as any).sizes = [...deviceSizes, ...imageSizes]
await promises.writeFile( await promises.writeFile(
path.join(distDir, IMAGES_MANIFEST), path.join(distDir, IMAGES_MANIFEST),
......
...@@ -27,7 +27,7 @@ import { ...@@ -27,7 +27,7 @@ import {
SERVER_DIRECTORY, SERVER_DIRECTORY,
} from '../shared/lib/constants' } from '../shared/lib/constants'
import { execOnce } from '../shared/lib/utils' import { execOnce } from '../shared/lib/utils'
import { NextConfig } from '../server/config' import { NextConfigComplete } from '../server/config-shared'
import { findPageFile } from '../server/lib/find-page-file' import { findPageFile } from '../server/lib/find-page-file'
import { WebpackEntrypoints } from './entries' import { WebpackEntrypoints } from './entries'
import * as Log from './output/log' import * as Log from './output/log'
...@@ -228,7 +228,7 @@ export default async function getBaseWebpackConfig( ...@@ -228,7 +228,7 @@ export default async function getBaseWebpackConfig(
isDevFallback = false, isDevFallback = false,
}: { }: {
buildId: string buildId: string
config: NextConfig config: NextConfigComplete
dev?: boolean dev?: boolean
isServer?: boolean isServer?: boolean
pagesDir: string pagesDir: string
......
...@@ -29,10 +29,8 @@ import { ...@@ -29,10 +29,8 @@ import {
SERVERLESS_DIRECTORY, SERVERLESS_DIRECTORY,
SERVER_DIRECTORY, SERVER_DIRECTORY,
} from '../shared/lib/constants' } from '../shared/lib/constants'
import loadConfig, { import loadConfig, { isTargetLikeServerless } from '../server/config'
isTargetLikeServerless, import { NextConfigComplete } from '../server/config-shared'
NextConfig,
} from '../server/config'
import { eventCliSession } from '../telemetry/events' import { eventCliSession } from '../telemetry/events'
import { hasNextSupport } from '../telemetry/ci-info' import { hasNextSupport } from '../telemetry/ci-info'
import { Telemetry } from '../telemetry/storage' import { Telemetry } from '../telemetry/storage'
...@@ -136,7 +134,7 @@ interface ExportOptions { ...@@ -136,7 +134,7 @@ interface ExportOptions {
export default async function exportApp( export default async function exportApp(
dir: string, dir: string,
options: ExportOptions, options: ExportOptions,
configuration?: NextConfig configuration?: NextConfigComplete
): Promise<void> { ): Promise<void> {
const nextExportSpan = trace('next-export') const nextExportSpan = trace('next-export')
......
import os from 'os' import os from 'os'
import { Header, Redirect, Rewrite } from '../lib/load-custom-routes' import { Header, Redirect, Rewrite } from '../lib/load-custom-routes'
import { imageConfigDefault } from './image-config' import { ImageConfig, imageConfigDefault } from './image-config'
export type DomainLocales = Array<{ export type DomainLocales = Array<{
http?: true http?: true
...@@ -9,8 +9,13 @@ export type DomainLocales = Array<{ ...@@ -9,8 +9,13 @@ export type DomainLocales = Array<{
defaultLocale: string defaultLocale: string
}> }>
type NoOptionals<T> = {
[P in keyof T]-?: T[P]
}
export type NextConfigComplete = NoOptionals<NextConfig>
export type NextConfig = { [key: string]: any } & { export type NextConfig = { [key: string]: any } & {
cleanDistDir?: boolean
i18n?: { i18n?: {
locales: string[] locales: string[]
defaultLocale: string defaultLocale: string
...@@ -28,10 +33,36 @@ export type NextConfig = { [key: string]: any } & { ...@@ -28,10 +33,36 @@ export type NextConfig = { [key: string]: any } & {
} }
> >
redirects?: () => Promise<Redirect[]> redirects?: () => Promise<Redirect[]>
trailingSlash?: boolean
webpack5?: false webpack5?: false
excludeDefaultMomentLocales?: boolean excludeDefaultMomentLocales?: boolean
trailingSlash?: boolean
env?: { [key: string]: string }
distDir?: string
cleanDistDir?: boolean
assetPrefix?: string
useFileSystemPublicRoutes?: boolean
generateBuildId: () => string | null
generateEtags?: boolean
pageExtensions?: string[]
compress?: boolean
images?: ImageConfig
devIndicators?: {
buildActivity?: boolean
}
onDemandEntries?: {
maxInactiveAge?: number
pagesBufferLength?: number
}
amp?: {
canonicalBase?: string
}
basePath?: string
sassOptions?: { [key: string]: any }
productionBrowserSourceMaps?: boolean
optimizeFonts?: boolean
future: { future: {
/** /**
* @deprecated this options was moved to the top level * @deprecated this options was moved to the top level
...@@ -67,7 +98,7 @@ export type NextConfig = { [key: string]: any } & { ...@@ -67,7 +98,7 @@ export type NextConfig = { [key: string]: any } & {
} }
export const defaultConfig: NextConfig = { export const defaultConfig: NextConfig = {
env: [], env: {},
webpack: null, webpack: null,
webpackDevMiddleware: null, webpackDevMiddleware: null,
distDir: '.next', distDir: '.next',
...@@ -99,6 +130,12 @@ export const defaultConfig: NextConfig = { ...@@ -99,6 +130,12 @@ export const defaultConfig: NextConfig = {
i18n: null, i18n: null,
productionBrowserSourceMaps: false, productionBrowserSourceMaps: false,
optimizeFonts: true, optimizeFonts: true,
webpack5:
Number(process.env.NEXT_PRIVATE_TEST_WEBPACK4_MODE) > 0 ? false : undefined,
excludeDefaultMomentLocales: true,
serverRuntimeConfig: {},
publicRuntimeConfig: {},
reactStrictMode: false,
experimental: { experimental: {
cpus: Math.max( cpus: Math.max(
1, 1,
...@@ -121,15 +158,9 @@ export const defaultConfig: NextConfig = { ...@@ -121,15 +158,9 @@ export const defaultConfig: NextConfig = {
craCompat: false, craCompat: false,
esmExternals: false, esmExternals: false,
}, },
webpack5:
Number(process.env.NEXT_PRIVATE_TEST_WEBPACK4_MODE) > 0 ? false : undefined,
excludeDefaultMomentLocales: true,
future: { future: {
strictPostcssConfiguration: false, strictPostcssConfiguration: false,
}, },
serverRuntimeConfig: {},
publicRuntimeConfig: {},
reactStrictMode: false,
} }
export function normalizeConfig(phase: string, config: any) { export function normalizeConfig(phase: string, config: any) {
......
...@@ -5,7 +5,11 @@ import * as Log from '../build/output/log' ...@@ -5,7 +5,11 @@ import * as Log from '../build/output/log'
import { hasNextSupport } from '../telemetry/ci-info' import { hasNextSupport } from '../telemetry/ci-info'
import { CONFIG_FILE, PHASE_DEVELOPMENT_SERVER } from '../shared/lib/constants' import { CONFIG_FILE, PHASE_DEVELOPMENT_SERVER } from '../shared/lib/constants'
import { execOnce } from '../shared/lib/utils' import { execOnce } from '../shared/lib/utils'
import { defaultConfig, normalizeConfig } from './config-shared' import {
defaultConfig,
NextConfigComplete,
normalizeConfig,
} from './config-shared'
import { loadWebpackHook } from './config-utils' import { loadWebpackHook } from './config-utils'
import { ImageConfig, imageConfigDefault, VALID_LOADERS } from './image-config' import { ImageConfig, imageConfigDefault, VALID_LOADERS } from './image-config'
import { loadEnvConfig } from '@next/env' import { loadEnvConfig } from '@next/env'
...@@ -168,7 +172,7 @@ function assignDefaults(userConfig: { [key: string]: any }) { ...@@ -168,7 +172,7 @@ function assignDefaults(userConfig: { [key: string]: any }) {
result.assetPrefix = result.basePath result.assetPrefix = result.basePath
} }
if (result.amp.canonicalBase === '') { if (result.amp?.canonicalBase === '') {
result.amp.canonicalBase = result.basePath result.amp.canonicalBase = result.basePath
} }
} }
...@@ -410,12 +414,15 @@ export default async function loadConfig( ...@@ -410,12 +414,15 @@ export default async function loadConfig(
phase: string, phase: string,
dir: string, dir: string,
customConfig?: object | null customConfig?: object | null
) { ): Promise<NextConfigComplete> {
await loadEnvConfig(dir, phase === PHASE_DEVELOPMENT_SERVER, Log) await loadEnvConfig(dir, phase === PHASE_DEVELOPMENT_SERVER, Log)
await loadWebpackHook(phase, dir) await loadWebpackHook(phase, dir)
if (customConfig) { if (customConfig) {
return assignDefaults({ configOrigin: 'server', ...customConfig }) return assignDefaults({
configOrigin: 'server',
...customConfig,
}) as NextConfigComplete
} }
const path = await findUp(CONFIG_FILE, { cwd: dir }) const path = await findUp(CONFIG_FILE, { cwd: dir })
...@@ -459,7 +466,7 @@ export default async function loadConfig( ...@@ -459,7 +466,7 @@ export default async function loadConfig(
configOrigin: CONFIG_FILE, configOrigin: CONFIG_FILE,
configFile: path, configFile: path,
...userConfig, ...userConfig,
}) }) as NextConfigComplete
} else { } else {
const configBaseName = basename(CONFIG_FILE, extname(CONFIG_FILE)) const configBaseName = basename(CONFIG_FILE, extname(CONFIG_FILE))
const nonJsPath = findUp.sync( const nonJsPath = findUp.sync(
...@@ -480,7 +487,7 @@ export default async function loadConfig( ...@@ -480,7 +487,7 @@ export default async function loadConfig(
} }
} }
return defaultConfig return defaultConfig as NextConfigComplete
} }
export function isTargetLikeServerless(target: string) { export function isTargetLikeServerless(target: string) {
......
...@@ -24,7 +24,7 @@ import { isWriteable } from '../../build/is-writeable' ...@@ -24,7 +24,7 @@ import { isWriteable } from '../../build/is-writeable'
import { ClientPagesLoaderOptions } from '../../build/webpack/loaders/next-client-pages-loader' import { ClientPagesLoaderOptions } from '../../build/webpack/loaders/next-client-pages-loader'
import { stringify } from 'querystring' import { stringify } from 'querystring'
import { difference } from '../../build/utils' import { difference } from '../../build/utils'
import { NextConfig } from '../config' import { NextConfigComplete } from '../config-shared'
import { CustomRoutes } from '../../lib/load-custom-routes' import { CustomRoutes } from '../../lib/load-custom-routes'
import { DecodeError } from '../../shared/lib/utils' import { DecodeError } from '../../shared/lib/utils'
...@@ -131,7 +131,7 @@ export default class HotReloader { ...@@ -131,7 +131,7 @@ export default class HotReloader {
private middlewares: any[] private middlewares: any[]
private pagesDir: string private pagesDir: string
private webpackHotMiddleware: (NextHandleFunction & any) | null private webpackHotMiddleware: (NextHandleFunction & any) | null
private config: NextConfig private config: NextConfigComplete
private stats: webpack.Stats | null private stats: webpack.Stats | null
private serverStats: webpack.Stats | null private serverStats: webpack.Stats | null
private clientError: Error | null = null private clientError: Error | null = null
...@@ -154,7 +154,7 @@ export default class HotReloader { ...@@ -154,7 +154,7 @@ export default class HotReloader {
previewProps, previewProps,
rewrites, rewrites,
}: { }: {
config: NextConfig config: NextConfigComplete
pagesDir: string pagesDir: string
buildId: string buildId: string
previewProps: __ApiPreviewProps previewProps: __ApiPreviewProps
......
...@@ -14,7 +14,7 @@ export type ImageConfig = { ...@@ -14,7 +14,7 @@ export type ImageConfig = {
loader: LoaderValue loader: LoaderValue
path: string path: string
domains?: string[] domains?: string[]
disableStaticImages: boolean disableStaticImages?: boolean
} }
export const imageConfigDefault: ImageConfig = { export const imageConfigDefault: ImageConfig = {
......
...@@ -96,6 +96,7 @@ import ResponseCache, { ...@@ -96,6 +96,7 @@ import ResponseCache, {
ResponseCacheEntry, ResponseCacheEntry,
ResponseCacheValue, ResponseCacheValue,
} from './response-cache' } from './response-cache'
import { NextConfigComplete } from './config-shared'
const getCustomRouteMatcher = pathMatch(true) const getCustomRouteMatcher = pathMatch(true)
...@@ -135,7 +136,7 @@ export type ServerConstructor = { ...@@ -135,7 +136,7 @@ export type ServerConstructor = {
export default class Server { export default class Server {
protected dir: string protected dir: string
protected quiet: boolean protected quiet: boolean
protected nextConfig: NextConfig protected nextConfig: NextConfigComplete
protected distDir: string protected distDir: string
protected pagesDir?: string protected pagesDir?: string
protected publicDir: string protected publicDir: string
...@@ -187,7 +188,8 @@ export default class Server { ...@@ -187,7 +188,8 @@ export default class Server {
this.quiet = quiet this.quiet = quiet
loadEnvConfig(this.dir, dev, Log) loadEnvConfig(this.dir, dev, Log)
this.nextConfig = conf this.nextConfig = conf as NextConfigComplete
this.distDir = join(this.dir, this.nextConfig.distDir) this.distDir = join(this.dir, this.nextConfig.distDir)
this.publicDir = join(this.dir, CLIENT_PUBLIC_FILES_PATH) this.publicDir = join(this.dir, CLIENT_PUBLIC_FILES_PATH)
this.hasStaticDir = !minimalMode && fs.existsSync(join(this.dir, 'static')) this.hasStaticDir = !minimalMode && fs.existsSync(join(this.dir, 'static'))
...@@ -207,7 +209,7 @@ export default class Server { ...@@ -207,7 +209,7 @@ export default class Server {
this.renderOpts = { this.renderOpts = {
poweredByHeader: this.nextConfig.poweredByHeader, poweredByHeader: this.nextConfig.poweredByHeader,
canonicalBase: this.nextConfig.amp.canonicalBase, canonicalBase: this.nextConfig.amp.canonicalBase || '',
buildId: this.buildId, buildId: this.buildId,
generateEtags, generateEtags,
previewProps: this.getPreviewProps(), previewProps: this.getPreviewProps(),
......
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