rsnext/packages/next/server/config-shared.ts

202 lines
4.8 KiB
TypeScript
Raw Normal View History

import os from 'os'
import { Header, Redirect, Rewrite } from '../lib/load-custom-routes'
import { ImageConfig, imageConfigDefault } from './image-config'
type NoOptionals<T> = {
[P in keyof T]-?: T[P]
}
export type NextConfigComplete = NoOptionals<NextConfig>
export interface I18NConfig {
defaultLocale: string
domains?: DomainLocale[]
localeDetection?: false
locales: string[]
}
export interface DomainLocale {
defaultLocale: string
domain: string
http?: true
locales?: string[]
}
export interface ESLintConfig {
/** Only run ESLint on these directories with `next lint` and `next build`. */
dirs?: string[]
/** Do not run ESLint during production builds (`next build`). */
ignoreDuringBuilds?: boolean
}
export type NextConfig = { [key: string]: any } & {
i18n?: I18NConfig | null
eslint?: ESLintConfig
headers?: () => Promise<Header[]>
rewrites?: () => Promise<
| Rewrite[]
| {
beforeFiles: Rewrite[]
afterFiles: Rewrite[]
fallback: Rewrite[]
}
>
redirects?: () => Promise<Redirect[]>
webpack5?: false
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
reactStrictMode?: boolean
publicRuntimeConfig?: { [key: string]: any }
serverRuntimeConfig?: { [key: string]: any }
httpAgentOptions?: { keepAlive?: boolean }
future?: {
/**
* @deprecated this options was moved to the top level
*/
webpack5?: false
strictPostcssConfiguration?: boolean
}
experimental?: {
cpus?: number
plugins?: boolean
profiling?: boolean
2021-07-22 04:42:33 +02:00
isrFlushToDisk?: boolean
reactMode?: 'legacy' | 'concurrent' | 'blocking'
workerThreads?: boolean
pageEnv?: boolean
optimizeImages?: boolean
optimizeCss?: boolean
scrollRestoration?: boolean
stats?: boolean
externalDir?: boolean
conformance?: boolean
amp?: {
optimizer?: any
validator?: string
skipValidation?: boolean
}
reactRoot?: boolean
disableOptimizedLoading?: boolean
gzipSize?: boolean
craCompat?: boolean
add support for esm externals (#27069) add `experimental.esmExternals: boolean | 'loose'` config option remove `output.environment` configuration in favor of `target` | | `esmExternals: false` (default) | `esmExternals: 'loose'` | `esmExternals: true` | | ------------------------ | ------------------------------- | ----------------------- | -------------------- | | import cjs package | `require()` | `require()` | `require()` | | require cjs package | `require()` | `require()` | `require()` | | import mixed package | `require()` *** | `import()` | `import()` | | require mixed package | `require()` | `require()` | `require()` | | import pure esm package | `import()` | `import()` | `import()` | | require pure esm package | Error ** | `import()` * | Error ** | | import pure cjs package | `require()` | `require()` | Resolving error | | require pure cjs package | `require()` | `require()` | `require()` | cjs package: Offers only CJS implementation (may not even have an `exports` field) mixed package: Offers CJS and ESM implementation via `exports` field pure esm package: Only offers an ESM implementation (may not even have an `exports` field) pure cjs package: CommonJs package that prevents importing via `exports` field when `import` is used. `*` This case will behave a bit unexpected for now, since `require` will return a Promise. So that need to be awaited. This will be fixed once the whole next.js bundle is ESM. It didn't work at all before this PR. `**` This is a new Error when trying to require an esm package. `***` For mixed packages we prefer the CommonJS variant to avoid a breaking change. ## 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` - [x] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [x] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes
2021-07-10 18:49:02 +02:00
esmExternals?: boolean | 'loose'
staticPageGenerationTimeout?: number
pageDataCollectionTimeout?: number
isrMemoryCacheSize?: number
}
}
export const defaultConfig: NextConfig = {
env: {},
webpack: null,
webpackDevMiddleware: null,
distDir: '.next',
cleanDistDir: true,
assetPrefix: '',
configOrigin: 'default',
useFileSystemPublicRoutes: true,
generateBuildId: () => null,
generateEtags: true,
pageExtensions: ['tsx', 'ts', 'jsx', 'js'],
target: 'server',
poweredByHeader: true,
compress: true,
analyticsId: process.env.VERCEL_ANALYTICS_ID || '',
images: imageConfigDefault,
devIndicators: {
buildActivity: true,
},
onDemandEntries: {
maxInactiveAge: 60 * 1000,
pagesBufferLength: 2,
},
amp: {
canonicalBase: '',
},
basePath: '',
sassOptions: {},
trailingSlash: false,
i18n: null,
productionBrowserSourceMaps: false,
optimizeFonts: true,
webpack5:
Number(process.env.NEXT_PRIVATE_TEST_WEBPACK4_MODE) > 0 ? false : undefined,
excludeDefaultMomentLocales: true,
serverRuntimeConfig: {},
publicRuntimeConfig: {},
reactStrictMode: false,
httpAgentOptions: {
keepAlive: true,
},
experimental: {
cpus: Math.max(
1,
(Number(process.env.CIRCLE_NODE_TOTAL) ||
(os.cpus() || { length: 1 }).length) - 1
),
plugins: false,
profiling: false,
2021-07-22 04:42:33 +02:00
isrFlushToDisk: true,
workerThreads: false,
pageEnv: false,
optimizeImages: false,
optimizeCss: false,
scrollRestoration: false,
stats: false,
externalDir: false,
reactRoot: Number(process.env.NEXT_PRIVATE_REACT_ROOT) > 0,
disableOptimizedLoading: false,
gzipSize: true,
craCompat: false,
add support for esm externals (#27069) add `experimental.esmExternals: boolean | 'loose'` config option remove `output.environment` configuration in favor of `target` | | `esmExternals: false` (default) | `esmExternals: 'loose'` | `esmExternals: true` | | ------------------------ | ------------------------------- | ----------------------- | -------------------- | | import cjs package | `require()` | `require()` | `require()` | | require cjs package | `require()` | `require()` | `require()` | | import mixed package | `require()` *** | `import()` | `import()` | | require mixed package | `require()` | `require()` | `require()` | | import pure esm package | `import()` | `import()` | `import()` | | require pure esm package | Error ** | `import()` * | Error ** | | import pure cjs package | `require()` | `require()` | Resolving error | | require pure cjs package | `require()` | `require()` | `require()` | cjs package: Offers only CJS implementation (may not even have an `exports` field) mixed package: Offers CJS and ESM implementation via `exports` field pure esm package: Only offers an ESM implementation (may not even have an `exports` field) pure cjs package: CommonJs package that prevents importing via `exports` field when `import` is used. `*` This case will behave a bit unexpected for now, since `require` will return a Promise. So that need to be awaited. This will be fixed once the whole next.js bundle is ESM. It didn't work at all before this PR. `**` This is a new Error when trying to require an esm package. `***` For mixed packages we prefer the CommonJS variant to avoid a breaking change. ## 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` - [x] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [x] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes
2021-07-10 18:49:02 +02:00
esmExternals: false,
staticPageGenerationTimeout: 60,
pageDataCollectionTimeout: 60,
// default to 50MB limit
isrMemoryCacheSize: 50 * 1024 * 1024,
},
future: {
strictPostcssConfiguration: false,
},
}
export function normalizeConfig(phase: string, config: any) {
if (typeof config === 'function') {
config = config(phase, { defaultConfig })
if (typeof config.then === 'function') {
throw new Error(
'> Promise returned in next config. https://nextjs.org/docs/messages/promise-in-next-config'
)
}
}
return config
}