提交 442c611d 编写于 作者: F Felix-Antoine Paradis 提交者: Arunoda Susiripala

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`
上级 e6f36517
......@@ -576,6 +576,7 @@ Supported options:
- `dev` (`bool`) whether to launch Next.js in dev mode - default `false`
- `dir` (`string`) where the Next project is located - default `'.'`
- `quiet` (`bool`) Hide error messages containing server information - default `false`
- `conf` (`object`) the same object you would use in `next.config.js` - default `{}`
Then, change your `start` script to `NODE_ENV=production node server.js`.
......
......@@ -308,7 +308,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false,
}
if (config.webpack) {
console.log('> Using "webpack" config function defined in next.config.js.')
console.log(`> Using "webpack" config function defined in ${config.configOrigin}.`)
webpackConfig = await config.webpack(webpackConfig, { dev })
}
return webpack(webpackConfig)
......
......@@ -9,17 +9,22 @@ const defaultConfig = {
poweredByHeader: true,
distDir: '.next',
assetPrefix: '',
configOrigin: 'default',
useFileSystemPublicRoutes: true
}
export default function getConfig (dir) {
export default function getConfig (dir, customConfig) {
if (!cache.has(dir)) {
cache.set(dir, loadConfig(dir))
cache.set(dir, loadConfig(dir, customConfig))
}
return cache.get(dir)
}
function loadConfig (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 = {}
......@@ -28,7 +33,12 @@ function loadConfig (dir) {
if (userHasConfig) {
const userConfigModule = require(path)
userConfig = userConfigModule.default || userConfigModule
userConfig.configOrigin = 'next.config.js'
}
return Object.assign({}, defaultConfig, userConfig)
return withDefaults(userConfig)
}
function withDefaults (config) {
return Object.assign({}, defaultConfig, config)
}
......@@ -8,7 +8,7 @@ import clean from './build/clean'
import getConfig from './config'
export default class HotReloader {
constructor (dir, { quiet } = {}) {
constructor (dir, { quiet, conf } = {}) {
this.dir = dir
this.quiet = quiet
this.middlewares = []
......@@ -22,7 +22,7 @@ export default class HotReloader {
this.prevFailedChunkNames = null
this.prevChunkHashes = null
this.config = getConfig(dir)
this.config = getConfig(dir, conf)
}
async run (req, res) {
......@@ -148,7 +148,7 @@ export default class HotReloader {
}
if (this.config.webpackDevMiddleware) {
console.log('> Using "webpackDevMiddleware" config function defined in next.config.js.')
console.log(`> Using "webpackDevMiddleware" config function defined in ${this.config.configOrigin}.`)
webpackDevMiddlewareConfig = this.config.webpackDevMiddleware(webpackDevMiddlewareConfig)
}
......
......@@ -24,14 +24,14 @@ const internalPrefixes = [
]
export default class Server {
constructor ({ dir = '.', dev = false, staticMarkup = false, quiet = false } = {}) {
constructor ({ dir = '.', dev = false, staticMarkup = false, quiet = false, conf = null } = {}) {
this.dir = resolve(dir)
this.dev = dev
this.quiet = quiet
this.router = new Router()
this.hotReloader = dev ? new HotReloader(this.dir, { quiet }) : null
this.hotReloader = dev ? new HotReloader(this.dir, { quiet, conf }) : null
this.http = null
this.config = getConfig(this.dir)
this.config = getConfig(this.dir, conf)
this.dist = this.config.distDir
this.buildStats = !dev ? require(join(this.dir, this.dist, 'build-stats.json')) : null
this.buildId = !dev ? this.readBuildId() : '-'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册