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

60 lines
1.8 KiB
JavaScript
Raw Normal View History

// @flow
import findUp from 'find-up'
2018-10-01 22:55:31 +00:00
import {CONFIG_FILE} from 'next-server/constants'
type WebpackConfig = *
type WebpackDevMiddlewareConfig = *
export type NextConfig = {|
webpack: null | (webpackConfig: WebpackConfig, {dir: string, dev: boolean, isServer: boolean, buildId: string, config: NextConfig, defaultLoaders: {}, totalPages: number}) => WebpackConfig,
webpackDevMiddleware: null | (WebpackDevMiddlewareConfig: WebpackDevMiddlewareConfig) => WebpackDevMiddlewareConfig,
poweredByHeader: boolean,
distDir: string,
assetPrefix: string,
configOrigin: string,
useFileSystemPublicRoutes: boolean,
generateBuildId: () => string,
generateEtags: boolean,
pageExtensions: Array<string>
|}
const defaultConfig: NextConfig = {
webpack: null,
webpackDevMiddleware: null,
poweredByHeader: true,
distDir: '.next',
assetPrefix: '',
configOrigin: 'default',
useFileSystemPublicRoutes: true,
generateBuildId: () => null,
generateEtags: true,
pageExtensions: ['jsx', 'js']
}
2016-10-28 14:38:24 +00:00
type PhaseFunction = (phase: string, options: {defaultConfig: NextConfig}) => NextConfig
2016-10-28 14:38:24 +00:00
export default function loadConfig (phase: string, dir: string, customConfig?: NextConfig): NextConfig {
if (customConfig) {
customConfig.configOrigin = 'server'
return {...defaultConfig, ...customConfig}
}
const path: string = findUp.sync(CONFIG_FILE, {
cwd: dir
})
2016-10-28 14:38:24 +00:00
// If config file was found
if (path && path.length) {
// $FlowFixMe
const userConfigModule = require(path)
const userConfigInitial: NextConfig | PhaseFunction = userConfigModule.default || userConfigModule
if (typeof userConfigInitial === 'function') {
return {...defaultConfig, configOrigin: CONFIG_FILE, ...userConfigInitial(phase, {defaultConfig})}
}
2016-10-28 14:38:24 +00:00
return {...defaultConfig, configOrigin: CONFIG_FILE, ...userConfigInitial}
}
return defaultConfig
2016-10-28 14:38:24 +00:00
}