Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add next configuration as an option for custom servers #2058

Merged
merged 6 commits into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
2 changes: 1 addition & 1 deletion server/build/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 14 additions & 4 deletions server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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)
}
6 changes: 3 additions & 3 deletions server/hot-reloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 3 additions & 3 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we've a config via the option, it shouldn't read from the filesystem. Change the code to reflect that.

And make sure to get default configurations. May be we need to add method called withDefaults to server/config.js

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, for my use case, I still want to be able to read from filesystem to overwrite default & custom server.

That's why I have this order in Object.assign

Your thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arunoda Is this OK?

this.dist = this.config.distDir
this.buildStats = !dev ? require(join(this.dir, this.dist, 'build-stats.json')) : null
this.buildId = !dev ? this.readBuildId() : '-'
Expand Down