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
Tim Neutkens 903f15acc4 Make pageExtensions configurable (#3787)
* Make page require faster

* Add windows search/replace

* Use normalize instead of resolve

* Add remaining tests

* Use sep instead of /

* Add test files

* Make component require faster

* Add console.error

* Make pageExtensions configurable

* Remove resolve.js

* Add test for `.jsx`

* Also resolve `/nav/index` and the likes

* Normalize page paths

* Use config passed off by webpack
2018-02-14 20:50:41 +05:30

48 lines
1.3 KiB
JavaScript

import findUp from 'find-up'
const cache = new Map()
const defaultConfig = {
webpack: null,
webpackDevMiddleware: null,
distDir: '.next',
assetPrefix: '',
configOrigin: 'default',
useFileSystemPublicRoutes: true,
pageExtensions: ['jsx', 'js'] // jsx before js because otherwise regex matching will match js first
}
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 = findUp.sync('next.config.js', {
cwd: dir
})
let userConfig = {}
if (path && path.length) {
const userConfigModule = require(path)
userConfig = userConfigModule.default || userConfigModule
if (userConfig.poweredByHeader === true || userConfig.poweredByHeader === false) {
console.warn('> the `poweredByHeader` option has been removed https://err.sh/zeit/next.js/powered-by-header-option-removed')
}
userConfig.configOrigin = 'next.config.js'
}
return withDefaults(userConfig)
}
function withDefaults (config) {
return Object.assign({}, defaultConfig, config)
}