-
Notifications
You must be signed in to change notification settings - Fork 27k
/
config.js
63 lines (54 loc) · 1.94 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// @flow
import findUp from 'find-up'
import {CONFIG_FILE} from '../lib/constants'
type WebpackConfig = *
type WebpackDevMiddlewareConfig = *
export type NextConfig = {|
webpack: null | (webpackConfig: WebpackConfig, {dir: string, dev: boolean, isServer: boolean, buildId: string, config: NextConfig, defaultLoaders: {}, totalPages: number}) => WebpackConfig,
webpackDevMiddleware: null | (WebpackDevMiddlewareConfig: WebpackDevMiddlewareConfig) => WebpackDevMiddlewareConfig,
poweredByHeader: boolean,
distDir: string,
assetPrefix: string,
configOrigin: string,
useFileSystemPublicRoutes: boolean,
generateBuildId: () => string,
generateEtags: boolean,
pageExtensions: Array<string>
|}
const defaultConfig: NextConfig = {
webpack: null,
webpackDevMiddleware: null,
poweredByHeader: true,
distDir: '.next',
assetPrefix: '',
configOrigin: 'default',
useFileSystemPublicRoutes: true,
generateBuildId: () => {
// nanoid is a small url-safe uuid generator
const nanoid = require('nanoid')
return nanoid()
},
generateEtags: true,
pageExtensions: ['jsx', 'js']
}
type PhaseFunction = (phase: string, options: {defaultConfig: NextConfig}) => NextConfig
export default function loadConfig (phase: string, dir: string, customConfig?: NextConfig): NextConfig {
if (customConfig) {
customConfig.configOrigin = 'server'
return {...defaultConfig, ...customConfig}
}
const path: string = findUp.sync(CONFIG_FILE, {
cwd: dir
})
// If config file was found
if (path && path.length) {
// $FlowFixMe
const userConfigModule = require(path)
const userConfigInitial: NextConfig | PhaseFunction = userConfigModule.default || userConfigModule
if (typeof userConfigInitial === 'function') {
return {...defaultConfig, configOrigin: CONFIG_FILE, ...userConfigInitial(phase, {defaultConfig})}
}
return {...defaultConfig, configOrigin: CONFIG_FILE, ...userConfigInitial}
}
return defaultConfig
}