2016-10-28 16:38:24 +02:00
|
|
|
import { join } from 'path'
|
2016-12-17 19:38:11 +01:00
|
|
|
import { existsSync } from 'fs'
|
2016-10-28 16:38:24 +02:00
|
|
|
|
|
|
|
const cache = new Map()
|
|
|
|
|
2016-12-17 19:38:11 +01:00
|
|
|
const defaultConfig = {
|
2016-12-19 16:27:47 +01:00
|
|
|
webpack: null,
|
2017-05-14 01:44:21 +02:00
|
|
|
webpackDevMiddleware: null,
|
2017-04-06 12:09:26 +02:00
|
|
|
poweredByHeader: true,
|
2017-04-18 06:18:43 +02:00
|
|
|
distDir: '.next',
|
2017-05-27 17:40:15 +02:00
|
|
|
assetPrefix: '',
|
2017-05-31 10:06:07 +02:00
|
|
|
configOrigin: 'default',
|
2017-05-27 17:40:15 +02:00
|
|
|
useFileSystemPublicRoutes: true
|
2016-12-17 19:38:11 +01:00
|
|
|
}
|
2016-10-28 16:38:24 +02:00
|
|
|
|
2017-05-31 10:06:07 +02:00
|
|
|
export default function getConfig (dir, customConfig) {
|
2016-10-28 16:38:24 +02:00
|
|
|
if (!cache.has(dir)) {
|
2017-05-31 10:06:07 +02:00
|
|
|
cache.set(dir, loadConfig(dir, customConfig))
|
2016-10-28 16:38:24 +02:00
|
|
|
}
|
|
|
|
return cache.get(dir)
|
|
|
|
}
|
|
|
|
|
2017-05-31 10:06:07 +02:00
|
|
|
function loadConfig (dir, customConfig) {
|
|
|
|
if (customConfig && typeof customConfig === 'object') {
|
|
|
|
customConfig.configOrigin = 'server'
|
|
|
|
return withDefaults(customConfig)
|
|
|
|
}
|
2016-12-17 19:38:11 +01:00
|
|
|
const path = join(dir, 'next.config.js')
|
2016-10-28 16:38:24 +02:00
|
|
|
|
2016-12-17 19:38:11 +01:00
|
|
|
let userConfig = {}
|
2016-10-28 16:38:24 +02:00
|
|
|
|
2016-12-17 19:38:11 +01:00
|
|
|
const userHasConfig = existsSync(path)
|
|
|
|
if (userHasConfig) {
|
|
|
|
const userConfigModule = require(path)
|
|
|
|
userConfig = userConfigModule.default || userConfigModule
|
2017-05-31 10:06:07 +02:00
|
|
|
userConfig.configOrigin = 'next.config.js'
|
2016-12-17 19:38:11 +01:00
|
|
|
}
|
2016-10-28 16:38:24 +02:00
|
|
|
|
2017-05-31 10:06:07 +02:00
|
|
|
return withDefaults(userConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
function withDefaults (config) {
|
|
|
|
return Object.assign({}, defaultConfig, config)
|
2016-10-28 16:38:24 +02:00
|
|
|
}
|