2018-03-30 13:08:09 +00:00
|
|
|
// @flow
|
2017-12-14 02:49:26 +00:00
|
|
|
import findUp from 'find-up'
|
2018-03-31 14:34:52 +00:00
|
|
|
import uuid from 'uuid'
|
2018-06-04 09:38:46 +00:00
|
|
|
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 = {
|
2016-12-19 15:27:47 +00:00
|
|
|
webpack: null,
|
2017-05-13 23:44:21 +00:00
|
|
|
webpackDevMiddleware: null,
|
2018-02-14 17:02:48 +00:00
|
|
|
poweredByHeader: true,
|
2017-04-18 04:18:43 +00:00
|
|
|
distDir: '.next',
|
2017-05-27 15:40:15 +00:00
|
|
|
assetPrefix: '',
|
2017-05-31 08:06:07 +00:00
|
|
|
configOrigin: 'default',
|
2018-02-14 15:20:41 +00:00
|
|
|
useFileSystemPublicRoutes: true,
|
2018-03-31 14:34:52 +00:00
|
|
|
generateBuildId: () => uuid.v4(),
|
2018-03-13 13:18:59 +00:00
|
|
|
generateEtags: true,
|
2018-03-30 13:08:09 +00:00
|
|
|
pageExtensions: ['jsx', 'js']
|
2016-12-17 18:38:11 +00:00
|
|
|
}
|
2016-10-28 14:38:24 +00:00
|
|
|
|
2018-06-04 09:38:46 +00:00
|
|
|
type PhaseFunction = (phase: string, options: {defaultConfig: NextConfig}) => NextConfig
|
2016-10-28 14:38:24 +00:00
|
|
|
|
2018-06-04 09:38:46 +00:00
|
|
|
export default function loadConfig (phase: string, dir: string, customConfig?: NextConfig): NextConfig {
|
|
|
|
if (customConfig) {
|
2017-05-31 08:06:07 +00:00
|
|
|
customConfig.configOrigin = 'server'
|
2018-06-04 09:38:46 +00:00
|
|
|
return {...defaultConfig, ...customConfig}
|
2017-05-31 08:06:07 +00:00
|
|
|
}
|
2018-06-04 09:38:46 +00:00
|
|
|
const path: string = findUp.sync(CONFIG_FILE, {
|
2017-12-14 02:49:26 +00:00
|
|
|
cwd: dir
|
|
|
|
})
|
2016-10-28 14:38:24 +00:00
|
|
|
|
2018-06-04 09:38:46 +00:00
|
|
|
// If config file was found
|
2017-12-14 02:49:26 +00:00
|
|
|
if (path && path.length) {
|
2018-03-30 13:08:09 +00:00
|
|
|
// $FlowFixMe
|
2016-12-17 18:38:11 +00:00
|
|
|
const userConfigModule = require(path)
|
2018-06-04 09:38:46 +00:00
|
|
|
const userConfigInitial: NextConfig | PhaseFunction = userConfigModule.default || userConfigModule
|
|
|
|
if (typeof userConfigInitial === 'function') {
|
|
|
|
return {...defaultConfig, configOrigin: CONFIG_FILE, ...userConfigInitial(phase, {defaultConfig})}
|
2018-02-23 13:42:06 +00:00
|
|
|
}
|
2016-10-28 14:38:24 +00:00
|
|
|
|
2018-06-04 09:38:46 +00:00
|
|
|
return {...defaultConfig, configOrigin: CONFIG_FILE, ...userConfigInitial}
|
|
|
|
}
|
2017-05-31 08:06:07 +00:00
|
|
|
|
2018-06-04 09:38:46 +00:00
|
|
|
return defaultConfig
|
2016-10-28 14:38:24 +00:00
|
|
|
}
|