1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/server/config.js

54 lines
1.3 KiB
JavaScript
Raw Normal View History

// @flow
import findUp from 'find-up'
import uuid from 'uuid'
2016-10-28 14:38:24 +00:00
const cache = new Map()
const defaultConfig = {
webpack: null,
webpackDevMiddleware: null,
poweredByHeader: true,
distDir: '.next',
assetPrefix: '',
configOrigin: 'default',
useFileSystemPublicRoutes: true,
generateBuildId: () => uuid.v4(),
generateEtags: true,
pageExtensions: ['jsx', 'js']
}
2016-10-28 14:38:24 +00:00
export default function getConfig (phase: string, dir: string, customConfig?: ?Object) {
2016-10-28 14:38:24 +00:00
if (!cache.has(dir)) {
cache.set(dir, loadConfig(phase, dir, customConfig))
2016-10-28 14:38:24 +00:00
}
return cache.get(dir)
}
export function loadConfig (phase: string, dir: string, customConfig?: ?Object) {
if (customConfig && typeof customConfig === 'object') {
customConfig.configOrigin = 'server'
return withDefaults(customConfig)
}
const path: string = findUp.sync('next.config.js', {
cwd: dir
})
2016-10-28 14:38:24 +00:00
let userConfig = {}
2016-10-28 14:38:24 +00:00
if (path && path.length) {
// $FlowFixMe
const userConfigModule = require(path)
userConfig = userConfigModule.default || userConfigModule
if (typeof userConfigModule === 'function') {
userConfig = userConfigModule(phase, {defaultConfig})
}
userConfig.configOrigin = 'next.config.js'
}
2016-10-28 14:38:24 +00:00
return withDefaults(userConfig)
}
function withDefaults (config: Object) {
return Object.assign({}, defaultConfig, config)
2016-10-28 14:38:24 +00:00
}