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
Felix-Antoine Paradis 442c611d49 Add next configuration as an option for custom servers (#2058)
* Add a configuration parameter to custom server startup

* Adding related documentation

* Do not access filesystem if configuration is supplied

* Make the configuration log clearer

* Make the conf default value to `null`
2017-05-31 13:36:07 +05:30

45 lines
1 KiB
JavaScript

import { join } from 'path'
import { existsSync } from 'fs'
const cache = new Map()
const defaultConfig = {
webpack: null,
webpackDevMiddleware: null,
poweredByHeader: true,
distDir: '.next',
assetPrefix: '',
configOrigin: 'default',
useFileSystemPublicRoutes: true
}
export default function getConfig (dir, customConfig) {
if (!cache.has(dir)) {
cache.set(dir, loadConfig(dir, customConfig))
}
return cache.get(dir)
}
function loadConfig (dir, customConfig) {
if (customConfig && typeof customConfig === 'object') {
customConfig.configOrigin = 'server'
return withDefaults(customConfig)
}
const path = join(dir, 'next.config.js')
let userConfig = {}
const userHasConfig = existsSync(path)
if (userHasConfig) {
const userConfigModule = require(path)
userConfig = userConfigModule.default || userConfigModule
userConfig.configOrigin = 'next.config.js'
}
return withDefaults(userConfig)
}
function withDefaults (config) {
return Object.assign({}, defaultConfig, config)
}