2017-12-14 02:49:26 +00:00
|
|
|
import findUp from 'find-up'
|
2016-10-28 14:38:24 +00:00
|
|
|
|
|
|
|
const cache = new Map()
|
|
|
|
|
2016-12-17 18:38:11 +00:00
|
|
|
const defaultConfig = {
|
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-13 13:18:59 +00:00
|
|
|
generateEtags: true,
|
2018-02-14 15:20:41 +00:00
|
|
|
pageExtensions: ['jsx', 'js'] // jsx before js because otherwise regex matching will match js first
|
2016-12-17 18:38:11 +00:00
|
|
|
}
|
2016-10-28 14:38:24 +00:00
|
|
|
|
2018-02-23 13:42:06 +00:00
|
|
|
export default function getConfig (phase, dir, customConfig) {
|
2016-10-28 14:38:24 +00:00
|
|
|
if (!cache.has(dir)) {
|
2018-02-23 13:42:06 +00:00
|
|
|
cache.set(dir, loadConfig(phase, dir, customConfig))
|
2016-10-28 14:38:24 +00:00
|
|
|
}
|
|
|
|
return cache.get(dir)
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:42:06 +00:00
|
|
|
export function loadConfig (phase, dir, customConfig) {
|
2017-05-31 08:06:07 +00:00
|
|
|
if (customConfig && typeof customConfig === 'object') {
|
|
|
|
customConfig.configOrigin = 'server'
|
|
|
|
return withDefaults(customConfig)
|
|
|
|
}
|
2017-12-14 02:49:26 +00:00
|
|
|
const path = findUp.sync('next.config.js', {
|
|
|
|
cwd: dir
|
|
|
|
})
|
2016-10-28 14:38:24 +00:00
|
|
|
|
2016-12-17 18:38:11 +00:00
|
|
|
let userConfig = {}
|
2016-10-28 14:38:24 +00:00
|
|
|
|
2017-12-14 02:49:26 +00:00
|
|
|
if (path && path.length) {
|
2016-12-17 18:38:11 +00:00
|
|
|
const userConfigModule = require(path)
|
|
|
|
userConfig = userConfigModule.default || userConfigModule
|
2018-02-23 13:42:06 +00:00
|
|
|
if (typeof userConfigModule === 'function') {
|
|
|
|
userConfig = userConfigModule(phase, {defaultConfig})
|
|
|
|
}
|
2017-05-31 08:06:07 +00:00
|
|
|
userConfig.configOrigin = 'next.config.js'
|
2016-12-17 18:38:11 +00:00
|
|
|
}
|
2016-10-28 14:38:24 +00:00
|
|
|
|
2017-05-31 08:06:07 +00:00
|
|
|
return withDefaults(userConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
function withDefaults (config) {
|
|
|
|
return Object.assign({}, defaultConfig, config)
|
2016-10-28 14:38:24 +00:00
|
|
|
}
|